Related
Scenario: I have a code that should write a formula to a worksheet cells. This formula is for an API to retrieve some value. My formula is inside a loop (this is done for multiple columns) and references the first row for an identifier.
The original formula:
=FS(B1;"FI(DATE,,DATE)")
The modified formula with the floating reference (inside the loop):
For i = 1 To lColumn
If wb.Worksheets("Dates").Cells(i, 1).Value <> "" Then
wb.Worksheets("Dates").Cells(i,2).value = "=FS(" & i & "1;"FI(DATE,,DATE)")"
End If
Next i
Where lColumn is some pre-defined number.
Issue: I keep getting the "Unexpected end of statement" error in the formula part of the loop.
What I already tried: I tried different variations, repositioning the "s and 's, for example:
wb.Worksheets("Dates").Cells(i,2).value = "'"=FS(" & i & "1;"FI(DATE,,DATE)")""
or
wb.Worksheets("Dates").Cells(i,2).value = "'=FS(" & i & "1;"FI(DATE,,DATE)")"
or
wb.Worksheets("Dates").Cells(i,2).value = "'""=FS(" & i & "1;"FI(DATE,,DATE)")"
and so on. But the error still persists.
Question: What is the proper way to do this operation?
Working with formulas in VBA is a little bit tricky:
To write a formula, use the range.formula property, not the .value.
You have to write the formula as if you are using an english Excel. Parameter-separator is comma (not semicolon).
If a formula needs a quote, double it so that the VBA compiler understands that you want a quote within a string.
I find it helpfull to write a formula into a variable before assigning it - you can check in the debugger if it is exactly how it should before assigning it.
To check how the formula should look like, write it into a cell, change to the VBA-editor, open the immediate window and write ? activecell.formula
Try (untested as the formula you need is not valid to us):
with wb.Worksheets("Dates")
dim f as string, adr as string
adr = cells(i, 1).address(false, false) ' get rid of Dollar signs
f = "=FS(" & adr & ",""FI(DATE,,DATE)"")"
.Cells(i, 2).formula = f
end with
wb.Worksheets("Dates").Cells(i,2).formula = "=FS(" & Cells(1, i).Address(0,0) & ";""FI(DATE,,DATE)"")"
There may be a better way to convert the column number to a letter (which is the problem you are having, along with the double quotes)!
I am new to VBA, I got a question on &, What does the two & do in this code below, can we delete the last one?
Range("H2").Formula = "=COUNTIF(H4:H" & WKB2.Range("F" &
Rows.Count).End(xlUp).Row & ",""AMB"")"
Some other code has only one & but still working. for example:
ActiveSheet.Range("F5:F" & lastrow1 - 1).Formula = "=IF(C5>0,""CC"",""AMB"")"
what is the difference here.
Thanks in advance.
& is one of the two possible operators for string concatenations in VBA.
(+ is also possible, but IMO & is better)
E.g. "A" & "B" will result in "AB".
Or, to use one of your examples - let's say lastrow1 = 5, so lastrow1 - 1 = 4, so this:
ActiveSheet.Range("F5:F" & lastrow1 - 1)
...will become:
ActiveSheet.Range("F5:F4")
For Excel formula and VBA - Exactly where and whether 1 or 2 '&'s depends on your use case.
To concatenate with rest of formula you need &variable& (so 2 &s).
If there is nothing to concatenate at the end, but there is something at the beginning then use &variable.
If there is nothing to concatenate at the beginning but there is something to concatenate at end then use variable&.
If there is nothing to concatenate at beginning or at end, then just use variable.
Hope this makes sense.
So currently I am trying to update an old excel file's macro codes so that we can use it with a new program. Though I am new to VB I know some C++ and Java from college so I was able to get just about everything working but this section of code. So basically, we are converting excel information to .CVS and when we do the conversion seems to give some of the information three quotation marks on either side instead of the single set like it is listed in the excel document.
To make a simple fix I just added this extra set of code to search each string for quotation marks and remove all but a single set. Instead it seems to just continue replacing sections of the entire string until there is only 1 quotation mark left...do y'all know what the issue might be?
I know this is some poorly written code especially since I havent moved on to fix the infinite loop BUT the main concern right now is just how I am misusing the Replace function.
For X = 1 To 56
String2 = Sheets(1).Cells(X, 1)
Z = 1
Do While Z <> Len(String2)
If Mid(String2, Z, 1) = Chr(34) Then
If Mid(String2, Z + 1, 1) = Chr(34) Then
String2 = Replace(String2, """", "", Z, 1)
' Replace(String2, Char(34) + Char(34), "", Z, 1)
Sheets(1).Cells(X, 1) = String2
Z = 1
Else
Z = Z + 1
End If
Else
Z = Z + 1
End If
Loop
Next
The File Information being altered:
"MH 7 to Tipton1",7.942,1,1,0.22
4.9,1,18.8
"SINGLE CABLE",15,206.8
""--""",0,0 ^^ This is a string that gets eaten by the Replace
function after a few loops. It continues till (",0,0") Previous
strings were not altered in operation.
0,"""--""",0,"""H""","""--""",100,21.1
0,"""--""",0,"""VU""","""U""",200,5.1
5.1,"""U""",0,"""N""","""--""",0,0
0,"""--""",0,"""H""","""--""",100,21.4
0,"""--""",0,"""VU""","""U""",200,6.5
0,"""--""",0,"""VD""","""U""",100,11.5
0,"""--""",0,"""N""","""--""",0,0
0,"""--""",0,"""VU""","""U""",100,11.5
0,"""--""",0,"""VD""","""U""",100,11.5
0,"""--""",23.1,"""N""","""--""",0,0
0,"""--""",0,"""VU""","""U""",100,3.6
0,"""--""",0,"""VD""","""U""",100,3.6
0,"""U""",120.8,"""N""","""--""",0,0
0,"""--""",0,"""H""","""--""",12,30.9
"Test4.pll"
I cant quite tell from your question but if all you are trying to do is replace triple quotes and with single quotes then you can remove the entire do loop and just put
string2=replace(string2,"""""""","""")
inside the for loop to replace contents of the entire string rather than just each character.
So
For X = 1 To 56
String2 = Sheets(1).Cells(X, 1).value
String2 = Replace(String2, """""""", """")
Sheets(1).Cells(X, 1).value = String2
Next
or even
For X = 1 To 56
Sheets(1).Cells(X, 1).value = Replace(Sheets(1).Cells(X, 1).value, """""""", """")
Next
EDIT
You'll also want to use .value for getting the cell value
I am a novice at Excel VBA and am running into an error 1004 while compiling a portion of the code:
Cells(i, j).Formula = _"=vlookup(Cells(i,1).Value,SKULifeCycle_Table_Temp!R1C1:R5000C500,match(Cells(1,j).Value,'SKULifeCycle_Table_Temp'!R1C1:R1C500,0),0)"
i and j have been defined previously as integers and are part of a for loop. Could anyone please help me out on this?
A few things wrong there:
First, you're using the literal text "Cells(i, 1).Value" and "Cells(1, j).Value" in the formula string. You would need to concatenate the values into the string like this:
Cells(i, j).FormulaR1C1 = "=vlookup(" & Cells(i,1).Value & ",SKULifeCycle_Table_Temp!R1C1:R5000C500,match(" & Cells(1,j).Value & ",'SKULifeCycle_Table_Temp'!R1C1:R1C500,0),0)"
Second, you used the .Formula property but passed R1C1 style references, so you should use the .FormulaR1C1 property (as I did above).
Third, if the values in the cells that you are using for the lookup values are text, you need to enclose them in quotes:
Cells(i, j).FormulaR1C1 = "=vlookup(""" & Cells(i,1).Value & """,SKULifeCycle_Table_Temp!R1C1:R5000C500,match(""" & Cells(1,j).Value & """,'SKULifeCycle_Table_Temp'!R1C1:R1C500,0),0)"
or use the addresses (in R1C1 format) instead:
Cells(i, j).FormulaR1C1 = "=vlookup(RC1,SKULifeCycle_Table_Temp!R1C1:R5000C500,match(R1C,'SKULifeCycle_Table_Temp'!R1C1:R1C500,0),0)"
One issue that is clear (maybe the cause of the whole error), is that variables are being used within a string. So, what does that mean? I'll simplify using the code below as a template:
Sub Test1()
Dim i as Integer
Dim j as Integer
i = 2
j = 3
Cells(i, j).Value = "The row number is i and the column number is j"
End Sub
In the Cells line, it correctly refers to cell C2, which is the second row and third column. But what value is inserted into the cell? It's literally "The row number is i and the column number is j" using the letters, and not their corresponding numbers. Similarly, in your case, the formula will read: VLOOKUP(CELLS(i,1)... using the letter i, which is incorrect.
Additionally, a cell's formula doesn't use the CELLS syntax. You instead need to build a string using the variables. It would look a lot like:
"=VLOOKUP(" & Cells(i, 1).Address & "SKULifeCycle_Table_Temp!R1C1:R5000C500, Match(" & Cells(i, 1)Address ... "
Try to build out your formula string and refer back to this post with any specific questions.
I need my macro to input the following formula:
Worksheets("U_NEDC_COLD_online_0").Cells(3, A).formula = "=IF(" & Worksheets (U_NEDC_COLD_online_0).Cells(3, AA).Value & "=" & Worksheets(U_NEDC_COLD_online).Cells(3, AA).Value & ";" & Worksheets(U_NEDC_COLD_online).Cells(3, A).Value & ";" & Worksheets(U_NEDC_COLD_online_0).Cells(3, A).Value & ")"
I've also tried the same formula with the ".Address" property and it doesn't work. What am I doing wrong?
The final formula should look like this:
=IF($AA3 = U_NEDC_COLD_online!$AA3; U_NEDC_COLD_online!A3; U_NEDC_COLD_online_0!A3)
PS: Worksheets("U_NEDC_COLD_online_0") is not the same as Worksheets(U_NEDC_COLD_online_0). (its not a typo)
Thanks.
Use commas instead of semicolons to separate the arguments of the IF statement.
Your formula appears to have a circular relationship; the formula is going into cell A3 and references cell A3... I'll leave that to you to work out.
Always use Option Explicit. Once you put that line at the top of your module, you'll get the error: Variable not defined for the variables: A, AA, and possibly U_NEDC_COLD_online and U_NEDC_COLD_online_0 that you probably intended to use as string literals. (see next bullet for a workaround)
If U_NEDC_COLD_online_0 is truly a variable/constant name and is not equal to the string literal that you use elsewhere (e.g. U_NEDC_COLD_online_0), you should really change the variable name to something else!
If the only reason you are dynamically building the formula is to accommodate variable row indexes, use this where the 3 can be replaced by a variable:
Worksheets("U_NEDC_COLD_online_0").Cells(3, 1).Formula = "=IF($AA" & 3 & "=" & "U_NEDC_COLD_online!AA" & 3 & ", U_NEDC_COLD_online!A" & 3 & ", A" & 3 & ")"
If you don't even need variable row indexes, just use this:
Worksheets("U_NEDC_COLD_online_0").Range(A3).Formula = "=IF($AA3=U_NEDC_COLD_online!AA3, U_NEDC_COLD_online!A3, A3)"