[英]Loop through each excel sheet using vba
Sub CommandButton1_Click()
c = 0
For Each e In Sheets
e.Cells(2, 30) = 0
If e.Cells(2, 15) = Sheet10.Cells(2, 4) Then
Sheet10.Cells(1, 1) = e.Cells(2, 15)
c = 1
Exit For
End If
Next e
If c = 0 Then
Sheet10.Cells(1, 1) = "No such player"
End If
End Sub
I am currently building a button which search the value in Sheet10.Cells(2, 4) through each sheet.When it found the value equal to itself it return the value in the Sheet10.Cells(1, 1).If the value is not found,then return the 'No such player' to the Sheet10.Cells(1, 1). 我当前正在构建一个按钮,该按钮在每个工作表中搜索Sheet10.Cells(2,4)中的值。当发现等于其自身的值时,将返回Sheet10.Cells(1,1)中的值。找不到,然后将“否此类播放器”返回到Sheet10.Cells(1,1)。 Please check the code,not sure which goes wrong.It seems it never loop through all sheets.
请检查代码,不确定出了什么问题。似乎它从未遍历所有工作表。
Try this one: 试试这个:
Sub CommandButton1_Click()
Dim e As Worksheet
c = 0
For Each e In ActiveWorkbook.Worksheets
e.Cells(2, 30) = 0
If e.Cells(2, 15) = Sheet10.Cells(2, 4) Then
Sheet10.Cells(1, 1) = e.Cells(2, 15)
c = 1
Exit For
End If
Next
If c = 0 Then
Sheet10.Cells(1, 1) = "No such player"
End If
End Sub
Nest your loop into 将循环嵌套
For each Sheets in ThisWorkbook
....
Next Sheets
This will do it for you: 这将为您做到:
Sub CommandButton1_Click()
Dim sh As Worksheet, sPlayer As String, rng As Range
sPlayer = Sheets("Sheet10").Cells(2, 4).Value 'Turns the player into a string
For Each sh In ActiveWorkbook.Sheets
Set rng = Nothing 'resets the range
Set rng = sh.Cells.Find(What:=sPlayer, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False) ' This check is to see if the "find" found the player in the sheet (looking for exact matches and only value - not formulas)
If Not rng Is Nothing And sh.Name <> "Sheet10" Then 'If it did find the player (and it's not on "Sheet10", where the input value is
Sheets("Sheet10").Cells(1, 1).Value = sPlayer ' puts in the player in A1
Exit Sub ' Exits sub (as it's found what it's looking for
ElseIf sh.Index = ActiveWorkbook.Sheets.Count Then Sheets("Sheet10").Cells(1, 1).Value = "No such player" ' End of loop and workbook says it can't find them
End If
Next
End Sub
your code could be refactored as follows: 您的代码可以如下重构 :
Sub CommandButton1_Click()
For Each e In Worksheets '<--| loop through 'Worksheets' only
e.Cells(2, 30) = 0 '<--?
If e.Cells(2, 15) = Sheet10.Cells(2, 4) Then
Sheet10.Cells(1, 1) = e.Cells(2, 15)
Exit Sub
End If
Next e
Sheet10.Cells(1, 1) = "No such player" '<--| this line would only be reached if 'For Each loop' has been completed, i.e. without exiting sub at first matching player
End Sub
just be aware that: 请注意:
you want to loop through Worksheets
collection, not Sheets
one 您要遍历
Worksheets
集合而不是工作Sheets
一个
since Sheets
collects all elements from both Worksheets
and Charts
collections 因为工作
Sheets
收集Worksheets
和Charts
集合中的所有元素
and this latter gathers Chart Object
s , that don't have any Cells()
property 后者收集没有任何
Cells()
属性的Chart Object
s
e.Cells(2, 30) = 0
would be written in all worksheets till the first one with matching player e.Cells(2, 30) = 0
将被写入所有工作表,直到第一个具有匹配播放器的工作表
is this what you actually want? 这是您真正想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.