Complex Hyperlink Function

cmeier7

Board Regular
Joined
Jul 23, 2009
Messages
64
Hi, in VBA I have

Code:
Sheets("" & tab_name & "").Cells(1, 2) = "=HYPERLINK("""" & 'Sheet1'!A2 & ""'Sheet1'!A1"""",""To Main Roll-Up"")"

tab_name is a defined variable.
The code inserts an equation containing a hyperlink function. In 'Sheet1'!A2, I have the current workbook name (ie [Book1]). I believe there is an issue with all of the quotations. Can anyone figure out this puzzle? I've been at it for a while with no luck.

Oh, btw, I get error message '1004' - Application-defined or object-defined error
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
For a start you shouldn't need any quotes within Sheets, that is assuming of course tab_name is a string with a sheet name.

As for the formula, it's hard to tell what the problem is - it probably is the quotes.

What formula do you want to end up with?
 
Upvote 0
I want to end up with a hyperlink. In Sheet1, Cell A2, I have the current workbook name. So I made refrence to that in the first part of the function. I then want the hyperlink to take me back to Sheet1, Cell A1, which I also have. The last part obviously refers to the name of the hyperlink. I want to be able to paste a formula that creates a hyperlink, but paste using VBA. I have a currently working one in my formula bar-

=HYPERLINK("" & 'Sheet1'!A2 & "'Sheet2'!A1", "FPS")

I am trying to get VBA to put that into a cell for me. Hm, actually writing that may have helped....but go ahead and give me your input if you have the time- thanks!
 
Upvote 0
Ok, that works in my formula bar- but now the question I have is how to place in a cell the exact text in the previous post using VBA.
 
Upvote 0
Try turning on the macro recorder and manually entering the formula you want in the cell.

That should give you a start on the syntax you need.

I think you might be overdosing on quotes.

If you enclose something like 'Sheet2'!A1 in quotes that's exactly what you'll get - you won't get anything that is referring to A1 on Sheet2.:)
 
Upvote 0
Ok, I have a clearer problem going on now. This is what I have in code

Code:
Sheets("" & tab_name & "").Cells(1, 3) = "=HYPERLINK("" & 'Sheet1'!A2 & " 'Sheet2'!A1","To Reachback")"

I want
"=HYPERLINK("" & 'Sheet1'!A2 & " 'Sheet2'!A1","To Reachback")"
to be a string that VBA inserts into the desired cell. However, VBA is automatically cutting off the string right before 'Sheet2. Do you notice the space between the quotation and the astric? Originally I type it in without the space, but VBA cuts it off at the ' and makes the rest of the string one of those green-texted notes that the program knows to ignore. How do I avoid this and get VBA to enter all of it as one big string?
 
Upvote 0
I think you should forget about the formula for a moment.

Did you see what I mentioned was the first problem?

Where do you actually want this formula to go?

What does the correct formula look like when you enter it on a worksheet?

This will put the formula =HYPERLINK(Sheet1!A2 & Sheet2!A1,"To Reachback") into C1 on Sheet1.
Code:
 tab_name = "Sheet1"
Sheets(tab_name).Cells(1, 3) = "=HYPERLINK('Sheet1'!A2 & 'Sheet2'!A1,""To Reachback"")"
 
Last edited:
Upvote 0
Ok, for clarification purposes, I want VBA to put this formula (below) into the specific cell. The below formula works. I have the workbook name in Cell A2 of Sheet1. I am wanting to place the hyperlink into Cell C1 of " & tab_name & " which is a String defined variable and changes with every loop in my code.

Sheets(tab_name).Cells(1,3)=HYPERLINK("" & 'Sheet1'!A2 & "Sheet2'!A1", "To Reachback")
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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