Philipl
Philipl

Reputation: 415

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). Please check the code,not sure which goes wrong.It seems it never loop through all sheets.

Upvotes: 0

Views: 173

Answers (4)

user3598756
user3598756

Reputation: 29421

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

    since Sheets collects all elements from both Worksheets and Charts collections

    and this latter gathers Chart Objects, that don't have any Cells() property

  • e.Cells(2, 30) = 0 would be written in all worksheets till the first one with matching player

    is this what you actually want?

Upvotes: 0

Jeremy
Jeremy

Reputation: 1337

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

Upvotes: 0

Limak
Limak

Reputation: 1521

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

Upvotes: 2

L.Dutch
L.Dutch

Reputation: 966

Nest your loop into

For each Sheets in ThisWorkbook
....
Next Sheets

Upvotes: 1

Related Questions