同时定位多个单元

我有一个项目,我有随机范围select的用户。

一切工作时,一个单一的select。 如果用户select多个select,则仅运行第一select的代码。 我想运行所有选定的单元格。 我试图使用多选和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 

您可以循环Selection您的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被定义为IntegerA被定义为Variant