VBA To Create a Index Sheet of all VISIBLE sheets AND Populate with values from those sheets

bhalbach

Board Regular
Joined
Mar 15, 2018
Messages
221
Office Version
  1. 2016
Platform
  1. Windows
I am trying to create an index sheet of all Visible sheets in a workbook, and also copy specific data from the sheets to the index sheet with VBA.

I have found some code that works great....Except it creates the Index sheet with ALL sheets in the workbook. I only want the sheets that are not hidden.

Also, while it is creating the Index sheet I would like it to copy data from each sheet to the Index sheet.

For Example if I have 20 visible sheets I would end up with a list of the sheet names on an index page...from these 20 sheets I would like to copy the data from cell's C5, I5 & J5 to the same row as the sheet name on the Index page.

The code I have now to make the index page is below, but it only copies ALL sheets to a Index sheet.

Please help...

Private Sub Worksheet_Activate()
'Updateby20150305
Dim xSheet As Worksheet
Dim xRow As Integer
Dim calcState As Long
Dim scrUpdateState As Long
Application.ScreenUpdating = False
xRow = 1
With Me
.Columns(1).ClearContents
.Cells(1, 1) = "INDEX"
.Cells(1, 1).Name = "Index"
End With
For Each xSheet In Application.Worksheets
If xSheet.Name <> Me.Name Then
xRow = xRow + 1
With xSheet
.Range("A1").Name = "Start_" & xSheet.Index
.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
SubAddress:="Index", TextToDisplay:="Back to Index"
End With
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
just add this if statement:

VBA Code:
Private Sub Worksheet_Activate()
'Updateby20150305
Dim xSheet As Worksheet
Dim xRow As Integer
Dim calcState As Long
Dim scrUpdateState As Long
Application.ScreenUpdating = False
xRow = 1
With Me
.Columns(1).ClearContents
.Cells(1, 1) = "INDEX"
.Cells(1, 1).Name = "Index"
End With
For Each xSheet In Application.Worksheets
If xSheet.Name <> Me.Name Then
If xSheet.Visible Then          ''''''''''''''addthis line
xRow = xRow + 1
With xSheet
.Range("A1").Name = "Start_" & xSheet.Index
.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
SubAddress:="Index", TextToDisplay:="Back to Index"
End With
End If
End If                  ' and this line
Next

End Sub
 
Upvote 0
just add this if statement:

VBA Code:
Private Sub Worksheet_Activate()
'Updateby20150305
Dim xSheet As Worksheet
Dim xRow As Integer
Dim calcState As Long
Dim scrUpdateState As Long
Application.ScreenUpdating = False
xRow = 1
With Me
.Columns(1).ClearContents
.Cells(1, 1) = "INDEX"
.Cells(1, 1).Name = "Index"
End With
For Each xSheet In Application.Worksheets
If xSheet.Name <> Me.Name Then
If xSheet.Visible Then          ''''''''''''''addthis line
xRow = xRow + 1
With xSheet
.Range("A1").Name = "Start_" & xSheet.Index
.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
SubAddress:="Index", TextToDisplay:="Back to Index"
End With
End If
End If                  ' and this line
Next

End Sub


How would I then copy data from each visible sheet and past it to the index sheet as well at the same time as creating the Index sheet?

For Example if I have 20 visible sheets I would end up with a list of the sheet names on an index page (column A)...from these 20 sheets I would like to copy the data from cell's C5, I5 & J5 to the same row as the sheet name on the Index page. So C5 on sheet 1 would go to column B on the Index sheet, I5 would copy to column C on the Index, and J5 would copy to column D on the index sheet.
 
Upvote 0
try this:
VBA Code:
Private Sub Worksheet_Activate()
'Updateby20150305
Dim xSheet As Worksheet
Dim xRow As Integer
Dim calcState As Long
Dim scrUpdateState As Long
Application.ScreenUpdating = False
Dim outarr() As Variant
nsht = Worksheets.Count
ReDim outarr(1 To nsht, 1 To 4)
xRow = 1
With Me
.Columns(1).ClearContents
.Cells(1, 1) = "INDEX"
.Cells(1, 1).Name = "Index"
End With
For Each xSheet In Application.Worksheets
If xSheet.Name <> Me.Name Then
If xSheet.Visible Then          ''''''''''''''addthis line
outarr(xRow, 1) = xSheet.Name
outarr(xRow, 2) = xSheet.Range("C5")
outarr(xRow, 3) = xSheet.Range("I5")
outarr(xRow, 4) = xSheet.Range("j5")
xRow = xRow + 1

With xSheet
.Range("A1").Name = "Start_" & xSheet.Index
.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
SubAddress:="Index", TextToDisplay:="Back to Index"
End With
End If
End If                  ' and this line
Next
With Me
 .Range(Cells(2, 1), Cells(nsht + 1, 4)) = outarr
End With

End Sub
 
Upvote 0
try this:
VBA Code:
Private Sub Worksheet_Activate()
'Updateby20150305
Dim xSheet As Worksheet
Dim xRow As Integer
Dim calcState As Long
Dim scrUpdateState As Long
Application.ScreenUpdating = False
Dim outarr() As Variant
nsht = Worksheets.Count
ReDim outarr(1 To nsht, 1 To 4)
xRow = 1
With Me
.Columns(1).ClearContents
.Cells(1, 1) = "INDEX"
.Cells(1, 1).Name = "Index"
End With
For Each xSheet In Application.Worksheets
If xSheet.Name <> Me.Name Then
If xSheet.Visible Then          ''''''''''''''addthis line
outarr(xRow, 1) = xSheet.Name
outarr(xRow, 2) = xSheet.Range("C5")
outarr(xRow, 3) = xSheet.Range("I5")
outarr(xRow, 4) = xSheet.Range("j5")
xRow = xRow + 1

With xSheet
.Range("A1").Name = "Start_" & xSheet.Index
.Hyperlinks.Add anchor:=.Range("A1"), Address:="", _
SubAddress:="Index", TextToDisplay:="Back to Index"
End With
End If
End If                  ' and this line
Next
With Me
.Range(Cells(2, 1), Cells(nsht + 1, 4)) = outarr
End With

End Sub
Will not work....It doesnt like "nsht = Worksheets.Count
 
Upvote 0
Do you have option explicit at the top, if so try it without, or just declare everything that fails as type variant
 
Upvote 0
Do you have option explicit at the top, if so try it without, or just declare everything that fails as type variant
This works great.

How would I change this code to do the same thing, except start the paste of the data on the Index sheet starting on cell D5?
 
Upvote 0
change this line:
VBA Code:
.Range(Cells(2, 1), Cells(nsht + 1, 4)) = outarr
to
VBA Code:
.Range(Cells(5, 4), Cells(nsht + 4, 7)) = outarr
 
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,229
Members
448,879
Latest member
VanGirl

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