Macro to list sheet names that match a criterion

Jmoz092

Board Regular
Joined
Sep 8, 2017
Messages
184
Office Version
  1. 365
  2. 2011
Platform
  1. Windows
  2. MacOS
After searching for examples, I found a code that I'm trying to manipulate to fit my needs here: example

I'm trying to make a list (as column headers) of all the worksheets in a summary workbook for all of the worksheets that have a MM-DD-YY format.

I can get the code to work, and list the names of all the worksheets, but I can't make it not display the data and reference sheets contained in the workbook.

this works, but lists the data sheets in the header row also:
Code:
Sub SheetNamesAsColumns()
 
Dim ws As Worksheet
Dim x As Integer
 x = 1
For Each ws In Worksheets
     Sheets("Index").Cells(1, x) = ws.Name
     x = x + 1
Next ws
End Sub

This one does not work, as I try to sort out the non MM-DD-YY worksheets:
Code:
Sub SheetNamesAsColumns()
 
Dim ws As Worksheet
Dim x As Integer


x = 1
For Each ws In Worksheets
If ws.Name Like "**-**-**" Then
     Sheets("Index").Cells(1, x) = ws.Name
     x = x + 1
Next ws
End If
 
End Sub

What mistakes am I making the 2nd code, and how can I correct them?
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
The End If needs to come become before the Next ws line.
 
Upvote 0
Thank you both, always something simple. It's working as intended now.

Code:
Sub SheetNamesAsColumns()
 
Dim ws As Worksheet
Dim x As Integer


x = 2
For Each ws In Worksheets
If ws.Name Like "**-**-**" Then
     Sheets("Index").Cells(1, x) = ws.Name
     x = x + 1
End If
Next ws


End Sub
 
Upvote 0
You are welcome.

Just remember that in closing block of code (like IF ... THEN, FOR ... NEXT, WITH ... END WITH), you close them out in the reverse order you opened them.
So the last one started is the first one to close.
 
Upvote 0
You are welcome.

Just remember that in closing block of code (like IF ... THEN, FOR ... NEXT, WITH ... END WITH), you close them out in the reverse order you opened them.
So the last one started is the first one to close.

Thanks, I was thinking completely the opposite. Some instinct I have?? LOL
 
Upvote 0
Something I used to do, when I was starting out, was to close the block as soon as I created it.
So you'd get this
Code:
Sub SheetNamesAsColumns()
 
   Dim ws As Worksheet
   Dim x As Integer
   
   x = 1
   For Each ws In Worksheets
   Next ws
    
End Sub
Followed by
Code:
Sub SheetNamesAsColumns()
 
   Dim ws As Worksheet
   Dim x As Integer
   
   x = 1
   For Each ws In Worksheets
      If ws.Name Like "**-**-**" Then
      End If
   Next ws
    
End Sub
Etc
 
Upvote 0
just wait until you do multiple loops and endifs :rofl:

Code:
for x = 1 to 100
if x = 3 then
'do something
endif
for y = 2 to 200
if y = 150 then
'do something
endif
next y
next x
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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