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
Related
I'm trying to select differrents worksheets, based on a variable.
All this in a purpose to print those sheets into a pdf.
I've tried with arrays but i don't know much.
So I'm continuing with string and split.
But I have no chance too.
All is ok except the last codeline , the sheets selection.
I receive a Subscript OutOf range
Any advice?
Sub Print_Full_Report()
Call Clear_clipboard
Call Stop_Calcul_Screen
LastOverviewRow = WS_Rep_Overview.Cells(Rows.Count, 3).End(xlUp).Row
tabtoprint = ""
SelectedDate = Date + 90
For i = 5 To LastOverviewRow
If WS_Rep_Overview.Range("E" & i) < SelectedDate Then
Module = WS_Rep_Overview.Range("D" & i).Value
tabtoprint = tabtoprint & Chr(34) & Module & Chr(34) & "," & Chr(34) & Module & "-Cow" & Chr(34) & "," & Chr(34) & Module & "-SubSys" & Chr(34) & ","
End If
Next i
tabtoprint = Mid(tabtoprint, 1, Len(tabtoprint) - 1)
Sheets(Split(tabtoprint, ",")).Select
End Sub
Try this code:
Sub Print_Full_Report()
Const template = ",#,#-Cow,#-SubSys" ' for further use in the loop
' previous your code ...
' in the code some redundant variables have been removed
With WS_Rep_Overview
SelectedDate = Date + 90
For i = 5 To .Cells(Rows.Count, 3).End(xlUp).Row
If .Range("E" & i) < SelectedDate Then
tabtoprint = tabtoprint & Replace(template, "#", .Range("D" & i).Value)
End If
Next i
End With
tabtoprint = Mid(tabtoprint, 2) 'remove the leading comma
' for debug; check that it prints something like this: Module5,Module5-Cow,Module5-SubSys,Module6,Module6-Cow,Module6-SubSys,Module7,Module7-Cow,Module7-SubSys
Debug.Print tabtoprint
Worksheets(Split(tabtoprint, ",")).Select
End Sub
I have a piece of code that loops down a column and inserts a formula into each cell in that column. The code runs, the only problems is that each cell the formula is inserted into displays #Value! Does anyone know how to fix this?
Here is the piece of code with the problem:
Dim j As Long
'Loop down the rows in mainfile
For j = 2 To lastFullRow2
' Make each argument a string, then concatenate it all all into large string
Dim firstArgument As String
firstArgument = "ws_multidax.Range(" & valuecolumnLetter & "2:" & valuecolumnLetter & lastFullRow1 & ")"
Dim secondArgument As String
secondArgument = "ws_multidax.Range(" & parameter1columnLetter & "2:" & parameter1columnLetter & lastFullRow1 & ")"
Dim thirdArgument As String
thirdArgument = "ws_multidax.Range(" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ")"
Dim fourthArgument As String
fourthArgument = "ws_multidax.Range(" & parameter2columnLetter & "2:" & parameter2columnLetter & lastFullRow1 & ")"
Dim condition3 As String
condition3 = "ws_mainfile.Range(" & "D2:" & D & j & ")"
Dim patid1 As String
patid1 = "ws_multidax.Range(" & "D2:" & D & lastFullRow2 & ")"
With ws_mainfile
Dim commandstring As String
'The formula we want is a concatenated string
commandstring = "{=INDEX(" & firstArgument & ",MATCH(1,(" & secondArgument & "=" & condition1 & ")*(" & thirdArgument & "=" & condition2 & ")*(" & patid1 & "=" & condition3 & "),0))}"
'Insert the formula into each cell as the loop goes down the rows
ws_mainfile.Range("AN" & j).Formula = Eval(commandstring)
'ws_mainfile.Range("AN" & j).Formula = commandstring
End With
Next j
The #Value! gets put into the cells when I run the line with the Eval(commandstring). When I run the line with just formula = commandstring, the formula gets put in each cell, but it does not solve.
Your code has 2 problems:
You are putting in the curly braces of an array formula manually, don't do this
You are putting in an array formula as a regular formula
So to correct, do the following:
'1. Change this line:
commandstring = "{=INDEX(....)}"
'And simply remove the curly braces {} from its beginning and end
commandstring = "=INDEX(....)"
'2. Change this line:
ws_mainfile.Range("AN" & j).Formula = Eval(commandstring)
'To use the .FormulaArray property instead of just .Formula:
ws_mainfile.Range("AN" & j).FormulaArray = commandstring
You're on the right track. There's no need to use the eval function, as you have built a string formula in code. The problem is you have surrounded it with the braces, which excel treats as if you had entered text.
Try this instead:
commandstring = "=INDEX(" & firstArgument & ",MATCH(1,(" & secondArgument & "=" & condition1 & ")*(" & thirdArgument & "=" & condition2 & ")*(" & patid1 & "=" & condition3 & "),0))"
'Insert the formula into each cell as the loop goes down the rows
ws_mainfile.Range("AN" & j).Formula = commandstring
I'm trying to write vba code so that every time I type a number, it becomes a stock ticker after "HK Equity" is added by vba code. Then spreadsheet can return its RSI using Bloomberg function BTP. For example, when I type "1" in a cell, spreadsheet will return the RSI of stock "1 HK Equity" using Bloomberg function =BTP($D3,E$2,"RSI","TAPeriod=14","DSClose=LAST_PRICE","Per=d").
Here are my code:
Sub RSI()
Dim num As Integer
num = Sheets("sheet2").Cells(3, 2).Value
Dim numString As String
numString = CStr(num)
Dim ticker As String
ticker = numString + "HK Equity"
Dim rsiString As String
rsiString = Sheets("sheet2").Cells(4, 2).Value
Sheets("sheet2").Cells(5, 2).Value = "=BTP(" & ticker & "," & rsiString & "," & "RSI" & ", " & "TAPeriod=14" & "," & "DSClose=LAST_PRICE" & "," & "Per=d" & ")"
End Sub
When I run the code it says Run-time Error '1004': application-defined or object-defined error. And debugger says the last command has a problem (i.e. Sheets("sheet2").Cells(5, 2).Value = "=BTP....) What's wrong with this command? THX!!
May you check the output of BTP formula?
' =BTP(1 HK Equity,rsi string,RSI, TAPeriod=14,DSClose=LAST_PRICE,Per=d)
Sheets("sheet2").Cells(5, 2).Value = "=BTP(" & ticker & "," & rsiString & "," & "RSI" & ", " & "TAPeriod=14" & "," & "DSClose=LAST_PRICE" & "," & "Per=d" & ")"
seems you have miss all escape character
"=BTP(""" & ticker & """,""" & rsiString & """,""RSI"",""TAPeriod=14"",""DSClose=LAST_PRICE"",""Per=d"")"
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
I am trying to assign a formula to a cell whose reference changes at every iteration. The left-hand side of the assignment below, however, seems to work since if I make the right-hand side "=""Test""", it seems to work.
I guess the problem then is with my formula on the right hand side. There is a combination of strings, a variable that changes with each loop iteration and a value taken from a cell.
Worksheets("LimsOutput").Cells(4, 2 + 14 * i).Formula = "=" & BR & " Blah blah " & Worksheets("Lims").Range("A3").Value
I'm thinking that the problem may be to do with the quotes in the sheet and cell referencing.
A concatenate formula in an Excel cell would look like this in your case
= "String" & " Blah Blah " & "30/06/2012"
To convert that into code, use CHR(34) for double quotes and "&"
See this example
Worksheets("LimsOutput").Cells(4, 2 + 14 * i).Formula = "=" & _
Chr(34) & BR & Chr(34) & _
"&" & _
Chr(34) & " Blah blah " & Chr(34) & _
"&" & _
Worksheets("Lims").Range("A3").Value
One more thing :)
Worksheets("Lims").Range("A3").Value will give you 0.00248508946322068 for 30/06/2012
So the above code will give an output like STRING Blah blah 0.00248508946322068 To get the date in the end you have to add CHR(34) before and after the date as well. For example
Worksheets("LimsOutput").Cells(4, 2 + 14 * i).Formula = "=" & _
Chr(34) & BR & Chr(34) & _
"&" & _
Chr(34) & " Blah blah " & Chr(34) & _
"&" & _
Chr(34) & Worksheets("Lims").Range("A3").Value & Chr(34)
Below is a simple example. I hope this helps you.
Sub sample()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
Dim rng As Range
Set rng = Range("B1:B" & lastRow)
Dim BR As String
BR = "STRING"
rng.Formula = BR & " Blah blah " & cstr(Range("$A$3"))
End Sub