问题
I'm programming in VBA. I want to create hyperlinks using hyperlink.add method of the ActiveSheet object but it doesn't work.
Here is my code:
'set the link
Dim mainsheet As Worksheet
Dim ws As Worksheet
Set mainsheet = ActiveWorkbook.Sheets("Main")
Set ws = ActiveWorkbook.Sheets(rowNumb + 2)
mainsheet.Hyperlinks.Add Anchor:=mainsheet.Range(rowTablecontent + rowNumb, colTablecontent + 3), _
Address:="", _
SubAddress:=ws.Name & "!A1", _
TextToDisplay:="Link"
Code description:
I have a Mainsheet, on which I have a Table of Contents and a button for starting the macro. When starting the macro, the program imports 4 Files (each has one sheet and equals one sheet in the mainworkbook. With the information of this 4 files there are about 500 sheets which are going to be generated. The point is: now i want to have a table of contents. for each sheet a link in the mainsheet.
Variable:
- rowTablecontent --> Row on the top of the content table
- colTablecontent --> Column on the left of the content table
- rowNumb --> Rowcounter on the inputfile
I used the same code in another program, where it worked, so I don't know what I did wrong.
Does anyone know what could be wrong?
Important Info Copied From Comments
In my project I have 4 Input files/sheets. And there it works. But on the autmatically created sheets, it doesn't work.
回答1:
Since you have not mentioned what error you are getting. There could be many errors. Some of them are.
- Activesheet is not the one you think it is.
- The selection is not a range
- Sheet doesn't exist
- rowNumb is not a valid number
- Sheet/workbook could be protected (Thanks Sam)
Try this
Option Explicit
Sub Sample()
Dim rowNumb As Long
Dim ws As Worksheet
'~~> Change this to the relevant number
rowNumb = 1
If TypeOf Selection Is Range Then
On Error Resume Next
Set ws = ThisWorkbook.Sheets(rowNumb + 1)
If Err.Number <> 0 Then
MsgBox "Sheet doesn't exist"
Exit Sub
End If
On Error GoTo 0
ThisWorkbook.ActiveSheet.Hyperlinks.Add Anchor:=Selection, _
Address:="", _
SubAddress:=ws.Name & "!A1", _
TextToDisplay:="Link"
Else
MsgBox "InValid Range Object"
End If
End Sub
FOLLOWUP FROM COMMENTS
But on the autmatically created sheets, it doesn't work.
You missed my first point. Activesheet is not the one you think it is.
Here is a sample on how to add hyperlink to a newly created file. This demonstration shows how to add hyperlink to Sheet1 A1 of newly created file. The hyperlink will address Sheet2 A1 of the newly created file.
Sub Sample()
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set wb = Workbooks.Add
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
ws1.Hyperlinks.Add Anchor:=ws1.Range("A1"), _
Address:="", _
SubAddress:=ws2.Name & "!A1", _
TextToDisplay:="Link"
End Sub
I am not doing any error handling. i am sure you can take care of that.
回答2:
I tested this, it worked on Excel 2007, you could try?
EDIT to approach to your question.
Sub sofMacro20000463(ByVal rowNumb)
'
' Dim rowNumb
'
' rowNumb = ActiveCell.Row
'
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", _
SubAddress:=Sheets(rowNumb + 1).Name & "!A1", _
TextToDisplay:="Link"
'
End Sub
Sub sofMacroDoIt()
'
Dim rowNumb
'
rowNumb = ActiveCell.Row
'
sofMacro20000463 rowNumb
'
End Sub
This permits navigating to Worksheet in function of row index from the activesheet.
sofMacro20000463() can be called with other parameters.
来源:https://stackoverflow.com/questions/20000463/hyperlinks-add-vba