使用VBA循环使用不同名称的工作表

时间:2017-06-27 11:51:50

标签: excel vba excel-vba loops

Dim Var_1 As String
Dim Var_2 As String
Dim Var_3 As String
Dim Var_4 As String
Dim Var_5 As String
Dim Var_6 As String
Dim Var_7 As String
Dim Var_8 As String
Dim Var_9 As String
Dim Var_10 As String
Dim Var_11 As String
Dim Var_12 As String
Dim Var_13 As String
Dim Var_14 As String
Dim Var_15 As String
Dim Var_16 As String
Dim Var_17 As String
Dim Var_18 As String
Dim Var_19 As String
Dim Var_20 As String
Dim Var_21 As String
Dim Var_22 As String
Dim Var_23 As String
Dim Var_24 As String
Dim Var_25 As String
Dim Var_26 As String
Dim Var_27 As String
Dim Var_28 As String
Dim Var_29 As String

Var_1 = "Ex-Bidadi"
Var_2 = "Ex-Hospet"
Var_3 = "Ex-Chennai"
Var_4 = "Ex-Coimbatore"
Var_5 = "Ex-Gangaikondan"
Var_6 = "Ex-Pune"
Var_7 = "Ex-Goa"
Var_8 = "Ex-Mumbai"
Var_9 = "Ex-Nashik"
Var_10 = "Ex-Aurangabad"
Var_11 = "Ex-Goblej"
Var_12 = "Ex-Hyderabad"
Var_13 = Ex - Vizag
Var_14 = "Ex-Vijayawada"
Var_15 = "Ex-Chittoor"
Var_16 = "Ex - Siliguri"
Var_17 = "Ex-odhisha"
Var_18 = "Ex-Jharkhand"
Var_19 = "Ex-Bihar"
Var_20 = "Ex-NorthEast"
Var_21 = "Ex-Delhi"
Var_22 = "Ex-Udaipur"
Var_23 = "Ex-Jammu"
Var_24 = "Ex-Haridwar"
Var_25 = "Ex-Dasna"
Var_26 = "Ex-Kanpur"
Var_27 = "Ex-Unnao"
Var_28 = "Ex-Var_anasi"
Var_29 = "Ex-Bhopal"

我正在向您展示我的代码的一部分,我只是想知道如何循环这些我尝试使用此表示

For n = 1 to 29
    For i = 3 To 445
        For m = 28 To 40
            ActiveWorkbook.Sheets("Var_" & n).Cells(i, m) = 999999
            least(i, m, n) =  ActiveWorkbook.Sheets("Var_" & n).Cells(i, m)
        Next m
    Next i
Next n

我已经定义了最小(i,m,n)数组,但是循环显示下标的错误超出范围我尝试过使用所有可能但循环不起作用

3 个答案:

答案 0 :(得分:1)

您需要将工作表名称存储在数组中:

Var = Array("Ex-Bidadi", "Ex-Hospet", "Ex-Chennai", "Ex-Coimbatore", "Ex-Gangaikondan", "Ex-Pune", "Ex-Goa", "Ex-Mumbai", "Ex-Nashik", "Ex-Aurangabad", "Ex-Goblej", "Ex-Hyderabad", "Ex - Vizag", "Ex-Vijayawada", "Ex-Chittoor", "Ex - Siliguri", "Ex-odhisha", "Ex-Jharkhand", "Ex-Bihar", "Ex-NorthEast", "Ex-Delhi", "Ex-Udaipur", "Ex-Jammu", "Ex-Haridwar", "Ex-Dasna", "Ex-Kanpur", "Ex-Unnao", "Ex-Var_anasi", "Ex-Bhopal")

然后在循环中使用以下内容:

least(i, m, n) = ActiveWorkbook.Sheets(Var(n)).Cells(i, m)

答案 1 :(得分:1)

创建工作表的数组

Dim Var
Var = Array("Ex-Bidadi"", "Ex-Hospet", "Ex-Chennai" ... till the last sheet)

计算阵列中包含的工作表数量。数组从索引0开始。

For n = 0 to 28 '29 sheets less 1, because of the array index 0
     For i = 3 To 445
         For m = 28 To 40
             ActiveWorkbook.Sheets(Var(n)).Cells(i, m) = 999999
             least(i, m, n) =  ActiveWorkbook.Sheets(Var(n)).Cells(i, m)
         Next m
     Next i
Next n

答案 2 :(得分:1)

遍历所有工作表,然后在SELECT CASE语句中选择您需要的工作表:

Sub Test()
    Dim wrkSht As Worksheet
    Dim i As Long, m As Long
    Dim cLeast As Collection

    Set cLeast = New Collection
    For Each wrkSht In ThisWorkbook.Worksheets
        'NB:  If you want sheets that start with "Ex-" use commented lines instead:
        'Select Case Left(wrkSht.Name, 3)
        Select Case wrkSht.Name
            'Case "Ex-"
            Case "Ex-Bidadi", "Ex-Hospet", "Ex-Chnnai"
                For i = 3 To 445
                    For m = 28 To 40
                        cLeast.Add wrkSht.Cells(i, m), wrkSht.Name & "|" & i & "|" & m
                    Next m
                Next i
        Case Else
          'Code if not the sheet you're after.
        End Select
    Next wrkSht

    Debug.Print cLeast("Ex-Bidadi|3|28")

End Sub

注意:我已经在循环中使用了一个集合,因为我不知道你在追求什么。字典可能是更好的方式。