【问题标题】:VBA Looping Through Multiple WorksheetsVBA循环遍历多个工作表
【发布时间】:2017-11-26 20:37:56
【问题描述】:

我正在构建代码,该代码可以遍历多个工作表上的列 (B5:B) 以查找匹配值。如果一个工作表的列 (B5:B) 上的值等于工作表名称,则工作表名称将放置在找到该值的相邻列 (C5:C) 上。我不是程序员,但我一直在学习 VBA 来实现这一点。到目前为止,我尝试使用 For Next 循环(从第三张表开始)、Thisworkbook.sheets 中的 For Each ws 方法均未成功。但我似乎无法让它发挥作用。我在互联网上搜索了类似的东西,但没有骰子。任何建议将不胜感激。

Sub MatchingPeople()
    Dim c As Variant
    Dim lastrow As Long
    Dim i As Variant
    Dim g As Long
    Dim w As Long

    i = Sheets("Anthony").Name
    g = Sheets("Anthony").Cells(Rows.Count, "C").End(xlUp).Row

    For w = 3 To Sheets.Count
        lastrow = Sheets(w).Cells(Rows.Count, 2).End(xlUp).Row
        Set NewRang = Sheets("Anthony").Cells(g + 1, 3)
        On Error Resume Next
        With Sheets(w).Range(Cells(5, 2), Cells(lasty, 2))
            Set c = .Find(i, LookIn:=xlValues)
            If Not c Is Nothing Then
                firstaddress = c.Address
                Do
                    NewRang.Value = Sheets(w).Name
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> firstaddress
            End If
        End With
    Next w
End Sub

【问题讨论】:

  • 嗨 Max,我希望你不介意我缩进了你的代码。将来您应该考虑以这种方式缩进,因为它使您(和我们!)更容易阅读您的代码并快速发现一些简单的错误。
  • 这里有一些事情要开始:我看到了很多尚未定义的变量。 NewRangfirstaddresslasty 在您在这里使用之前未声明或赋予值:Cells(lasty,2)
  • 注释掉 On Error Resume Next 并单步执行代码可能会发现一些东西。
  • 不,我绝对不介意您缩进代码,在您发表评论后我已经开始这样做了。感谢您的建议。

标签: vba excel


【解决方案1】:

Sub Bygone()

暗×长

变暗

将 z 变暗

变暗

变暗

将 b 变暗

将 c 变暗

变暗

a = Sheets.Count

For m = 3 To a

   x = Sheets(m).Cells(3, 3).Value

       For b = 3 To a

            w = Sheets(b).Cells(Rows.Count, 1).End(xlUp).row

                For z = 5 To w

                    y = Sheets(b).Cells(z, 1)

                        Select Case x

                            Case y
                              c =Sheets(m).Cells(Rows.Count,3).End(xlUp).Offset(1, 0).row
                              Sheets(m).Cells(c, 3).Value = Sheets(b).Name
                         End Select
                Next z

      Next b
Next m

结束子

【讨论】:

    【解决方案2】:

    这里有 2 个版本,一个使用像代码中一样的 Find 方法,另一个使用 For 循环

    Option Explicit
    
    Public Sub MatchingPeopleFind()
        Dim i As Long, lrColB As Long
        Dim wsCount As Long, wsName As String
        Dim found As Variant, foundAdr As String
    
        wsCount = Worksheets.Count
        If wsCount >= 3 Then
            For i = 3 To wsCount
                With Worksheets(i)
                    wsName = .Name
                    lrColB = .Cells(.Rows.Count, 2).End(xlUp).Row
                    With .Range(.Cells(5, 2), .Cells(lrColB, 2))
                        Set found = .Find(wsName, LookIn:=xlValues)
                        If Not found Is Nothing Then
                            foundAdr = found.Address
                            Do
                                found.Offset(0, 1).Value2 = wsName
                                Set found = .FindNext(found)
                            Loop While Not found Is Nothing And found.Address <> foundAdr
                        End If
                    End With
                End With
            Next
        End If
    End Sub
    

    Public Sub MatchingPeopleForLoop()
        Dim wsCount As Long, wsName As String, i As Long, j As Long
    
        wsCount = Worksheets.Count
        If wsCount >= 3 Then
            For i = 3 To wsCount
                With Worksheets(i)
                    wsName = .Name
                    For j = 5 To .Cells(.Rows.Count, 2).End(xlUp).Row
                        If .Cells(j, 2).Value2 = wsName Then .Cells(j, 3).Value2 = wsName
                    Next
                End With
            Next
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2014-11-15
      • 2018-01-05
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多