IF(ISBLANK(" & variable & "!G2);" & """""" & ";IF(ISERROR(DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2)));" & """""" & "; (DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2))))"
variable is a name of variable which contains name of sheet.
I get: "application-defined or object-defined error.
Can someone help me?
Try to use this one (removed ( before DATE):
Range("A1").FormulaLocal = "=IF(ISBLANK(" & variable & "!G2);" & """""" & ";IF(ISERROR(DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2)));" & """""" & "; DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2))))"
The main problem is that you are using semi-colons instead of commas in the if statement.
Initially I thought you were trying to do this in the worksheet, in which case you would have needed to use INDIRECT().
However on re-reading your question I realised you were trying to build a formula string in VBA.
The formula string you are building is not the correct syntax. For example you use ";" instead of ",". I also think you have the wrong number of brackets.
If I were you I would debug this by doing something like:
sFormula = "IF(ISBLANK(" & variable & "!G2);" & """""" & ";IF(ISERROR(DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2)));" & """""" & "; (DATE(YEAR(" & variable & "!G2);MONTH(" & variable & "!G2)+6;DAY(" & variable & "!G2))))"
Debug.Print sFormula
And then pasting the resulting string into the spreadsheet and see if it works, and edit as appropriate.
Related
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) & ")))"
I cant for the life of me get the escapes working correctly for my macro. I am trying to input a Countif function that uses wildcards on either side of a cell reference value, it also finds the last row within a separate tab to help define the range to count in.
I want the target cell to contain the following string:
=COUNTIF('NCR''s'!$B$8:$C$117,"*"&'Design Baseline'!B8&"*")
The following is the code that attempts to do this:
Sub test()
Dim Wild1 As String
Dim Wild2 As String
Wild1 = Chr(34) & Chr(42) & Chr(34) & Chr(38)
Wild2 = Chr(38) & Chr(34) & Chr(42) & Chr(34)
'code for counting instances of parts on each tab
Sheets("NCR's").Select
Cells.Select
Selection.EntireRow.Hidden = False
NCRlastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Sheets("Design Baseline").Select
Cells.Select
Selection.EntireRow.Hidden = False
'Range("AB8").Formula = "=COUNTIF('NCR''s'!$B$8:$C$" & NCRlastRow & "," & Chr(34) & "*" & Chr(34) & "&" & "'Design Baseline'!B29" & "&" & Chr(34) & "*" & Chr(34) & ")"
Range("AB8").Formula = "=COUNTIF('NCR''s'!$B$8:$C$" & NCRlastRow & "," & Wild1 & "'Design Baseline'!B29" & Wild2 & ")"
End Sub
As you can see in the line beginning with ', I have tried to use the Chr(ref) method to get around this as well as using user defined strings for the ", * and & symbols. I have also attempted the double " escape and even ". Not sure how to get around this o.O
The following line was what I used before realizing I needed wildcards and worked fine, not sure if it would help but I'll put it here regardless:
Range("AC8").Formula = "=COUNTIF('NCR''s'!$B$8:$B$" & NCRlastRow & ",'Design Baseline'!B8)"
Any help would be greatly appreciated!
You need to escape the double quotes in your formula.
Something like this:
Dim formula As String
formula = "=COUNTIF('NCR''s'!$A$8:$C$" & NCRlastRow & ",""*"" &'Design Baseline'!B8&""*"")"
Debug.Print formula
Range("AB8").formula = formula
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 :)
I'm trying to add a formula using VBA to a cell in Excel, the (simplified) code is as
Dim destRow as integer
destRow = 107
Cells(destRow, 19) = "=IF(L" & destRow & "="""";"""";""UNB"")"
`I've also tried the following (in place of the last line):
Cells(destRow, 19) = "=IF(L" & destRow & "=" & chr(34) & chr(34) & ";" & chr(34) & chr(34) & ";" & chr(34) & "UNB" & chr(34) & ")"
In the immediate window, both produce the correct output:
=IF(L107="";"";"UNB")
...however, I get error 1004 "Application-defined or object-defined error" on the last line when I run my code.
Any help would be appreciated!
You should replace the semicolons ";" in the IF formula for commas ",". They will appear as semicolons depending on your excel configuration when the formula is interpreted.
I think you probably just need to refer to the relevant worksheet.
Your code would work if it was in the code for a sheet rather than in a module.
So
Sheet1.Cells(destRow, 19) = "=IF(L" & destRow & "=" & chr(34) & chr(34) & ";" & chr(34) & chr(34) & ";" & chr(34) & "UNB" & chr(34) & ")"
Obviously substitute for the desired sheet if it's not Sheet1
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)"