I'm trying to generate a HYPERLINK formula via VBA but when I click on the resulting link I get an Excel error message "Cannot open the specific file." I tried this but same results.
This is my code to generate the link.
wksUsersSheet.Range(COMMENTARY_COL & lngRowNow).FormulaR1C1 =
"=HYPERLINK(" & Chr(34) & "[" & strThisBooksName & "]" &
strFoundMatchWorksheetName & "!" & strFoundMatchAddress & Chr(34) & ","
& Chr(34) & rngReturnedMatchingPart.Value & Chr(34) & ")"
The immediate window displays this:
=HYPERLINK("[UPN_Template_Wip]AleksV!$I$4","2322 734 61009L")
I've tried it with .Formula as well but the result is the same.
I'm using Excel 2007 in Windows 7. Am I missing something here 'cause it's all looking fine, just not working fine. Thanks.
Using the macro recorder to create a hyperlink on a blank test sheet, I got:
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"Sheet1!A1", TextToDisplay:="HL Here"
Translating that to your specifics, I believe you should have:
wksUsersSheet.Hyperlinks.Add Anchor:=wksUsersSheet.Cells(lngRowNow, Commentary_Col), _
Address:="", _
SubAddress:="[" & strThisBooksName & "]" & strFoundMatchWorksheetName & "!" & _
strFoundMatchAddress, _
TextToDisplay:=rngReturnedMatchingPart.Value
Based on your comment try this:
Convert your strFoundMatchAddress to a row number & a column number, I'll call them FoundMatchAddrRow and FoundMatchAddrCol
ActiveCell.FormulaR1C1 = "=HYPERLINK(" & Chr(34) & "[" & strThisBooksName & "]" & _
strFoundMatchWorksheetName & "!R[" & FoundMatchAddrRow & "]C[" & _
FoundMatchAddrCol & "],""" & _
rngReturnedMatchingPart.Value & """)"
Note the use of "" generates a single " in your final string & reduces the concatenation and eliminates the need for Chr(34). It's not required, but it does reduce the typing.
Related
I wanna change the font size from where I start formatting as hours: Format(Sheet3.Range("F" & TaskRow).Value...
I tried using .Font.Size = 8 on the front, back and inside the code but can't get it to make it work. I am very new to VBA, sorry if this is a stupid question, really tried looking for an answer online.
TaskText = Sheet3.Range("C" & TaskRow).Value & _
vbCrLf & vbCrLf & "De " & Format(Sheet3.Range("F" & TaskRow).Value, "h:mm AM/PM") & " a " & _
Format(Sheet3.Range("G" & TaskRow).Value, "h:mm AM/PM") & _
vbCrLf & Format(Sheet3.Range("D" & TaskRow).Value, "###-###-####")
I am trying to copy an IF formula in a range, but my issue, much like others, comes from the double quotes. I currently have CHR(34) to indicate I want the double quote, but my VBA error is Expected end of line.
How I need it to appear as a formula:
=IF(D2=team,"",IF(D2=on," - Type1",IF(OR(D2=lr,D2=sn),"*"," - Type3")))
team, on, lr, sign are variables that refer to strings.
Range()= "=IF(D2=" & team & ", "&Chr(34)&Chr(34)&",IF(D2=" & on & ","&Chr(34)&" - Type1"&Chr(34)&",IF(OR(D2=" &lr& ",D2=" &sn& "),"&Chr(34)&"*"&Chr(34)&","&Chr(34)&" - Type3"& Chr(34)&")))"
It presents the error in the second instance of D2 in the OR statement
I believe the following should work as you expect it to, I simply changed the variable name from on to oni as I don't think you can have a variable named on also I separated the &'s with spaces:
ActiveCell.Formula = "=IF(D2=" & team & ", " & Chr(34) & Chr(34) & ",IF(D2="" & oni & ""," & Chr(34) & " - Type1" & Chr(34) & ",IF(OR(D2=" & lr & ",D2=" & sn & ")," & Chr(34) & "*" & Chr(34) & "," & Chr(34) & " - Type3" & Chr(34) & ")))"
How do I insert or paste the large excel formula in a specific cell in excel formula formate only. My project has large excel table to calulate no. days left and it is linked with current date and time. formula has to be inserted in column whenever i submitted the data from data entry form. It automatically collects the values from different cells in a current sheet and calculates.
But here i getting "compile error, expected end of statement" at formula line ie., at double quatotions "".
I could write directly in excel and drage
or
I could write vba code for the above calculation but due to my project requirement, i have to be inserted the formula.
Is there any way to insert the formula???? i'm using excel 2016
Set Fcell = formulaWks.Range("O7")
'formula = "=$A1+$B1" ' example for testing
Formula = "=IF(YEAR(NOW())=$W$3,IF(ISBLANK($G7),"",IFERROR(IF(DATEDIF(TODAY(),$N7,"y")=0,"",DATEDIF(TODAY(),$N7,"y")&" y ")&IF(DATEDIF(TODAY(),$N7,"ym")=0,"",DATEDIF(TODAY(),$N7,"ym")&" m ")&IF(DATEDIF(TODAY(),$N7,"md")=0,"",DATEDIF(TODAY(),$N7,"md")&" d"),"wrong date")),"Package completed")"
Fcell = ActiveCell.formula
Try the code below, it will work with your basic formula that you wanted to test.
Option Explicit
Sub InsertFormula()
Dim formulaWks As Worksheet
Dim Fcell As Range
Dim FormulaString As String
' modify "Sheet1" to your sheet's name
Set formulaWks = Worksheets("Sheet1")
Set Fcell = formulaWks.Range("O7")
FormulaString = "=$A1+$B1" ' example for testing
Fcell.Formula = FormulaString
End Sub
Regarding your "LONG" formula, the formula string below passes:
FormulaString = "=IF(YEAR(NOW())=$W$3,IF(ISBLANK($G7)," & Chr(34) & Chr(34) & ",IFERROR(IF(DATEDIF(TODAY(),$N7," & Chr(34) & "y" & Chr(34) & ")=0," & Chr(34) & Chr(34) & ",DATEDIF(TODAY(),$N7," & Chr(34) & "y" & Chr(34) & ")&" & Chr(34) & " y " & Chr(34) & ")" & _
"&IF(DATEDIF(TODAY(),$N7," & Chr(34) & "ym" & Chr(34) & ")=0," & Chr(34) & Chr(34) & ",DATEDIF(TODAY(),$N7," & Chr(34) & "ym" & Chr(34) & ")&" & Chr(34) & " m " & Chr(34) & ")" & _
"&IF(DATEDIF(TODAY(),$N7," & Chr(34) & "md" & Chr(34) & ")=0," & Chr(34) & Chr(34) & ",DATEDIF(TODAY(),$N7," & Chr(34) & "md" & Chr(34) & ")&" & Chr(34) & " d" & Chr(34) & ")," & _
Chr(34) & "wrong date" & Chr(34) & "))," & Chr(34) & "Package completed" & Chr(34) & ")"
Debug.Print FormulaString ' for debug, to see the Formula string in the immediate window
Note: Final version of the "long" formula has been edited in by YowE3K - if it doesn't work, blame me (i.e. YowE3K) not Shai.
You need to escape double quotes from the string first by adding double quotes twice in the string like this - ISBLANK($G7),""""
Then use formula like this
Range("O7").Formula = "[Your formula with escaped double quotes]"
I have been batling this suposedly simple problem for 2 hours now and I just cant figure it out. I am trying to make a link and no matter how many " marks I try it always ends up in error or the link says & Poracun(Z).Ime & Can anyone find me a way that I can change the link name ?
Here is my code:
Cells(i, "B").Formula = "=HYPERLINK(""[" & ActiveWorkbook.Path & "\" & "DN_Pokalkulacija.xlsx" & "]" & "'" & "Sheet1" & "'" & "!E" & Poracun(Z).Vrstica & ""","" & Poracun(Z).Ime & "")"
Two issues:
Your " delimiting is not quite right
Formula expects ' around the file name, not the sheet. Like '[FileName]SheetName'!
Try
Cells(5, "B").Formula = "=HYPERLINK('[" & ActiveWorkbook.Path & "\" & "DN_Pokalkulacija.xlsx" & "]" & "Sheet1" & "'" & "!E" & Poracun(Z).Vrstica & ",""" & Poracun(Z).Ime & """)"
I have figured it out , it was quotation problem, i forgot all about it and now i have read up on it inside quotation string you need to make double quotation to use as single in final string. Here is a working link :
g = "=HYPERLINK(""[" & ActiveWorkbook.Path & "\" & "DN_Pokalkulacija.xlsx" & "]" & "'" & "Sheet1" & "'" & "!E" & Poracun(Z).Vrstica & """,""" & Poracun(Z).Ime & """)"
It was all in quotation (") marks :)
Single line of code is causing my application to stop working - I think that the problem is with the apostrophes (maybe wrong escape):
.Formula = "=IF(AND(" 'Criterion " & i & "'!" & cellAdress & ">=1;"'Criterion " & i & "'!" & cellAdress & "<=4);"'Criterion " & i & "'!" & cellAdress & ";0)"
When I try to enter the "' sequence VBA automatically puts a space between " and ' making the latter appear as comment. What is wrong - do I need to use escape character here? If yes how to code it. I get an error 1004 Application of object-defined error.
Thank you
You escape double quotes in VBA by putting 2 double quotes, like so:
""
You can also explicitly call Chr(34) to put in double quotes, like so:
Debug.Print "The following is in double quotes: " & _
chr(34) & "Inside double quotes" & chr(34)
I wasn't able to try out the following formula in Excel, but I think it should work:
.Formula = "=IF(AND(""Criterion""" & i & "!" & cellAdress & _
">=1;""Criterion""" & i & "!" & cellAdress & "<=4);""Criterion""" & _
i & "!" & cellAdress & ";0)"