Quotes in VBA with R1C1 - excel

I need to write in the cells from Cells(1,1) to Cells(501,1) the formula
=FDSC("-",$C$3,"FG_PRICE(D1)") for line 1
=FDSC("-",$C$3,"FG_PRICE(D2)") for line 2
=FDSC("-",$C$3,"FG_PRICE(D3)") for line 3
Now my vba code is the following one :
Range(Cells(1, 1), Cells(1 + 500, 1)).FormulaR1C1 = "=FDSC(""" & "-" & """,R1C3,""" & " FG_PRICE(RC[3]) " & """)"
I've tested everypart of the code to determine that the last part (FG_PRICE...) is the one that is not working!
The formule that is written in Excel is :
=FDSC("-",$C$1,"FG_PRICE(RC[3])")
How can i do to have D1,D2,etc... and not RC[3]??? I've tried a few things but i can't find it!
Thank you very much for your time and your help!

Use chr(34) to represent quotation marks when you are already working inside quotation marks. For example, change your forumula above to the following:
Range(Cells(1, 1), Cells(1 + 500, 1)).FormulaR1C1 = "=FDSC(" & chr(34) & "-" & chr(34) & ",$C$1," & chr(34) & "FG_PRICE(D1)" & chr(34) & ")"
To put it in a loop, just do the following:
Sub writeFormulas()
Dim i as Integer
For i = 0 to 2
Range(Cells(1, 1), Cells(1 + 500, 1)).FormulaR1C1 = "=FDSC(" & chr(34) & "-" & chr(34) & ",$C$1," & chr(34) & "FG_PRICE(D" & i+1 & ")" & chr(34) & ")"
next i
End Sub

Related

how to write the formula in excel vba with use of special symbols

Twsht.Cells(Trow + 1, 15).Formula = "=IFERROR(VLOOKUP(M" & Trow + 1 & Chr(38) & "-" & Chr(38) & "N" & Trow + 1 & ",'Table Array'!$A$2:$D$26,4,FALSE)," & Chr(34) & Chr(34) & ")"
Getting below error message
"Run-Time error 1004"
Application-defined or object-defined error
if i paste below formula directly in cell it working
=IFERROR(VLOOKUP(M604&-N604,'Table Array'!$A$2:$D$26,4,FALSE),"")
use:
Twsht.Cells(Trow + 1, 15).Formula = "=IFERROR(VLOOKUP(M" & Trow + 1 & "&-N" & Trow + 1 & ",'Table Array'!$A$2:$D$26,4,FALSE),"""")"

VBA using part of formula as a celll value

