WITH statement with ARRAY?

hellfire45

Active Member
Joined
Jun 7, 2014
Messages
464
Hi guys,
If I have something like the below, that will loop through some rows on a spreadsheet. Say, I want to do this same type of operation on 4 or 5 spreadsheets. Rather than copy and paste this 5 times and just changing the WITH statement, can I create like a looping array that will let me only have the code once but then it loops and changes what is in the WITH?

Btw, for the With, I am using the "left worksheet name" in the VBA editor. Dunno what to call that but when you chance the name of the worksheet under properties. Does this make sense? Thanks so much!

VBA Code:
With Air
            user_id = Application.Match("Reserved by ID", Air.Rows("1:1"), 0)
            department = Application.Match("Department", Air.Rows("1:1"), 0)
            tier2_man = Application.Match("Tier 2 Manager", Air.Rows("1:1"), 0)
            lastrow = POD.Columns(POD_PO).Find("*", , , , xlRows, xlPrevious).Row
            For x_row = 2 To lastrow
                        current_user = .Cells(x_row, user_id).Value
                        If IsError(Application.Index(map_index_range, Application.Match(current_user, map_match_range, 0), map_tier2_man)) Then
                                    .Cells(x_row, tier2_man) = ""
                        Else
                                    .Cells(x_row, tier2_man) = Application.Index(map_index_range, Application.Match(current_user, map_match_range, 0), map_tier2_man)
                        End If
                        
                        If IsError(Application.Index(map_index_range, Application.Match(current_user, map_match_range, 0), map_department)) Then
                                    .Cells(x_row, department) = ""
                        Else
                                    .Cells(x_row, department) = Application.Index(map_index_range, Application.Match(current_user, map_match_range, 0), map_department)
                        End If
            Next
End With
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You can do something like this:

VBA Code:
arr = Array("Sheet1", "Sheet2")

For i = LBound(arr) To UBound(arr)
    With Sheets(arr(i))
        'other code
    End With
Next
 
Upvote 0

Forum statistics

Threads
1,226,730
Messages
6,192,715
Members
453,749
Latest member
learningexcelthankyou

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