Creating Hyperlink to the last worksheet in the workbook (VBA)

kps1201

New Member
Joined
Sep 25, 2015
Messages
4
Hi All,

I'm trying to write a VBA formula that would insert a hyperlink to the last tab of the workbook. Currently I'm able to get the hyperlink displayed where I want it with the text that I want it. Bust using a location of "Sheets(Sheets.Count)" within the formula isn't getting me anywhere.

Thanks,
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
try this:
Code:
Sub AddHyperlink()
    Dim x As Integer
    x = ThisWorkbook.Sheets.Count
    ActiveSheet.Hyperlinks.Add Sheets("NameofSheet").Cells(20, 1), "", Sheets(x).Name & "!A1", "", "Hello"
'-------this places a hyperlink called "Hello" in cell A20 on Sheet "NameofSheet".
'-------the hyperlink goes to the last sheet in the workbook, cell A1
End Sub
 
Upvote 0
Perhaps you can help me with another similar problem? I'm trying to perform a vlookup function within VBA, which isn't difficult in itself, but the problem lies within the location of the data range.

A new tab is created as part of this macro I'm building, which is why we we're focusing on the last tab in the workbook in the above problem. However, the name of this worksheet is always going to be changing as more and more are added as part of the macro. I'm trying to get a vlookup from the recently created last tab into my summary page. Right now I have the VBA as:

Sheets("Summary").Select
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(0,2).Select

ActiveCell.FormulaR1C1 = _
'"=VLOOKUP(""Overall Status"", LastWorksheet[-2]:C[3], 6, FALSE)"

The value of the LastWorksheetneeds to be the last worksheet in the workbook. The location of the data we're pulling will always been the same.

Any thoughts?

Really appreciate it.
 
Upvote 0
I can tell you how to do it in VBA, but writing the "=VLOOKUP("... in VBA is a serious pain in the butt. I've been trying to get the god**** syntax right for too long, and I'm throwing in the towel.

Here's how you can do it in VBA, though - downside being, this won't recalculate when your worksheet changes. You'd have to make a worksheet_change event with this, to get it to work:
Rich (BB code):
Sub vlooklast
    Sheets("Summary").Select
    Dim x As Integer
    x = ThisWorkbook.Sheets.Count
    Dim R As Range
    Set R = Sheets(x).Range("A1:F19") '<--------you said the location is fixed, right? Modify this to match it.
    ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(0,2).Select
    Selection.Value = Application.WorksheetFunction.VLookup("Overall Status", R, 6, False)
End Sub
 
Upvote 0
That definitely got me closer, but just as you mentioned, it doesn't recalculate when the worksheet changes which is the functionality i'm trying to incorporate. Would it be possible by setting the target cell equal to that cells value and not using a vlookup? Something as easy as the "=E6" formula or something, so the value of the target cell would change when the value was modified in the worksheet?
 
Upvote 0
That's the thing - I can't figure out how to get it to refer to Sheet x range(something) when x is variable. I can do it VB, but not in workbook. Maybe start a separate thread asking about this?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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