I have a macro to display a tab list on a single sheet, but I want the macro to also display data from that tab

StackOverflow https://stackoverflow.com//questions/22078635

  •  24-12-2019
  •  | 
  •  

Question

Please bear with this total noob. Here's the Macro I want to modify.

Private Sub Workbook_Open()
Sheets("TabList").Select
Range("b5").Activate
Dim sh As Worksheet
Dim cell As Range
For Each sh In ActiveWorkbook.Worksheets
    If ActiveSheet.Name <> sh.Name Then
        ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
        "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
        ActiveCell.Offset(1, 0).Select
    End If
Next sh
End Sub

When I open the workbook, this macro opens up the TabList sheet and creates a hyperlinked columnar list of all the tabs in the workbook starting at cell B5. While this macro runs, I'd like it to pull data from a particular cell of each sheet and display it in the column next to the hyperlinked name on the TabList sheet, starting at C5.

For example, one of the sheets during the macro is called Sheet2 and has cell J8 that has a value I'd like to display next to it on the TabList sheet. If Sheet2 is the first sheet in the list, it will be at location B5, so the value of Sheet2!J8 should be displayed in cell C5. I'd like that same cell read from each successive sheet and displayed next to its entry further down the list.

Thanks for any help.

Was it helpful?

Solution

Try this code:

Private Sub Workbook_Open()
    Dim sh As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim tabShName As String

    tabShName = "TabList"
    Sheets(tabShName).Select

    Set rng = Range("B5")

    For Each sh In ThisWorkbook.Worksheets
        If sh.Name <> tabShName Then
            rng.Hyperlinks.Add Anchor:=rng, Address:="", SubAddress:= _
                "'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
            rng.Offset(, 1).Value = sh.Range("J8").Value
            Set rng = rng.Offset(1)
        End If
    Next sh
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top