Listing tab names after a certain tab

ardykav

Board Regular
Joined
Oct 18, 2015
Messages
172
Office Version
  1. 365
Platform
  1. Windows
Hi, I have the below code which works well. I have 18 tabs in total and they change all the time apart from 2 or 3. However I now only want the list to start after the "template" tab which is the 4th tab. Can anyone tell me what I need to add to the below for this to work?


VBA Code:
Sub Listalltabs()
 
Dim ws As Worksheet
Dim x As Integer
 
x = 1
 
Sheets("Tab list").Range("A:A").Clear
 
For Each ws In Worksheets
     Sheets("Tab list").Cells(x, 1) = ws.Name
     x = x + 1
Next ws
 
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
VBA Code:
Sub List_Sheet_Names()
Dim x As Long, i As Long
Sheets("Tab list").Range("A:A").Clear
x = 1
'For i = 5 To ThisWorkbook.Sheets.Count
' Or
 For i = Sheets("template").Index + 1 To ThisWorkbook.Sheets.Count    '<---- template sheet can be anywhere
        Sheets("Tab list").Cells(x, 1) = Sheets(i).Name
     x = x + 1
Next i
End Sub
 
Upvote 0
Thank you for the feedback.
Good Luck
The above has worked superbly over the last few months, was just wondering if I wanted to extend this now to be after the template (have changed this tab name to simply "A") and before another tab (Tab named "Z") what would I need to put into the code?

VBA Code:
Sub List_Sheet_Names()
Dim x As Long, i As Long
Sheets("list").Range("J:J").Clear
x = 1
'For i = 5 To ThisWorkbook.Sheets.Count
' Or
 For i = Sheets("A").Index + 1 To ThisWorkbook.Sheets.Count    '<---- template sheet can be anywhere
        Sheets("list").Cells(x, 10) = Sheets(i).Name
     x = x + 1
Next i
End Sub
 
Upvote 0
Try this
Code:
For i = Sheets("template").Index + 1 To Sheets("Z").Index - 1    '<---- template sheet can be anywhere
If there is a chance of Sheet Z being next to Sheet template, you should have a message alerting you.
Maybe something like
Code:
 If Sheets("Z").Index = Sheets("template").Index + 1 Then MsgBox "No sheets between template and Z!":Exit Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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