Visual Basic code stopped working

squirrellydw

Board Regular
Joined
Apr 2, 2015
Messages
65
This was working fine for years but now it gives an error, any idea what is causing it? I think it has something to do with the Address but not sure. Error code is "Run-time error 1004 Application-defined or object-defined error"

VBA Code:
Private Sub Worksheet_Activate()
  Dim wks           As Worksheet
  Dim iRow          As Long
 
  With Me.Range("A1")
    .EntireColumn.ClearContents
    .Value = "Index"
    .Name = "Index"
  End With

  iRow = 1
 
  For Each wks In Me.Parent.Worksheets
    If Not wks Is Me And wks.Visible = xlSheetVisible Then
      iRow = iRow + 1
      With wks
        .Range("A1").Name = "Start_" & .Index
        .Hyperlinks.Add Anchor:=.Range("A1"), _
                        Address:="", _
                        SubAddress:="Index", _
                        TextToDisplay:="Back to Employee List"
      End With
      Me.Hyperlinks.Add Anchor:=Me.Cells(iRow, 1), _
                        Address:="", _
                        SubAddress:="Start_" & wks.Index, _
                        TextToDisplay:=wks.Name
    End If
  Next wks
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This was working fine for years but now it gives an error, any idea what is causing it? I think it has something to do with the Address but not sure. Error code is "Run-time error 1004 Application-defined or object-defined error"

VBA Code:
Private Sub Worksheet_Activate()
  Dim wks           As Worksheet
  Dim iRow          As Long
 
  With Me.Range("A1")
    .EntireColumn.ClearContents
    .Value = "Index"
    .Name = "Index"
  End With

  iRow = 1
 
  For Each wks In Me.Parent.Worksheets
    If Not wks Is Me And wks.Visible = xlSheetVisible Then
      iRow = iRow + 1
      With wks
        .Range("A1").Name = "Start_" & .Index
        .Hyperlinks.Add Anchor:=.Range("A1"), _
                        Address:="", _
                        SubAddress:="Index", _
                        TextToDisplay:="Back to Employee List"
      End With
      Me.Hyperlinks.Add Anchor:=Me.Cells(iRow, 1), _
                        Address:="", _
                        SubAddress:="Start_" & wks.Index, _
                        TextToDisplay:=wks.Name
    End If
  Next wks
End Sub
I tested your code in a Standard Module and got an error.
I had to look it up what Me was in your code, and from what I found, this only works when the code in the Worksheet Code and not a Standard Module.
So, I created a new worksheet and placed the code in the Worksheet Code (right-click on the Sheet name > View Code) and past it there.
Now it's working for me.
Pretty cool. It created a table of contents for all visibile worksheets with hyperlinks. (y)

1738102750959.png
 
Last edited:
Upvote 0
attached is a screenshot of the code incase I'm doing something wrong

I should note that I learned something new today...

OK, so I took a look into why the Worksheet_Activate isn't triggering in your workbook.

I think you have Worksheet Events off.

Troubleshooting Steps:
  1. I created a new workbook and added your code to Sheet1
  2. Added a few Sheets
  3. Selected Sheet1 and the code ran successfully
  4. Disabled Worksheet Events
  5. Added a new sheet
  6. Selected Sheet1 and the code did not run
  7. Enabled Worksheet Events
  8. Selected Sheet1 and the code ran successfully
I would first check the staus using the Immediate Window: ?Application.EnableEvents

1738107833069.png

If it returns False, then enable it using:
VBA Code:
Sub DisableEvents()
    Application.EnableEvents = True
End Sub

1738108353278.png
 
Last edited:
Upvote 0
I should note that I learned something new today...

OK, so I took a look into why the Worksheet_Activate isn't triggering in your workbook.

I think you have Worksheet Events off.

Troubleshooting Steps:
  1. I created a new workbook and added your code to Sheet1
  2. Added a few Sheets
  3. Selected Sheet1 and the code ran successfully
  4. Disabled Worksheet Events
  5. Added a new sheet
  6. Selected Sheet1 and the code did not run
  7. Enabled Worksheet Events
  8. Selected Sheet1 and the code ran successfully
I would first check the staus using the Immediate Window: ?Application.EnableEvents


If it returns False, then enable it using:
VBA Code:
Sub DisableEvents()
    Application.EnableEvents = True
End Sub

View attachment 121754
it came back TRUE, any other ideas?
 
Upvote 0
it came back TRUE, any other ideas?
Check ScreenUpdating, too... ?Application.ScreenUpdating

1738109796973.png

VBA Code:
Application.ScreenUpdating = True

Then be sure to select a different worksheet first, add a new Sheet, then select the worksheet that triggers the Worksheet_Activate event again to see if that new Sheet was added.
 
Upvote 0

Forum statistics

Threads
1,226,061
Messages
6,188,640
Members
453,487
Latest member
LZ_Code

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