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
Related
IN VBA
Function SNAME(number As String) As String
Dim WORD$
WORD = "Sheet" + number
' SNAME = Worksheets(1).CodeName
For i = 1 To ThisWorkbook.Worksheets.Count
If Worksheets(i).CodeName = WORD Then SNAME = Worksheets(i).Name
Next i
End Function
IN EXCEL
=SUMIFS((SNAME(19)!$T$2:$T$9962),(SNAME(19)!$A$2:$A$9962),"=31",(SNAME(19)!$C$2:$C$9962),"<="&EDATE(TODAY(),-12))
Purpose:
The idea here is that I want to pass the sheets name to the range function based on its code name. the VBA function SNAME seems functional and returns the worksheet's name based on the VBA object code name. When I try to combine that with the range function in the equation, there seems to be an error I'm unsure how to resolve. When I replace SNAME(19) with the sheet name the Equation executes properly.
Goal:
I need to be able to reference the sheet using its code name to avoid future errors of individuals changing the name of the sheets. I'd really appreciate any advice someone might have on the issue.
As far as I know, Excel automatically corrects the formula if any name used there has been changed. So I'm not sure whether you need this construction. Anyway, to make it work we have to change SNAME(19)!$T$2:$T$9962 to something like INDIRECT("'" & SNAME(19) & "'!$T$2:$T$9962").
To make this cleaner, let's put quoting inside the function SNAME. Also, I'd change the type of number to Variant and put Exit For on success:
Function SNAME(number) As String
Dim sh As Worksheet
Dim WORD$
WORD = "Sheet" & number
For Each sh In ThisWorkbook.Worksheets
If sh.CodeName = WORD Then
SNAME = "'" & sh.Name & "'"
Exit For
End If
Next sh
End Function
In the formula, I would use LET to ease editing the function call and to call it once:
=LET(sh, SNAME(19),
SUMIFS(
INDIRECT(sh & "!$T$2:$T$9962"),
INDIRECT(sh & "!$A$2:$A$9962"),
"=31",
INDIRECT(sh & "!$C$2:$C$9962"),
"<=" & EDATE(TODAY(),-12)
)
)
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")
Currently I need to change over hundreds of cells of hyperlinks to a new format that is being used.
Previously we would have for example "https://oldServer:oldPort/project_code/Some_File"
and we are now moving to
"https://newServer:newPort/project_code/Some_File".
I only want to be able to change the "oldServer:oldPort" to "newServer:newPort" without changing the rest of the hyperlink.
I know an excel macro would be the easiest solution but I do not have the experience to be able to create one.
You'd want to loop through each of the hyperlink objects in each sheet.
Code something like this should work. (Make sure you backup before testing this, since I can't test on your actual data!)
Also, note that this changes the link but not the text displayed, since I'm not sure what that consists of. The text could be changed with the same procedure by updating TextToDisplay, or else by using Find & Replace.
Edit: (added confirmation on each change.)
Sub changeLinks()
Const oldPrefix = "https://oldServer:oldPort/"
Const newPrefix = "https://newServer:newPort/"
Dim h As Hyperlink, oldLink As String, newLink As String
For Each h In ActiveSheet.Hyperlinks
'this will change Address but not TextToDisplay
oldLink = h.Address
Debug.Print "Found link: " & oldLink
If Left(oldLink, Len(oldPrefix)) = oldPrefix Then
newLink = newPrefix & Right(h.Address, Len(h.Address) - Len(oldPrefix))
If MsgBox("Click OK to change:" & vbLf & vbLf & oldLink & _
vbLf & vbLf & "to" & vbLf & vbLf & newLink, vbOKCancel, _
"Confirmation?") <> vbOK Then Exit Sub
h.Address = newLink
Debug.Print " Changed to " & h.Address
End If
Next h
End Sub
I removed the hyperlink from the column containing the old server information. Then I created a couple of new columns. I typed in the full path to the new server in the first new columns and filled it down. In the other new column I created a formula to concatenate the full path and the friendly name (which happened to be what the link was originally named). This created the new server path along with the file name. I copied/pasted values of this column and deleted the contents of the first new column I created. I then used the HYPERLINK formula to put the hyperlink and the friendly name back together. It sounds more convoluted than it was - took me longer to write it down here than to actually do it in my Excel spreadsheet. I hope this makes sense.
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.
I'm trying to use STRCONV vbPropercase to correct input in a macro that I use to copy data from one "input" worksheet to another. I have been able to assign specific variables to cells and then apply the STRCONV Propercase to the variable, but no change is made to the text in the cell when I do this. The code saves the file as a combination of the variable names, so I know that it makes the corrections there. How do I make the change appear in the cell and not just to the variable in the code?
Here is an excerpt from the code I'm using:
Dim Property As String
Dim Accnt As String
Property = Worksheets("Audit").Range("L6").Value
Property = StrConv(Property, vbProperCase)
Accnt = Worksheets("Audit").Range("L7").Value
ActiveWorkbook.saveas "D:\(username)\Documents\" & Accnt & " - " & Property & ".xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Just flip the assignment around:
Worksheets("Audit").Range("L6").Value = Property