簡體   English   中英

在VBA中循環瀏覽具有相似名稱結構的工作表

[英]Loop through Worksheets with similar name structure in VBA

我的Excel文件具有工作表的以下結構:

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

因此,您可以看到A的4倍,B的3倍,C的5倍,等等(均勻分布)

我想做的是遍歷工作表組並應用一些代碼。 在這種情況下,組內必須是相同的代碼,但組之間必須是不同的代碼,因此我不能簡單地同時遍歷所有工作表。

我知道如何在VBA中獲取工作表的名稱。 我的第一個想法是首先從組名的右邊“截去”數字(最后一個字符),然后確定結果的唯一組。 然后,我想為每個組循環,例如,第一個循環將從A1開始並在A4處停止。 但是,如何告訴VBA在名稱中標識一個非恆定的上限(在示例A4,B3,C5等)中呢?

也許那也不是最有效的方法。 如果可以的話,我什至可以將所有工作表重命名為其他系統,但是無論如何都必須使用編碼。 任何想法都受到高度贊賞。

簡而言之,我想做什么:

1)按名稱(在上例中為A,B,C)標識工作組的唯一組

2)對於每個組,循環瀏覽所有關聯的工作表並應用一些代碼

謝謝。

為了標識唯一的組,您可以執行一個循環,該循環查看工作表名稱,如果它具有“ 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

然后,您可以為每種工作表類型創建其他宏,並在上面的代碼中調用它。 即:

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

編輯:對於只希望在某些工作表上執行此操作或設置某些上限的部分...如果您確切知道要在哪些工作表上運行代碼,則可以將其放入數組中,然后運行代碼僅在該陣列中的圖紙上。

類似於(偽代碼):

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

我建議使用字典來定義組和每個組中的工作表計數。

這是一個主意:

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

還有一種方法..如果您的名字只是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM