help with inserting variables into formula

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
Hey guys

I have the below. It is erroring out on the second statement. I suspect a quote is in the wrong spot. Can someone please help? ALso, can someone explain to me when to use "" and """ and & when using variables within formulas? it doesnt make sense to me.

Jordan

ActiveCell.FormulaR1C1 = _
"=+IF(AND(RC[-7]=""=CO21army" & Left(ThisWorkbook.Worksheets("Variables").Range("a2").Value, 3) & _
Right(ThisWorkbook.Worksheets("Variables").Range("A3").Value, 2) & ".xlsx"",OR(RC[-12]="" _
& right(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 2) & "/" & left(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 4)", RC[-12]="" & right(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 1) & "/" & left(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 4)"")),""X"","" "")"


", RC[-12]="" & right(ThisWorkbook.Worksheets("
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The best way i have found for sorting these problems is to remove the equals sign at the start so that you are just writing text to the cell instead of an equation, Then selelct the cell and put the equals sign back in again, excel will then show you where the error is. i.e change:
VBA Code:
ActiveCell.FormulaR1C1 = _
"=+IF(AND(RC[-7]=""=CO21army" & Left(ThisWorkbook.Worksheets("Variables").Range("a2").Value, 3) & _
to
VBA Code:
ActiveCell.value = _
"+IF(AND(RC[-7]=""=CO21army" & Left(ThisWorkbook.Worksheets("Variables").Range("a2").Value, 3) & _
the double quote character is used in VBA to show the start and end of a text string, this is usually fairly obvious until you wnt to include the double quote character into the text string. it is in the this second case that you need the """. I always avoid this by using the ascii function to create the double quote character SO if I want string that was "Varaibles" i.e including double quotes
is would create as :
VBA Code:
tt = Chr(34) & "Variables" & Chr(34)
compare this to what you get with :
VBA Code:
tt = """Variables"""
 
Last edited:
Upvote 0
The best way i have found for sorting these problems is to remove the equals sign at the start so that you are just writing text to the cell instead of an equation, Then selelct the cell and put the equals sign back in again, excel will then show you where the error is. i.e change:
VBA Code:
ActiveCell.FormulaR1C1 = _
"=+IF(AND(RC[-7]=""=CO21army" & Left(ThisWorkbook.Worksheets("Variables").Range("a2").Value, 3) & _
to
VBA Code:
ActiveCell.value = _
"+IF(AND(RC[-7]=""=CO21army" & Left(ThisWorkbook.Worksheets("Variables").Range("a2").Value, 3) & _
the double quote character is used in VBA to show the start and end of a text string, this is usually fairly obvious until you wnt to include the double quote character into the text string. it is in the this second case that you need the """. I always avoid this by using the ascii function to create the double quote character SO if I want string that was "Varaibles" i.e including double quotes
is would create as :
VBA Code:
tt = Chr(34) & "Variables" & Chr(34)
compare this to what you get with :
VBA Code:
tt = """Variables"""
thanks this got me going. I found out that if its a string then you need to use :""string""
if its a variable then use "&variable&"
that was the issue here is the corrected code

ActiveCell.Formula = _
"+IF(AND(RC[-7]=""=CO21army" & Left(ThisWorkbook.Worksheets("Variables").Range("a2").Value, 3) & _
Right(ThisWorkbook.Worksheets("Variables").Range("A3").Value, 2) & ".xlsx"",OR(RC[-12]=""" _
& Right(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 2) & "/" & Left(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 4) & ", RC[-12]=" & Right(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 1) & "/" & Left(ThisWorkbook.Worksheets("Variables").Range("a6").Value, 4) & ",""X"","" "")"
Range("AB2").Select
 
Upvote 0
Solution

Forum statistics

Threads
1,214,883
Messages
6,122,077
Members
449,064
Latest member
MattDRT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top