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

GSanchez

New Member
Joined
Feb 23, 2021
Messages
10
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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
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
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,413
Messages
6,119,372
Members
448,888
Latest member
Arle8907

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
Back
Top