VBA在多个工作表中循环
我正在研究建筑代码,它可以循环通过多个工作表上的一列(B5:B)来查找匹配的值。如果一个工作表列(B5:B)上的值等于工作表名称,则工作表名称将放置在找到该值的相邻列(C5:C)上。我不是程序员,但我一直在学习VBA来实现这一点。到目前为止,我尝试使用For Next循环(从第三张工作表开始),即For Thisworkbook.sheets方法,但没有成功。但我似乎无法让它工作。我在互联网上搜索过类似的东西,但没有找到。如有任何建议,将不胜感激VBA在多个工作表中循环,vba,excel,Vba,Excel,我正在研究建筑代码,它可以循环通过多个工作表上的一列(B5:B)来查找匹配的值。如果一个工作表列(B5:B)上的值等于工作表名称,则工作表名称将放置在找到该值的相邻列(C5:C)上。我不是程序员,但我一直在学习VBA来实现这一点。到目前为止,我尝试使用For Next循环(从第三张工作表开始),即For Thisworkbook.sheets方法,但没有成功。但我似乎无法让它工作。我在互联网上搜索过类似的东西,但没有找到。如有任何建议,将不胜感激 Sub MatchingPeople()
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
子匹配人员()
dimc作为变体
最后一排一样长
Dim i作为变体
暗g一样长
暗w长
i=图纸(“安东尼”)。名称
g=纸张(“Anthony”).单元格(行数,“C”).结束(xlUp).行
对于w=3的板材。计数
lastrow=工作表(w).单元格(Rows.Count,2).结束(xlUp).行
设置新范围=表(“Anthony”)。单元格(g+1,3)
出错时继续下一步
带床单(w)。范围(单元格(5,2),单元格(lasty,2))
Set c=.Find(i,LookIn:=xlValues)
如果不是,那么c什么都不是
firstaddress=c.地址
做
NewRang.Value=Sheets(w).Name
集合c=.FindNext(c)
循环而不是c为Nothing,c.Address为firstaddress
如果结束
以
下一个w
端接头
这里有两个版本,一个使用代码中的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
过去的事
暗x等长
长得一样暗
暗z一样长
暗w长
暗淡如长
暗b一样长
尺寸c与长度相同
我和你一样长
a=床单数
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
End Sub嗨,Max,我希望你不介意我缩进了你的代码。您应该在将来以这种方式缩进,因为它使您(和我们!)更容易阅读代码并快速发现一些简单错误
NewRang
,firstaddress
以及lasty
在这里使用之前没有声明或给定值:单元格(lasty,2)
注释出错误恢复下一步
并单步执行代码可能会发现一些东西。不,我绝对不介意你缩进代码,在你发表评论后,我开始做这件事。谢谢你的建议。
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