我有一个项目,我可以为用户选择随机范围。
在单个选择上执行时,一切都有效。如果用户选择多个选择,则仅运行第一个选择的代码。我想运行所有选定的单元格。我尝试使用多个选择和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
答案 0 :(得分:1)
您可以循环浏览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 Sub
注意:在您的声明中,它必须是Public A As Integer, B As Integer
。否则,只有B
被定义为Integer
而A
将被定义为Variant
。