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.
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,453
Office Version
365
Platform
Windows
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
 

scottandersonn

New Member
Joined
Oct 1, 2019
Messages
6
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


 

scottandersonn

New Member
Joined
Oct 1, 2019
Messages
6
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,453
Office Version
365
Platform
Windows
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
 

scottandersonn

New Member
Joined
Oct 1, 2019
Messages
6
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
38,453
Office Version
365
Platform
Windows
Yes it is. :)
 

scottandersonn

New Member
Joined
Oct 1, 2019
Messages
6
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))
 

Watch MrExcel Video

Forum statistics

Threads
1,095,196
Messages
5,442,972
Members
405,210
Latest member
clmoi90

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top