【问题标题】:For Each Worksheet in Workbook [duplicate]对于工作簿中的每个工作表 [重复]
【发布时间】:2016-01-07 13:46:56
【问题描述】:

有什么想法为什么下面的代码不会在工作表中循环?

我正在尝试根据工作表名称在每个工作表中设置一列。它卡在活动工作表上,忽略了If ws.Name <> "Default"。这是作为一个模块构建的:

Sub LangID_update()

Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Dim LastCol As Integer
Dim LastRow As Integer
Dim rng As Range

Application.ScreenUpdating = False

For Each ws In wb.Worksheets
If ws.Name <> "Default" Then

LastCol = ws.Cells(1, Columns.count).End(xlToLeft).Column
LastRow = ws.Cells(Rows.count, 1).End(xlUp).Row
Set rng = Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
    With rng
        For Each c In Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
            If ws.Name = "ARGS" Then c.Value = "ESP"
            If ws.Name = "AUTS" Then c.Value = "GR"
            If ws.Name = "DEUS" Then c.Value = "GR"

        Next c
    End With
    End If
Next

Application.ScreenUpdating = True
Set wb = Nothing
Set ws = Nothing
Set rng = Nothing

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您使用的许多对象引用都是不合格的,因此引用了活动工作表。在每个对象的开头添加ws. 对象限定符。

    【讨论】:

      【解决方案2】:

      您可能希望明确声明您的rng 范围的工作表。 (我假设它会在ws 中)。

      试试这个:

      Sub LangID_update()
      
      Dim wb As Workbook: Set wb = ThisWorkbook
      Dim ws As Worksheet
      Dim LastCol As Integer
      Dim LastRow As Integer
      Dim rng As Range
      
      Application.ScreenUpdating = False
      
      For Each ws In wb.Worksheets
      If ws.Name <> "Default" Then
      
      LastCol = ws.Cells(1, Columns.count).End(xlToLeft).Column
      LastRow = ws.Cells(Rows.count, 1).End(xlUp).Row
      Set rng = ws.Range(ws.Cells(2, LastCol + 1), ws.Cells(LastRow, LastCol + 1))
          With rng
              For Each c In rng
                  If ws.Name = "ARGS" Then c.Value = "ESP"
                  If ws.Name = "AUTS" Then c.Value = "GR"
                  If ws.Name = "DEUS" Then c.Value = "GR"
      
              Next c
          End With
          End If
      Next ws
      
      Application.ScreenUpdating = True
      Set wb = Nothing
      Set ws = Nothing
      Set rng = Nothing
      
      End Sub
      

      编辑:我也很确定您不需要with rng,因为您使用for 循环遍历它,并且不一定在With 语句中使用rng.____

      【讨论】:

        【解决方案3】:

        这是因为您在访问范围时没有引用 ws 变量。

        Set rng = Range(ws.Cells(2, LastCol + 1), ws.Cells(LastRow, LastCol + 1))
        For Each c In rng
        

        注意:当您不为范围和单元格添加工作表限定条件时,它们会从 ActiveSheet 中获取。这就是您的代码卡在 ActiveSheet 上的原因。

        【讨论】:

          【解决方案4】:

          我认为您的For Each 行有问题,但如果不了解背后的内容,很难说。

          For Each ws In wb.Worksheets
          If ws.Name <> "Default" Then
          
          LastCol = ws.Cells(1, Columns.count).End(xlToLeft).Column
          LastRow = ws.Cells(Rows.count, 1).End(xlUp).Row
          'next line is useless
          'Set rng = Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1))
              With rng
                  '             qualify Range and Cells
                  For Each c In ws.Range(ws.Cells(2, LastCol + 1), ws.Cells(LastRow, LastCol + 1))
                      If ws.Name = "ARGS" Then c.Value = "ESP"
                      If ws.Name = "AUTS" Then c.Value = "GR"
                      If ws.Name = "DEUS" Then c.Value = "GR"
          
                  Next c
              End With
              End If
          Next
          

          【讨论】:

            【解决方案5】:

            到目前为止的答案已经确定:问题是 for 块中缺少 rng 的资格。尽管如此,快速添加一行将解决问题:

            If ws.Name <> "Default" Then ws.Activate

            激活工作表将使其余代码正常运行,因为您正在/正在考虑活动工作表,并且您刚刚点亮了那个工作表。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-08-28
              • 2022-12-24
              • 2019-04-28
              • 2022-12-08
              • 2014-05-14
              • 2014-12-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多