Next empty row and Looping

gasper21

New Member
Joined
Jan 30, 2019
Messages
20
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi, I hope someone can help me with this code. I’m trying to make it paste the information to summary sheet and then copy the same information from other worksheet. There are 15 worksheets in one workbook. Here is my code. Thank you in advance for your help.

Code:
Sub SearchForString()

Dim wsSource As Worksheet'Active worksheet
Dim a As Long, arr As Variant, fnd As Range, cpy As Range, addr As String

On Error GoTo Err_Execute

'Populate the array for the outer loop search for values
arr = Array("Total Revenue", "Net Revenue to the WI Owners", "Total WI Expenses", "Average $ per BBL")

Set wsSource = ActiveSheet'Active worksheet

'outer loop through the array
For a = LBound(arr) To UBound(arr)
'locate first instance
Set fnd = Columns("B").Find(what:=arr(a), LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not fnd Is Nothing Then
'record address of first find
addr = fnd.Address
'seed the cpy range object
If cpy Is Nothing Then Set cpy = fnd.EntireRow
Do
'build union
Set cpy = Union(cpy, fnd.EntireRow)

'look for another
Set fnd = Columns("B").FindNext(after:=fnd)

'keep finding new matches until it loops back to the first
Loop Until fnd.Address = addr
End If
Next a

With Worksheets("Summary")
'one stop copy & paste operation
cpy.Copy Destination:=.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

Exit Sub

Err_Execute:
Debug.Print Now & " " & Err.Number & " - " & Err.Description

End Sub
 
Last edited by a moderator:
Re: Next empty row and Looping through multiple worksheets

Found another error after devising a couple of sheets that let me test the code. This is lightly tested, but see if it works for you.
Code:
Sub SearchForString()
Dim wsSource As Worksheet
Dim a As Long, arr As Variant, fnd As Range, cpy As Range, addr As String
On Error GoTo Err_Execute
'Populate the array for the outer loop search for values
arr = Array("Total Revenue", "Net Revenue to the WI Owners", "Total WI Expenses", "Average $ per BBL")
For Each wsSource In ThisWorkbook.Worksheets
    If wsSource.Name <> "Summary" Then
        'outer loop through the array
        For a = LBound(arr) To UBound(arr)
            'locate first instance
            Set fnd = wsSource.Columns("B").Find(what:=arr(a), LookIn:=xlFormulas, LookAt:=xlPart, _
            SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)
            If Not fnd Is Nothing Then
                'record address of first find
                addr = fnd.Address
                'seed the cpy range object
                If cpy Is Nothing Then Set cpy = fnd.EntireRow
                Do
                    'build union
                    Set cpy = Union(cpy, fnd.EntireRow)
                    
                    'look for another
                    Set fnd = wsSource.Columns("B").FindNext(after:=fnd)
                    
                    'keep finding new matches until it loops back to the first
                Loop Until fnd.Address = addr
            End If
        Next a
        With Worksheets("Summary")
            If Not cpy Is Nothing Then
            'one stop copy & paste operation
                cpy.Copy Destination:=.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).EntireRow
            End If
        End With
    End If
    Set cpy = Nothing
Next wsSource
Exit Sub
Err_Execute:
Debug.Print Now & " " & Err.Number & " - " & Err.Description
End Sub
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Re: Next empty row and Looping through multiple worksheets

After lots of research I was able to find a solution for my macro.. :)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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