If a sheet name equals a cell name?

scottandersonn

New Member
Joined
Oct 1, 2019
Messages
6
Hello everyone

Suppose if I have a list of countries in column A in a "master" sheet, and all the other sheets are named after each country with more details contained within each sheet.

I want to create a macro which will iterate through my list of countries, refer to the sheet of that country, perform a function I already have, then move onto the next country in the list.

Is this a practice possible or very complex?

Happy to provide further examples of code or examples if need be.

Any help is greatly appreciated, Scott.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi & welcome to MrExcel.
Maybe something like
Code:
Sub scottandersonn()
   Dim Cl As Range
   Dim Ws As Worksheet
   
   Set Ws = Sheets("Master")
   For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
      If Evaluate("isref('" & Cl.Value & "''!A1)") Then
         With Sheets(Cl.Value)
            'do somthing
         End With
      End If
   Next Cl
End Sub
 
Upvote 0
this is great thanks fluff!

So now I can replace 'do something with my function (which is a for loop and an if statement), but will the Sheets(Cl.Value) be selected already or do I need to add Sheets(Cl.Value).Select


 
Upvote 0
so am I still able to nest my for loop and if statement within the with statement, and the function will work fine. I require offsetted copy and paste steps to be performed for each sheet which the function will do, I just want to make sure I can interchange back to the master sheet within the with statement if you understand. Apologies if its poorly explained
 
Upvote 0
As long as you precede the ranges with a . as shown in the link I supplied then yes.
This will copy range A1:X1 from each sheet to the master sheet on the same row as the sheet name
Code:
         With Sheets(Cl.Value)
            .Range("A1:X1").Copy Cl.Offset(, 1)
         End With
 
Upvote 0
ok thats great.

now my if statement will look to satisfy two conditions. if it is satisfied in column A of the subset, then it looks to column B on the same row to see if thats also satisfied. If both are satisfied, then "Yes" will be inputted next to the country name in the master sheet, however if only the first condition was satisfied, then "No" will be inputted next to the country name in the master sheet.

So I guess my question is, is it possible to have an if statement in a with statement?
 
Upvote 0
just one more quick question regarding the initial code you provided.. what is the purpose of this line?

For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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