我有一个项目,我有随机范围选择的用户。
当在单个选择上执行时,一切都可以工作。如果用户选择多个选择,则只运行第一个选择的代码。我想要运行所有选定的单元格。我试图使用多个选择和Application.Intersect方法,但它们没有起作用。
Public A, B As Integer
Sub AutoLabel()
A = 1
B = 1
End Sub
'=======================================================
Sub LabelTest()
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
SR = .Row
SC = .Column
LR = SR + .Rows.Count - 1
LC = SC + .Columns.Count - 1
End With
For Rcount = SR To LR
For CCount = SC To LC
Cells(Rcount, CCount).value = B & Mid("ABCDEFGHIJKLMNOPQRSTUVWXYZ", A, 1)
A = A + 1
If A = 5 Then A = 1: B = B + 1
Next
Next
End Sub发布于 2017-03-28 02:43:20
您可以循环遍历Selection中的每个单元格,尝试下面的代码:
Sub LabelTest()
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Dim Cell As Range
For Each Cell In Selection
Cell.Value = B & Mid("ABCDEFGHIJKLMNOPQRSTUVWXYZ", A, 1)
A = A + 1
If A = 5 Then A = 1: B = B + 1
Next Cell
End SubNote:在您的声明中,它需要是Public A As Integer, B As Integer。否则,只有B将被定义为Integer,A将定义为Variant。
https://stackoverflow.com/questions/43059749
复制相似问题