I want a method to create a hyperlink of some variable in excel macro.
My requirement is I have to capture a link in some variable for ex:
abc = InputBox("Enter the path")
now I want to use this abc as a parameter to a hyperlink function and create a hyperlink with name xyz.
Can you help me to solve this issue?
OK, here is the code. What it is doing is
1. Asking what URL you want
2. Storing the URL in a variable called URL
3. Going to Sheet 1, adding the hyperlink function to cell A5, linking it to the URL you input
4. Displaying the friendly name you give it in the code
Let me know if you have any questions | you will just need to modify the code for the URL you want or place the links where you want.
Tested and working. Please vote as answer :)
Sub CreateHyperLink()
Dim URL As String
URL = InputBox("Enter the link")
With Worksheets("Sheet1")
.Hyperlinks.Add Anchor:=.Range("A5"), _
Address:="http://www." & URL, _
TextToDisplay:="Google"
End With
End Sub
Use the HYPERLINK function:
=HYPERLINK(link_location, friendly_name)
Creates a shortcut or jump that opens a document stored on your hard drive, a network server, or on the Internet.
For example in macro code:
Worksheets("Sheet1").Range("A1").Formula = "=HYPERLINK(""" & link_destination & """,""" & link_text & """)"
Note the use of "" (two double quotes) in the VBA code to produce a single double quote in the cell formula.
Related
I have the code below and it works perfectly the way I want. I type a certain phrase in Excel pop-up window using VBA to search using Google Search. However, I would love to be able to store all the Excel VBA Google searches, either in the same Excel file (same/another sheet) or in another file. Does anybody know if this is possible to do? I don't know if it's the code that should be modified or the Excel settings in some way.
Private Const LicenseRegistration As String = "+brott+och+straff"
Private Sub CommandButtonSearch_Click()
Dim query As String
Dim search_string As String
Dim googleChromePath As String
query = InputBox("Enter your keyword", "Google Search")
search_string = Replace(query, " ", "+") & LicenseRegistration
googleChromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (googleChromePath & " -url http://google.com/search?q=" & search_string)
With ThisWorkbook.Worksheets("AnotherSheet")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = search_string
End With
End Sub
A window pops up titled "Microsoft Visual Basic" and it says: Run-time error "9":
Subscript out of range
I copied and pasted the code exactly as you wrote it, even tried to modify it a bit, but to no avail.
I really hope you can see the picture with this link: https://drive.google.com/file/d/1me7xBn8jGvtmpRADp5QFUFmR9k2oGzBs/view?usp=sharing
If you're just looking to keep a running list of search_string, then you can use:
With ThisWorkbook.Worksheets("AnotherSheet") ' change sheet name as needed
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = search_string
End With
before (or even after) the Shell call.
I need some help with my VBA Script.
I need to use already assign value in vlookup formula in vba script.
Currently i'm using the below code as you can see i have already assigned Value Like "Path", "Work_sheet", "Sheet_no".
Now i want to use my assign value VBA
Required VBA
Range("C9").Formula = "=VLOOKUP(A8,'& Path & Work_sheet & sheets_no &'!$A$14:$B$14,2,FALSE)"
Currently using VBA
Path ="C:\Users\Desktop\iMAGE lINK cHECKING\"
Work_sheet = "[Main Image Link.xlsm]"
sheets_no = "Sheet1"
Range("C9").Formula = "=VLOOKUP(A8,'C:\Users\Desktop\iMAGE lINK cHECKING\[Main Image Link.xlsm]Sheet1'!$A$14:$B$14,2,FALSE)"
Your quotes were mismatched and the & need to be outside of quotes so the substitution can be made.
Range("C9").Formula = "=VLOOKUP(A8,'" & Path & Work_sheet & sheets_no & "'!$A$14:$B$14,2,FALSE)"
HTH
I would like to save my file with the flexible name, which will change as the cell value changes.
The one answer is here:
Save a file with a name that corresponds to a cell value
however, I want also some fixed part of the name, which won't change unlike the part described in the query above.
Basing on this solution I tried to write something as follows:
Sub Save ()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = "NBU" & name & "- Opportunity list.xlsx"
ActiveWorkbook.SaveAs Filename:=Custom_Name
In the effect, I am getting an error:
This extension cannot be used with the selected file type. Change the file extension in the File name text box or select a different type file by changing the Save as type.
I would like to have this file in the .xlsx extension.
Excel VBA - save as with .xlsx extension
The answer above doesn't really match to my situation.
It will be vital to remove all form control buttons in the newly saved file, when possible.
Thanks & Regards,
End Sub
There is no action in the routine listed to save the file. It just simply takes the contents of a cell and creates a string with wrapped values.
I am not totally sure of what your goal is, but you need to add the action from the second link you provided. Workbook.SaveAs Method.
See the code below for a working example that I created to test.
Public Sub Save()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = ThisWorkbook.Path & "\" & "NBU" & name & " - Opportunity list.xlsx"
'Disable alert when saving
Application.DisplayAlerts = False
'Save the workbook.
ActiveWorkbook.SaveAs Filename:=Custom_Name, FileFormat:=51
End Sub
You should note that after this code has executed, you will now be in the newly created file. This is not an export.
Test this and let me know if you have any questions. There are a few things that seem to be unnecessary in your code, but we can address those if you find this answers your first issue.
Edit:
I would also call out specifically then worksheet with the range as well.
name = Worksheets("Sheet1").Range("A2")
You said the file name is 'Opportunity v1.0.xslm'. Would it have macros? My version of Excel complains about using .xlsx if there is code in the workbook.
I am trying to change an excel workbook link, but by constructing a new workbook link using dates in cells (the naming convention is always the same, except that the date changes)
I know that to change an excel workbook link (using VBA), all I have to do is:
ActiveWorkbook.ChangeLink "c:\excel\book1.xls", _
"c:\excel\book2.xls", xlExcelLinks
But what if I want to construct a new workbook link using different figures in cells?
This is what I have been trying:
ActiveWorkbook.ChangeLink "part of file name" & Range("N2") & ".xlsx"", _
"part of file name" & Range("N4") & ".xlsx"", xlExcelLinks
Range N2 is the previous week's date, and Range N4 is current week's dates.
But whenever I do this I get the following message "Compile error: Syntax error"
Any ideas on what I can do? Really want to be able to construct file name as it'll mean I can automate a LOT of what I do...lol
Thank you in advance for any guidance on this!
First, if you want to include " in string you must double it, so:
ActiveWorkbook.ChangeLink """part of file name" & Range("N2") & ".xlsx""", _
"""part of file name" & Range("N4") & ".xlsx""", xlExcelLinks
this was your error reason
Also you should refer to .Value property od Range object, and if cells you are using contains dates the safest way would be using Format(Range("N4").Value, "yyyymmdd")
I have a spreadsheet in which I add hyperlinks to files using vba as follows:
Sheet1.Cells.Hyperlinks.Add Sheet1.Cells(1, 1), objFile.Path
This works fine. But if i move the spreadsheet which has the hyperlinks in it to another folder, all the hyperlinks change relative to the folder where I move the spreadsheet to.
Is there a way to stop this happening and fixing the hyperlinks path.
Thanks
Try adding the hyperlink full path formula rather than the hyperlink object
Sub AddHyperlinkFormula()
Dim strMyPath As String, strMyFile As String, strName As String
strMyPath = "C:\Path\to\"
strMyFile = "Workbook.xlsx!"
strName= "Alt Text!"
ActiveCell.Formula = "=HYPERLINK(""" & strMyPath & strMyFile & """,""" & strName& """)"
End Sub
The same thing drove me nuts.. '=hyperlink' is not always a choice since it has a 255 maxchar limitation. So the best solution is indeed to set the hyperlink base (which is workbook-specific). Can be done in two ways:
1) File -> Properties -> Summary Tab -> Hyperlink base
2) or with vba ActiveWorkbook.BuiltinDocumentProperties(29)