Inserting formula as string into a cell - excel

It seems to be a problem with some characters in it
My original formula in the worksheet (working):
ThisWorkbook.Worksheets("Zazmluvnenia").Cells("3", "AM").Value = "=Výkony!M4 & ", " & TEXT(Výkony!J4;"d.m.yyyy") & ", " & TEXT(Výkony!K4;"hh:mm")"
Putting this into VBA doesn't work because of the quotes. I tried to solve the problem this way: How do I put double quotes in a string in vba?
I changed my code according to the answers on the question above:
"=Výkony!M3 & " & Chr(34) & ", " & Chr(34) & " & TEXT(Výkony!J3;" & Chr(34) & "d.m.yyyy" & Chr(34) & ") & " & Chr(34) & ", " & Chr(34) & " & TEXT(Výkony!K3;" & Chr(34) & "hh:mm" & Chr(34) & ")"
And error '1004' occurred.

Double Quotes vs CHR(34)
Your CHR solution was almost fine, but you didn't know that VBA doesn't recognize the semi-colon (;) as a separator like Excel does. You always have to replace every semi-colon (used as a separator) with a comma in VBA.
Similarly VBA uses the dot (.) as the decimal separator.
The Code
Option Explicit
Sub SEMI()
Dim strF As String
' Double double-quotes ("") in several rows.
strF = "=Výkony!M4&"", """ _
& "&TEXT(Výkony!J4,""d.m.yyyy"")&"", """ _
& "&TEXT(Výkony!K4,""hh:mm"")"
' CHR(34) in several rows.
strF = "=Výkony!M4&" & Chr(34) & ", " & Chr(34) _
& "&TEXT(Výkony!J4," & Chr(34) & "d.m.yyyy" & Chr(34) & ")&" _
& Chr(34) & ", " & Chr(34) _
& "&TEXT(Výkony!K4," & Chr(34) & "hh:mm" & Chr(34) & ")"
' Double double-quotes ("") in one row.
strF = "=Výkony!M4&"", ""&TEXT(Výkony!J4,""d.m.yyyy"")&"", ""&TEXT(Výkony!K4,""hh:mm"")"
' CHR(34) in one row.
strF = "=Výkony!M4&" & Chr(34) & ", " & Chr(34) & "&TEXT(Výkony!J4," & Chr(34) & "d.m.yyyy" & Chr(34) & ")&" & Chr(34) & ", " & Chr(34) & "&TEXT(Výkony!K4," & Chr(34) & "hh:mm" & Chr(34) & ")"
ThisWorkbook.Worksheets("Zazmluvnenia").Cells("3", "AM").Value = strF
End Sub

Related

Formula/VBA - Application defined or object defined error

I'm trying to create a vba code to write a formula in determined cell on my sheet, but it doesn't work. What's wrong ? 'Cause, i really don't see what am i doing wrong in this code.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your computer's use of a ; as the regional list separator is fouling you up. VBA is very EN-US-centric so the Range.Formula and Range.FormulaR1C1 expect a comma (,) as the function's argument list separator.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, " & """" & "DROP" & """" & ", " & """" & "REVIEW" & """" & "), " & """" & "REVIEW" & """" & "), " & """" & """" & ")"
Alternately, Range.FormulaLocal property or Range.FormulaR1C1Local property can be used with your own semi-colon as the list separator.
Range("BC" & ActiveCell.Row).FormulaLocal = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your doubling up double quotes within a quoted string is a bit verbose and xlR1C1 notation would save some steps. TEXT(,) is the same as "" in a formula; each produces the same zero-length string.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=""REVIEW"", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=""OK"", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
'.FormulaR1C1
Range("BC" & ActiveCell.Row).FormulaR1C1 = "=IF(RC54=""REVIEW"", IF(ROW(R:R)<MAX(IF(C54:C54=""OK"", C1:C1)), IF(TODAY()-RC51>=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
The xlA1 and xlR1C1 formula rewrites above each produce the following when ActiveCell is on the second row.
=IF($BB2="REVIEW", IF(ROW($BB2)<MAX(IF($BB:$BB="OK", $A:$A)), IF(TODAY()-$AY2>=3, "DROP", "REVIEW"), "REVIEW"), TEXT(,))
As I mentioned in comments above, that formula would seem to be an array formula. In that case, use the .Formula comma based rewrite but change the Range.Formula property to Range.FormulaArray.

Qutation Marks within VBA for IF statement

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) & ")))"

Invalid Qualifier when checking for a specific character in visual basic

Hi i am trying to check for a specific character in visual vba but i encountered the "INVALID QUALIFIER" error when i run my code.
This is my code:
For i = LBound(rtv) To UBound(rtv) - 1
If rtv(i).Contains(":") Then
Value = Split(rtv(i), ":")
rtv(i) = Chr(34) & Value(0) & Chr(34) & ":" & Chr(34) & Value(1) & Chr(34) & ";"
Else
rtv(i) = Chr(34) & rtv(i) & Chr(34) & ":" & Chr(34) & Chr(34)
' rtv(i) = Chr(34) & rtv(i) & Chr(34) & ":" & Chr(34) & Chr(34) & ";"
Next i
The error lies in the IF statement condition but i have no idea what i am doing wrong here. Any help ?
As noted in the comments, rtv(i).Contains() won't work. Arrays don't have functions or properties that you can use this way.
Use this instead:
If InStr(rtv(i),":") > 0 Then

1004 error when writing a formula to a cell using VBA-Excel

I am trying to write a formula into a cell, using VBA. The code excerpt below delivers the correct formula, with correct references. But I keep getting a 1004 run-time error. I can't figure out what is triggering it. Hope this code excerpt is enough to reveal the answer, but if you need more just ask.
Set rg_sheet = ActiveWorkbook.Worksheets("RUNGLANCE")
for colNo = 2 to 8
for rowNo = 3 to 27
daySheetName = Cells(1,colNo)
rg_sheet.cells(rowNo,ColNo).formula = "=VLookUp(" & Chr(34) & "$A" & rowNo & Chr(34) & "), " & daySheetName & "!(" & chr(34) & "$A$3:$B$27" & Chr(34) & ", 2, False"
next rowNo
next colNo
Instead of
rg_sheet.cells(rowNo,ColNo).formula = "=VLookUp(" & Chr(34) & "$A" & rowNo & Chr(34) & "), " & daySheetName & "!(" & chr(34) & "$A$3:$B$27" & Chr(34) & ", 2, False"
try
rg_sheet.Cells(rowNo, colNo).Formula = "=VLookUp($A" & rowNo & ", " & daySheetName & "!" & "$A$3:$B$27" & ", 2, False" & ")"

using symbol in vba to fill up cell

this is my code in vba excel
Range("a1").Value = "=" & Chr(34) & beat_name & Chr(34) & char(38)
final output
="name" & TEXT(TODAY(),"DD/MM/YYYY")
unable to show & sign in excel sheet
Consider:
Sub luxation()
s = "name "
Range("A1").Formula = "=" & Chr(34) & s & Chr(34) & " & TEXT(TODAY(),""DD/MM/YYYY"")"
End Sub

Resources