【问题标题】:How to run a macro in different excel worksheets? It is not looping如何在不同的 Excel 工作表中运行宏?它没有循环
【发布时间】:2020-12-21 23:05:20
【问题描述】:

工作表 1 的 8 列中有数据。工作表 2 中有类似的信息。我正在尝试在两个工作表中运行宏。 (一个接一个)

一旦在 A 列中找到空白单元格,我将尝试退出工作表 1 中的宏。然后移至下一个工作表。在工作表 2 中完成宏后,退出 sub。

我根本不是程序员。我只是在学习一些东西。

请告诉我该怎么做。

 '''
    Sub Macro3()

      Dim cell As Range
      Dim i As Integer, row As Integer, column As Integer
      Dim ws As Worksheet
      i = 0
      row = 1
      column = 1
          For Each ws In Worksheets
             For Each cell In Range("$A:$A")
                If Cells(row, 1) = "" Then
                   Exit Sub
                Else
                   If Mid(Cells(row, column), 1, 1) = "R" Then
                   Cells(row, column + 1) = "RES" & "_" & Cells(row, column + 5)
                   ElseIf Mid(Cells(row, column), 1, 2) = "FI" Then
                   Cells(row, column + 1) = "FID" & "_" & Cells(row, column + 5)
                   ElseIf Mid(Cells(row, column), 1, 1) = "F" Then
                   Cells(row, column + 1) = "FUSE" & "_" & Cells(row, column + 5)
                   ElseIf Mid(Cells(row, column), 1, 1) = "C" Then
                 Else
                   Cells(row, column + 1) = "N/A"
                   Cells(row, column + 1).Interior.ColorIndex = 37
               End If
                  i = i + 1
                  row = i + 1
               End If
           Next cell
         Next
       End Sub

'''

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    不同的可能解决方案,但目前您告诉它在第一个空白单元格上退出 sub 而不是进入下一个工作表。也许尝试添加工作表的数量,然后如果您在最终工作表上则退出(假设您只有 2 张工作表)。

    如果空白单元格意味着工作表中没有更多数据,那么您最好添加一个变量来查找最后使用的行,并在循环开始时将row = row +1 更改为For row =1 to lastrow Next 在底部,lastrow=Range("A" & Rows.Count).End(xlUp).Row 然后单元格检查可以在最后使用的行结束并移动到下一个工作表。

      Dim i As Integer, column As Integer, wscount as Integer
      Dim ws As Worksheet
      Dim row as Long ' you should use long rather than integer as if you ever have data beyond row 32kish the code will fail as that as integers stop there
      Dim MyAr(3)
      Dim NewAR(3)
      wscount = Application.Sheets.Count
      MyAr(1) = "R"
      MyAr(2) = "FI"
      MyAr(3) = "F"
      NewAr(1) = "RES"
      NewAr(2) = "FID"
      NewAr(3) = "FUSE"
      column = 1
          For Each ws In Worksheets
          lastrow=Range("A" & Rows.Count).End(xlUp).Row
             For row =1 to lastrow
                If Cells(row, 1) = "" and ActiveSheet.Index = wscount Then
                   Exit Sub*
              
              For i =1 to 3
              If Mid(Cells(row, column), 1, 1) = MyAr(i) Then _
                   Cells(row, column + 1) = NewAr(i) & "_" & Cells(row, column + 5) 'If you are just looking at the start of a cell you can use Left as well as Mid
              Next i
                   
         If Mid(Cells(row, column), 1, 1) = "C" Then _
             Cells(row, column + 1) = "N/A"
             Cells(row, column + 1).Interior.ColorIndex = 37
         End If
                               
    
         Next row
         Next ws
       End Sub
    

    【讨论】:

    • 旁注:If .. End If 结构中没有重复的 Else 关键字,重新组织逻辑或尝试 ElseIf :-) 顺便说一句,始终使用完全限定的范围引用。
    • 您是否尝试过自己的答案? a) If .. Else .. Else .. Endif 语法不正确; b) 在没有完全限定范围引用的情况下循环工作表集合不会影响返回的单元格值,因为 VBA 然后总是假定活动工作表。
    【解决方案2】:

    我想我想通了。感谢您的反馈意见。这是更新后的代码:

     '''
     Sub MacroTest()
     For Each ws In Worksheets
        ws.Activate
            i = 0
            row = 1
            column = 1
                For Each cell In Range("$A:$A")
                    If Cells(row, 1) = "" Then
                Exit For
                    Else
                        If Mid(Cells(row, column), 1, 1) = "R" Then
                          Cells(row, column + 1) = "RES" & "_" & Cells(row, column + 5)
                          ElseIf Mid(Cells(row, column), 1, 2) = "FI" Then
                          Cells(row, column + 1) = "FID" & "_" & Cells(row, column + 5)
                          ElseIf Mid(Cells(row, column), 1, 1) = "F" Then
                          Cells(row, column + 1) = "FUSE" & "_" & Cells(row, column + 5)
                        Else
                             Cells(row, column + 1) = "N/A"
                             Cells(row, column + 1).Interior.ColorIndex = 37
                        End If
                            i = i + 1
                            row = i + 1
                      End If
                Next cell
            Next ws
         End If
       End If
     End Sub
     '''
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多