简体   繁体   English

使用VBA遍历每个Excel工作表

[英]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收集WorksheetsCharts集合中的所有元素

    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.

相关问题 Excel VBA:For Loop,用于遍历图纸范围 - Excel VBA: For Loop for iteration through Sheet range 如何遍历每个Excel工作表名称 - how to loop through each excel sheet name 使用Excel表和VBA在动态列的每一行中循环 - Loop through each row in dynamic column using Excel Tables and VBA Excel VBA-遍历工作簿并用每个工作表的名称标记单元格 - Excel VBA - Loop through workbook and label a cell with each sheet's name 在一张表中循环遍历范围,并为每个单元格设置自定义公式。 将数据放入新工作表 [excel][vba][bloomberg] - Loop through range in one sheet and have custom formula for each cell. Put data into a new sheet [excel][vba][bloomberg] Excel VBA查找每个工作表的最后一行 - Excel VBA lookup each through sheet last row 如何使用vba循环浏览已过滤的表格并检查每一行是否在另一张纸中? - How can I loop through a filtered table and check if each row is in another sheet using vba? 循环浏览excel中的每个选项卡,并通过VBA在Excel中逐个激活它 - Loop through each tabs in excel and active it one by one in Excel by VBA 循环通过 excel 工作表并根据条件将每个工作表保存到 csv - Loop through excel sheets and save each sheet into a csv based on a condition 在Excel VBA中使用&#39;For Each&#39;循环和&#39;If&#39;语句时出错 - Error in using 'For Each' loop and 'If' statement in excel vba
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM