(VBA) - My Loop Code is repeating itself after going through first two sheets

GSanchez

New Member
Joined
Feb 23, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hello Everyone!

The code below is attempting to go through every sheet we have in this workbook and copy any column that matches the "FindString" piece.

This code works perfectly when all of the sheets matches what the FindString piece is, but I noticed that if a worksheet does not match with "FindString" then it would go back and loop to the previous sheets. It wouldn't go to the Next available WS for a while. I cannot seem to find the reason why it is doing that.

Any help is appreciated!

VBA Code:
Sub TakeInfoFromMonthsTest()
 Dim FindString As String
    Dim Rng As Range
    Dim WS As Worksheet
    Dim SizeFit As Long
    
    FindString = InputBox("Enter a Search value")
    SizeFit = InputBox("How many rows would you like to copy")
    
    For Each WS In ThisWorkbook.Sheets
            If WS.Name <> "Holder" And WS.Name <> "Macros" Then
            With WS.Range("A:GY")

            Set Rng = .Find(What:=FindString, _
                            After:=.Range("N32"), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                Sheets("Holder").Range("A1").End(xlToRight).Offset(0, 1) = "Nothing"
            End If
            End With
        
        Selection.Resize(SizeFit).Copy
            Sheets("Holder").Range("A1").End(xlToRight).Offset(0, 1).PasteSpecial Paste:=xlPasteValues
        
        End If
        Next WS
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,969
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
How about
VBA Code:
Sub TakeInfoFromMonthsTest()
Dim FindString As String
    Dim Rng As Range
    Dim WS As Worksheet
    Dim SizeFit As Long
   
    FindString = InputBox("Enter a Search value")
   
    SizeFit = InputBox("How many rows would you like to copy")
   
    For Each WS In ThisWorkbook.Sheets
            If WS.name <> "Holder" And WS.name <> "Macros" Then
            With WS.Range("A:GY")

            Set Rng = .Find(What:=FindString, _
                            After:=.Range("N32"), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
               Sheets("Holder").Range("A1").End(xlToRight).Offset(0, 1).Resize(SizeFit).Value Rng.Resize(SizeFit).Value
            Else
                Sheets("Holder").Range("A1").End(xlToRight).Offset(0, 1) = "Nothing"
            End If
            End With
       
        End If
        Next WS
End Sub
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,969
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,315
Messages
5,641,482
Members
417,211
Latest member
loadius

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top