Failing againg with my project
I have formulas with variable Brand that is changed dynamically (AF Column). Basically all I want is to extract Brands into a column next (AE) to the formula column for visial convenience
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Next i
Range("AF:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
ActiveSheet.Range("AG2:AG8").Formula = ActiveSheet.Range("AF2:AF8").Formula
ActiveSheet.Range("AH2:AH8").Formula = ActiveSheet.Range("AF2:AF8").Formula
Dim ws As Worksheet
Set ws = Worksheets(1)
Dim j As Variant
j = Application.Match(""" & Brand(i) & """, ws.Range("AF2:AF8"))
ActiveSheet.Range("AE2").Value = Application.Index(ws.Range("AF2:AF8"), j, 0)
And I get #N/A Already lost two days for that. Would be enourmously grateful to anyone who could help.
It's not exactly clear from your question as to your desired output but here's a guess:
For i = LBound(Brand) To UBound(Brand)
Range("AF" & i + 2).Formula = "=COUNTIFS(C:C," & RTrim(Month(Mesyaz3)) & _
",H:H,""Headphones"",F:F," & Chr(34) & Brand(i) & Chr(34) & ")"
Range("AE" & i + 2).Value = Brand(i)
Next i
Range("AE:AF").Sort Key1:=Range("AF2"), Order1:=xlDescending, Header:=xlYes
I've added a line to write the brand to AE, and altered the Sort to accommodate this.

VBA Excel formula compile error - expected end of statement

I am getting a this error:
Compile error: Expected: End of Statement
While trying to use an Excel formula in a VBA statement in the code below.
The error occurs on "FIND("for ","...
For i = 2 To lastRow
aa = ThisWorkbook.Sheets(3).Cells(i, "A").Text
ThisWorkbook.Sheets(3).Cells(i, "E").Formula = "=MID(aa,FIND("for ",aa)+4,FIND(" ",aa,FIND("for ",aa)+4)-(FIND("for ",aa)+4))"
Next i
It's a little long:
You need to add the " inside your formula, I like to use the Chr(34) to add it.
So first, your aa is a varaible that changes every time you advance i in the loop, so inside the formula you need to break the constant part and add " & aa & " every time you use it.
But, you need to add the " to aa, that's why I modifed your aa line to:
aa = Chr(34) & ThisWorkbook.Sheets(3).Cells(i, "A").Text & Chr(34)
Second, I added a String variable named Myfor, and set the value to it Myfor = Chr(34) & "for " & Chr(34), just to try to "shorten" the formula a little.
Code
Dim aa As String
Dim Myfor As String
Myfor = Chr(34) & "for " & Chr(34)
For i = 2 To lastRow
aa = Chr(34) & ThisWorkbook.Sheets(3).Cells(i, "A").Text & Chr(34)
ThisWorkbook.Sheets(3).Cells(i, "E").Formula = "=MID(" & aa & ",FIND(" & Myfor & "," & aa & ")+4, FIND("" ""," & aa & ",FIND(" & Myfor & "," & aa & ")+4)-(FIND(" & Myfor & "," & aa & ")+4))"
Next i

VBA coding using last used column number

I'm trying to write VBA code to sum up cells, in the process i calculate the latest updated column using the statement:
clncnt1 = wb.Worksheets("Sheet1").Range("XFD16").End(-4159).Column
Suppose I capture the clncnt1 as 20 which is column P. Now how do I use:
wb.Worksheets("sheet1").Range("A1").Offset(16, clncnt).Formula = "=sum(*P12:P13*)"
using the column number clncnt1 that I just captured?
replace your sum with below
"=sum(" & Cells(12, clncnt1 ).Address & ":" & Cells(13, clncnt1 ).Address & ")"
you are actually getting the addresses of the range and then concatenating it all together.
Full code:
clncnt1 = Worksheets("sheet1").Range("XFD16").End(-4159).Column
Worksheets("sheet1").Range("A1").Offset(16, clncnt1).Formula = "=sum(" & Cells(12, clncnt1).Address & ":" & Cells(13, clncnt1).Address & ")"
this worked wb.Worksheets("sheet1").Range("A1").Offset(11, clncnt + 3).Formula = "=sum(" & _ wb.Worksheets("sheet1").Range("A1").Offset(8, clncnt + 3).Address & ":" & _ wb.Worksheets("sheet1").Range("A1").Offset(9, clncnt + 3).Address & ")"

Inserting semicolon in between data in a cell in Excel

I have data in column A, C, E and G. I want column I to hold all of the data from these separated by semicolons, I have searched the web but all I find is how to replace line breaks with semicolons and that doesn't do it. Below are the 3 pieces of code I have attempted to work into it
Range("I" & i).Value = "=A" & i & Chr(59) & "&" & "C" & i & "&" & "E" & i & "&" & "G" & i
Range("I" & i).Value = "=A" & i & "&C" & i & "&E" & i & "&G" & i
Range("I" & i).Value = Range("A" & i).Value + ; + Range("C" & i).Value + ; + Range("E" & i).Value + ; + Range("G" & i).Value
The second line comes closest to what I want but when I add & ";" into it I get errors.
Any ideas thoroughly appreciated
SOLVED : This was solved by an answer below, I had managed my own fix which I will detail but the accepted answer is a cleaner, more efficient way
mystring = "=A" & i & "&" & Chr(34) & ";" & Chr(34) & "& C" & i & "&" & Chr(34) & ";" & Chr(34) & "& E" & i & "&" & Chr(34) & ";" & Chr(34) & "& G" & i
Range("I" & i).Value = mystring
You should use "&"";""".
In detail, you should use something like
"=A" & i & "&"";""" & "&C" & i
which result in
=A1&";"&C1
suppose i = 1.
=CONCATENATE(A1, ";", C1, ";", E1, ";", G1)
Using VBA
Range("A2").FORMULA = REPLACE("=CONCATENATE(A1, ';', C1, ';', E1, ';', G1)", "'", CHR(34))

Resources