Excel VBA - Extract/Open a dynamic hyperlink URL/Address - vba

Greeting!
I am attempting to use excel vba to extract a dynamic hyperlink from OR use excel vba to open the dynamic hyperlink in a worksheet. I am able to click the hyperlink and it will open up correctly. So far, I am not able to find any relevant information pertaining to how to extract the URL the hyperlink is pointing to or simply open the hyperlink.
Here is the formula that is used to build the dynamic hyperlink based on a model selection.
=IFERROR(HYPERLINK(VLOOKUP(S.O.P.!$D$3,Models!$C$2:$G$296,4,FALSE), "Click Here to Open Box Label"), "Unable to retrieve part information")
So based on a selection the user makes this formula goes and finds the respective link that will be used to create the hyperlink.
What I want: Using a button - open the file(PDF) to allow the users to easily print the files they need from one button. I am trying to minimize human touches to eliminate the possibility for human errors.
I have also attempted to record a macro to open up the link, but it only takes me to the worksheet that the hyperlink is placed in.
Here are links to other post that were not able to resolve my issue, but provided good information...
Excel VBA Get hyperlink address of specific cell
Extract URL From Excel Hyperlink Formula
Thank you, I look forward to some helpful responses c:
let me know if I need to elaborate or clarify anything!

Any reason you cant run the vlookup in VBA. Then you will have the link address as a string which you can then do what you want with:
Sub GetLink()
Dim ReturnedAddress As String
ReturnedAddress = Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("S.O.P.").Range("D3"), ThisWorkbook.Sheets("Models").Range("C2:G296"), 4, 0)
end sub

Related

Efficiently copy text from Excel to Word, hyperlink if applicable

What I'm trying to do
I'm trying to copy contents of different Excel cells into their respective Word bookmarks. Then this filled-in Word document gets pasted into an Outlook email. If the cell has a URL in it, I want it to be a clickable hyperlink in the email.
The problem
If the cell's value was a URL, then while it was un-hyperlinked before sending the email, it appeared hyperlinked (for some people) when the email was received. However, others users saw what looked like a clickable link (blue and underlined), but were unable to click because it wasn't actually hyperlinked. My guess is that certain mail programs recognize the URL and automatically hyperlink it while others do not.
What I've tried
I figured the solution was to make sure the URL was hyperlinked before sending. I tried a few forms of .PasteSpecial and .PasteAndFormat with the URL already hyperlinked in Excel. I got close, but either got an unwanted carriage return or no hyperlink. My latest try is a 2-line hack:
'put the cell contents in (sans formatting)
BKMRange.Text = ActiveCell.Value
'If it looks like a hyperlink, then hyperlink it
If UCase(Left(ActiveCell.Value, 4)) = "HTTP" Or UCase(Left(ActiveCell.Value, 4)) = "WWW." Then TempEmailDoc.Hyperlinks.Add BKMRange, ActiveCell.Value
The problem with this is that if the cell is more than just a URL (e.g. "This is your URL: www.google.com"), the URL won't get hyperlinked, since the first 4 characters are not "HTTP" or "WWW.". I could do a more robust search for "HTTP" or "WWW.", but then the code becomes much more complicated. There must be a more efficient way.
My question
How can I efficiently copy cell contents, but preserve hyperlinks if the cell contains them?
Thanks!
You can use the cell's Hyperlinks collection to learn whther the cell contains a hyperlink:
Sub test()
Dim i
i = Selection.Hyperlinks.Count
If (i > 0) Then MsgBox Selection.Hyperlinks(1).Address
End Sub
Now that you know a cell is (contains) a hyperlink, you can insert it as a hyperlink in Outlook. I assume you work in Outlook VBA and open the worksheets in Outlook VBA.
Note that when you enter a proper URL in Excel it will turn it automaticlly into a hyperlink.

Pasting an Excel chart into a Word document so it is editable but not linked

