在VBA中通过具有类似名称结构的工作表进行循环

我的Excel文件具有以下工作表结构:

A1 A2 A3 A4 B1 B2 B3 C1 C2 C3 C4 C5

所以你可以看到4次A,3次B,5次C等(不均匀分布)

我想要做的是循环工作表组和应用一些代码。 在这种情况下,它必须是组内相同的代码,但组间代码不同,因此我不能简单地在所有工作表中循环。

我知道如何获取VBA中工作表的名称。 我的第一个想法是首先从组名右边删除数字(最后一个字符),然后确定唯一的结果组。 然后,我想循环每个组,例如,第一个循环将在A1开始,然后停在A4。 但是我怎样才能告诉VBA确定一个名称不是常数的上边界(例如A4,然后是B3,然后是C5等)?

也许这不是最有效的方法。 我甚至可以将我的所有工作表重命名为不同的系统,如果这样做是有道理的,但是编码必须在任何情况下应用。 任何想法,高度赞赏。

简而言之,我想要做的是:

1)按名称识别唯一的作品集(在上面的例子中是A,B,C)

2)对于每个组,循环遍历所有关联的工作表并应用一些代码

谢谢。

还有一种方法,如果你的名字只是A – Z

 Sub DoStuff() Dim i As Integer Dim counter As Integer 'loop through A to Z For i = 65 To 90 counter = 1 'loop until we no longer have a valid sheet Do While isWorksheet(Chr(i) + CStr(counter)) = True 'do work by calling the correct method Run (setSubName(Chr(i))) counter = counter + 1 Loop Next i End Sub 'check to see if the worksheet exists Function isWorksheet(name As String) As Boolean On Error GoTo wsError Err.Clear 'just try and access the name Sheets(name).name wsError: If Err.Number = 0 Then isWorksheet = True Else isWorksheet = False End If End Function 'set the sub routine name to call Function setSubName(value As String) As String setSubName = Switch(value = "A", "Sub_A_Name", value = "B", "Sub_B_Name", _ value = "C", "Sub_C_Name", value = "D", "Sub_D_Name") End Function Sub Sub_A_Name() 'do work for group A End Sub 

为了识别唯一的组,你可以做一个查看工作表名称的循环,如果它有一个“A”,做X,“B”,做Y等等。

 Dim ws as Worksheet For each ws in Activebook.Sheets If ws.name like "A*" Then ** Code for "A" worksheets Else If ws.name like "B" Then ** code for "B*" worksheets Else If [...] End if Next ws 

然后,您可以为每个表单types创build其他macros,并在上面的代码中调用它。 即:

 Private Sub A_Things() msgbox("This is a sheet 'A' type") [...whatever other code you want] End Sub Private Sub B_Things() msgbox("This is a sheet 'B' type") [...whatever other code you want] End Sub Sub checkSheets() Dim ws as Worksheet For each ws in Activebook.Sheets If ws.name like "A*" Then Call A_Things Else If ws.name like "B" Then Call B_Things Else If [...] End if Next ws End Sub 

编辑:对于只想在某些表单上执行此操作的部分,或者设置一些上限…如果您确切知道要在哪些表单上运行代码,则可以将它们放入数组中,然后运行代码只在该数组中的表单上。

像(psuedocode):

 Dim nameArray() as Variant ReDim nameArray(4) 'Note, this can hold 5 values, so if you have X sheets, ReDim this to X-1 sheets nameArray = Array("A1","A2","A4","B1","B3") for i = 0 to UBound(nameArray()) 'this will loop through Sheet A1, then A2, then A4, etc. and run the code below If nameArray(i) = "A1" Then [run A1 code] ElseIf [...] End If Next i 

我build议使用字典来定义每个组中的工作表和组数。

这是一个想法:

 Sub LoopThroughGroupsOfSheets() 'Needs reference to MS Scripting Runtime Dim dic As Dictionary Dim i As Integer, k As Integer Dim wshName As String 'define Sheets for loop and count of them Set dic = New Dictionary dic.Add "A", 4 dic.Add "B", 3 dic.Add "C", 5 For k = 0 To dic.Count - 1 For i = 1 To dic.Items(k) DoSomething dic.Keys(k) & i Next Next End Sub Sub DoSomething(wshName As String) Debug.Print wshName End Sub 

结果:

 SheetName: A1 SheetName: A2 SheetName: A3 SheetName: A4 SheetName: B1 SheetName: B2 SheetName: B3 SheetName: C1 SheetName: C2 SheetName: C3 SheetName: C4 SheetName: C5