I have inherirted an application that has been working up until now using VBA to put hyperlinks on an active worksheet in Excel to other worksheets in the workbook.
I have debugged the code and narrowed it down to the following:
Function ksListSheets()
'===========================================================
' Copy summary AFO data to worksheet
'===========================================================
On Error GoTo Err_ksListSheets
Dim ws As Object
Dim wsMain As Object
Dim intCtr As Integer
Dim intRC1 As Integer
intCtr = 7
With oWkBk.Sheets("MainSheet")
Set wsMain = oWkBk.Sheets("MainSheet")
End With
wsMain.Activate
For Each ws In oWkBk.Worksheets
Select Case ws.Name
Case "MainSheet", "rhTemplate", "rhTemplateAuto", "rhTemplateFire", "rhSummaryData", "rhTeamAFOData"
' do not build hyperlinks for these sheets
Case Else
ActiveSheet.Hyperlinks.Add _
Anchor:=Cells(intCtr, 3), _
Address:="", _
SubAddress:="'" & Replace(ws.Name, "'", "''", 1) & "'!A1", _
TextToDisplay:=ws.Name
intCtr = intCtr + 1
End Select
Next ws
Done_ksListSheets:
ksListSheets = intRC1
Set ws = Nothing
Set wsMain = Nothing
On Error Resume Next
Exit Function
Err_ksListSheets:
MsgBox Err.Number & " " & Err.Description
intRC1 = 16
Resume Done_ksListSheets
End Function
What happens is the Excel workbook is open to the "MainSheet" and is active. It goes through the case once and the ws.Name is set at "MainSheet" so it does nothing and goes to the next worksheet, which is "Zone - Auto". The code then falls through to Case Else and immediately goes to Err-ksListSheets. I have Anchor, SubAddress and TextToDisplay in my watch list and they display "expression not defined in context" in the value field. Again this code had been working up until recently.
Can anyone help. I have no idea.
I have debugged the code and narrowed it down to the following:
Function ksListSheets()
'===========================================================
' Copy summary AFO data to worksheet
'===========================================================
On Error GoTo Err_ksListSheets
Dim ws As Object
Dim wsMain As Object
Dim intCtr As Integer
Dim intRC1 As Integer
intCtr = 7
With oWkBk.Sheets("MainSheet")
Set wsMain = oWkBk.Sheets("MainSheet")
End With
wsMain.Activate
For Each ws In oWkBk.Worksheets
Select Case ws.Name
Case "MainSheet", "rhTemplate", "rhTemplateAuto", "rhTemplateFire", "rhSummaryData", "rhTeamAFOData"
' do not build hyperlinks for these sheets
Case Else
ActiveSheet.Hyperlinks.Add _
Anchor:=Cells(intCtr, 3), _
Address:="", _
SubAddress:="'" & Replace(ws.Name, "'", "''", 1) & "'!A1", _
TextToDisplay:=ws.Name
intCtr = intCtr + 1
End Select
Next ws
Done_ksListSheets:
ksListSheets = intRC1
Set ws = Nothing
Set wsMain = Nothing
On Error Resume Next
Exit Function
Err_ksListSheets:
MsgBox Err.Number & " " & Err.Description
intRC1 = 16
Resume Done_ksListSheets
End Function
What happens is the Excel workbook is open to the "MainSheet" and is active. It goes through the case once and the ws.Name is set at "MainSheet" so it does nothing and goes to the next worksheet, which is "Zone - Auto". The code then falls through to Case Else and immediately goes to Err-ksListSheets. I have Anchor, SubAddress and TextToDisplay in my watch list and they display "expression not defined in context" in the value field. Again this code had been working up until recently.
Can anyone help. I have no idea.