I'm using VBA to create a series of charts in Excel and then copying them into a Word file.
Up till now I've been pasting the charts as pictures, so in Excel I used
ActiveChart.CopyPicture
and then in Word, after selecting the target location:Selection.Paste.
Now I want to change it so the charts will be editable but not linked to the source Excel file.
I copy a chart from Excel using ActiveChart.ChartArea.Copyand look at the paste-special options in Word, the options "use destination theme/keep source formatting & embed workbook" work fine for me:
the chart is editable (also the data is editable which I don't need but is OK) and there is no link to the original Excel file.
BUT - I can't find how to perform this through VBA code. Trying to record this in a macro only give me Selection.Paste - which pastes a linked chart.
I also tried a different approach - pasting a linked chart, and then killing the link. once again, deleting the links in the link editor doesn't get recorded in the macro at all.
Please help with coding any of these two options or suggesting a different approach.
The Range.PasteAndFormat method should work. This takes a WdRecoveryType Enum parameter that lets you specify what kind of result you want.
Selection.PasteAndFormat(wdChart) 'Enum value 14 in case of late binding

Hyperlink doesn't activates macro in VBA

I'm trying to get a hyperlink to activate a macro. I can't use a fixed target address, because I've several links; The goal is to create a clickable history of taken step. So I need to create a back button.
I already tried this:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Range.Address = "$A$4" Then
MsgBox "This isn't what I had in mind"
Else
MsgBox "MACRO!"
Exit Sub
End If
End Sub
Anybody an idea?
If I click the hyperlink it only show the code for that cell.
The code is in the Worksheet module.
The general idea:
I'm making a workbook that calculates based on several steps (40 in total). For example: if sum is larger than 9 go to step 4, if not, go to step 21.
But because we're all human and mistakes can be made, i want excel to make a list of all the steps that have been taken and their answer. I want to make the steps in this list clickable, so when they click on the step, the can go back to that step and continue from there. So a lot of hyperlinks :)
Why not use Worksheet_SelectionChange with the same setup?
This seems to work for me, I've copy and pasted your code into the "Sheet1" module of a new Excel workbook, and put a hyperlink on the 'Sheet1' tab to click on. The MsgBox pops up as expected.
I believe the macro will only work if the code is in the Sheet module for the Worksheet that the users is clicking the link on.
If you need a Hyperlink Back button, you can always add one to the Quick Access Toolbar:
This Macro doesn't work if you use =HYPERLINK(), you have to convert it via 'rightclick', Hyperlink.

Microsoft Excel Search Engine - Hyperlink Results Provides Error Message

I've been trying for two days to figure out this problem.
I have created an Excel search engine to search through a database of hyperlinks in the same worksheet. I want the results to display working hyperlinks, but right now it just displays the text. When the hyperlink is clicked, I get an error message - "Cannot open the specified file."
Can someone please help me. I've tried multiple codes including the following:
=HYPERLINK("#"&VLOOKUP(D5,A2:C91,3,FALSE),""""))) to no avail.
I can also email the spreadsheet if a email is provided. Thank you!
A hyperlink cannot start with the # sign. If the link is to a file, then you need to start with the file path, like shown in the Excel help
=HYPERLINK("D:\FINANCE\1stqtr.xlsx", H10)
If the link goes to another sheet in the same file, use
=HYPERLINK("[Budget.xlsx]E56", E56)
The Vlookup will need to return the correct file path or a valid URL, including the "http://"

VBA to open Excel hyperlink does not work when hyperlink generated with a formula

There seems to be a bug with Excel hyperlinks which are generated with a formula. I'm using Excel 2010. I have a spreadsheet with cells containing URLs, and my goal is to do the following two things:
Turn these cells into hyperlinks.
Create a keyboard shortcut to open these hyperlinks so I don't have to use the mouse.
To do #1, initially I just used the function =HYPERLINK(). So, my URLs are in column A, and I used this formula to make hyperlinks in column B.
To do #2, I created the following macro which should open the hyperlink with the keyboard shortcut Ctrl+H:
Sub Open_Hyperlink()
'
' Open_Hyperlink Macro
'
' Keyboard Shortcut: Ctrl+h
'
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
The problem is that this macro only seems to work on hyperlinks which are not created using a formula. For example, if I just type into a cell http://www.google.com, Excel will automatically make this a hyperlink and the keyboard shortcut macro works, where it doesn't with formula generated hyperlinks.
I've also noticed that when I right click on formula generated hyperlinks, there is no option in the drop-down menu to open the hyperlink, yet there is that option when right clicking on hyperlinks not generated by a formula.
I've found the following workaround. Rather than generate hyperlinks using a formula, I used a macro which I found here.
Sub HyperAdd()
'Converts each text hyperlink selected into a working hyperlink
For Each xCell In Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell
End Sub
I'm able to use the keyboard shortcut to open the hyperlinks generated with this macro. I'm wondering if anyone has or had a similar problem, and why the formula generated hyperlinks are not working for me. I would prefer to use formulas to make hyperlinks in the future, since it is simpler, so if anyone knows of a way to avoid using a macro to make hyperlinks, I'd really appreciate it.
I'm wondering if anyone has had a similar problem, and why the formula
generated hyperlinks are not working for me.
Alas, this seems to be painful truth: Excel does not add to Hyperlinks collection formula-generated links - below is the screen from the debugger which is pointed to =HYPERLINK("http://www.google.com/";"Google"):
I'm not sure whether this is a deliberate implementation or a bug, but yes, formula-generated links may NOT be opened using Hyperlinks().Follow method.
However, if you're going to use keyboard shortcut for links opening, just use the following code - it will automatically convert to clickable link selected cell text and open it:
Sub Open_Hyperlink()
Selection.Hyperlinks.Add Anchor:=Selection, Address:=Selection.Formula
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
Just assign any shortcut and you're ready to go) Here is the sample: https://www.dropbox.com/s/d4cie7lun22quma/FollowLinks.xlsm
Hope that's somehow helpful. Good luck!