ashu2087
ashu2087

Reputation: 23

Excel Index / Table of Contents - Macro

I was reading up some codes that used a macro to auto-create a table of contents. I have a workbook with multiple sheets and new sheets are being added everyday. Each sheet name is a unique number and has an identical template inside.

The codes I saw would only give the option of collating an index with the Sheet Name.

I was wondering if I could get an option that would also pick up one or two values from each sheet, like this:

Sr No. Sheet Name Cell A1 Cell A2 Cell A3

I need the Macro to run every time the Index sheet is opened and the Sheet Name column to be hyper-linked to the worksheet.

Any ideas?

Thanks in advance for any help!

Upvotes: 2

Views: 4344

Answers (2)

jacouh
jacouh

Reputation: 8741

This code is based on http://www.ozgrid.com/VBA/sheet-index.htm since years.

In the modified code, Column 1 will contain index of sheets, Column 2 will show extracts of cells A2 and A3 for all WorkSheets. Be careful, the old contents of Columns 1, 2 and 3 of the index sheet will be completely erased!!!

Private Sub Worksheet_Activate()

  Dim wSheet As Worksheet

  Dim i As Long

  i = 1
  With Me
    .Columns(1).ClearContents
    .Columns(2).ClearContents
    .Columns(3).ClearContents
    .Cells(1, 1) = "INDEX"
    .Cells(1, 1).Name = "Index"
  End With

  For Each wSheet In Worksheets

    If wSheet.Name <> Me.Name Then
      i = i + 1
      '
      ' in the WorkSheet: set up link to return to the index sheet from:
      '
      With wSheet
        .Range("A1").Name = "Start" & wSheet.Index
        .Hyperlinks.Add Anchor:=.Range("A1"), Address:="", _
          SubAddress:="Index", TextToDisplay:="Back to Index"
      End With
      '
      ' in the Index WorkSheet: set up link to go to WorkSheet:
      '
      Me.Hyperlinks.Add Anchor:=Me.Cells(i, 1), Address:="", _
        SubAddress:="Start" & wSheet.Index, _
        TextToDisplay:=wSheet.Name
      '
      Me.Cells(i, 2).Value = wSheet.Range("A2").Value
      Me.Cells(i, 3).Value = wSheet.Range("A3").Value
      '
    End If

  Next wSheet

End Sub

Upvotes: 2

Andi Mohr
Andi Mohr

Reputation: 458

I do this without VBA. This creates a self-populating index table, that pulls info from each worksheet.

autopopulating index table

  1. Create a New Name in Name Manager called wshNames, with the value: =RIGHT(GET.WORKBOOK(1),LEN(GET.WORKBOOK(1))-FIND("]",GET.WORKBOOK(1)))

  2. Create a Contents worksheet, move it to be the left-most tab in your workbook (ie in position one).

  3. Insert a new data table.

  4. In the first column enter numbers 1-50 (or enough to cover the maximum number of worksheets you'll use). These will be your worksheet index IDs.

  5. In column 2 we'll use a formula to autopopulate the worksheet name. If the first worksheet ID number (1) is in cell B5, use this. RAND()*0 keeps the formula volatile and forces Excel to refresh it on any change. =IFERROR(INDEX(wshNames,1+B5+RAND()*0),"")

  6. You can retrieve data from each worksheet using this formula in your next column: =IFERROR(INDIRECT("'"&C5&"'!C2"),""), where C2 is the cell you want to grab from your target worksheet, and C5 is the worksheet name you've got in column 2.

  7. If you want to enter a hyperlink to jump to the worksheet use this in column 4: =IF([@[Your Column 2 Heading]]="","",HYPERLINK("#'" & C5 & "'!A1", [@[Your Column 2 Heading]])). You can tweak this as you need it to display different link text.

  8. Repeat this formula as many times as you need to add more data from your worksheets.

  9. Optional: add conditional formatting to hide unused rows.

Note: Step 1 relies on an XLM formula. More on this here - note you will still need to save as a .xlsm. This technique is borrowed from David Hager, via this.

Upvotes: 0

Related Questions