VBA looping through tabs, columns, and keyword searches

nicoletta99

New Member
Joined
Oct 31, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello all! I'm hoping this is a simple one for you all… fingers crossed!

I have a spreadsheet with heaps of tabs and have two macro requirements.
1) If I list 10 tabs in cell A2-A11 of the "Collation" tab, I need a macro that will loop through each of the 10 tabs listed and copy cells A4:C40 and paste it into the "Collation" tab below each other (starting in cell A21).
2) If I have a keyword in cell B1 of the "Search" tab, I need a macro to loop through column A of every tab in the file (until an empty cell - not whole column). If it finds the keyword in any of those tabs, I need it to list what tab it found the true value (could be more than 1) in a list on the Collation tab starting in cell A4.
I'm still learning ranges and strings, and as much as I try to google examples and put it together I'm getting so many errors I don't know how to fix!
I'm hoping these examples will be the basis for many awesome spreadsheets to come - so thanks for your help :)
 

Some videos you may like

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

hnsd24_CN

Board Regular
Joined
Oct 13, 2020
Messages
78
Office Version
  1. 2016
Platform
  1. Windows
no test
VBA Code:
Sub test1()
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim i As Integer
    Dim r As Integer
    Set ws1 = Worksheets("Collation")
    For i = 2 To 14
        Set ws = Worksheets(ws1.Range("A" & i))
        ws.Range("A4:C40").Copy ws1.Range("A" & 21 + r)
        r = r + 37
    Next
End Sub
put result on the Collation tab starting in cell B4
VBA Code:
Sub test2()
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim i As Integer
    Dim s As Integer
    Dim arr(1 To 10000)
    Set ws1 = Worksheets("Collation")
    Set ws2 = Worksheets("Search")
    s = 1
    For Each ws In Worksheets
        If ws.Name <> "Collation" Or ws.Name <> "Search" Then
            i = 1
            Do Until ws.Range("A" & i) = ""
                If ws.Range("A" & i) = ws2.Range("B1") Then
                    arr(s) = ws.Name
                    s = s + 1
                    Exit Do
                End If
                i = i + 1
            Loop
        End If
    Next
    ws1.Range("B4:B" & s + 2) = Application.Transpose(arr)
End Sub
 

nicoletta99

New Member
Joined
Oct 31, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
no test
VBA Code:
Sub test1()
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim i As Integer
    Dim r As Integer
    Set ws1 = Worksheets("Collation")
    For i = 2 To 14
        Set ws = Worksheets(ws1.Range("A" & i))
        ws.Range("A4:C40").Copy ws1.Range("A" & 21 + r)
        r = r + 37
    Next
End Sub
put result on the Collation tab starting in cell B4
VBA Code:
Sub test2()
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim i As Integer
    Dim s As Integer
    Dim arr(1 To 10000)
    Set ws1 = Worksheets("Collation")
    Set ws2 = Worksheets("Search")
    s = 1
    For Each ws In Worksheets
        If ws.Name <> "Collation" Or ws.Name <> "Search" Then
            i = 1
            Do Until ws.Range("A" & i) = ""
                If ws.Range("A" & i) = ws2.Range("B1") Then
                    arr(s) = ws.Name
                    s = s + 1
                    Exit Do
                End If
                i = i + 1
            Loop
        End If
    Next
    ws1.Range("B4:B" & s + 2) = Application.Transpose(arr)
End Sub

Thank you so much for your reply!

I've finally gotten around to having another go - but am finding that I'm getting a type mismatch?
1613892616628.png
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
914
Office Version
  1. 2013
Platform
  1. Windows
Replace with
VBA Code:
 Set ws = Worksheets(ws1.Range("A" & i).Value)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,780
Messages
5,598,032
Members
414,205
Latest member
Tushark

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