I recorded part of this macro in Excel:
Cells(x, i).Select
Selection.FormulaArray = _
"=SQRT((MMULT(MMULT(TRANSPOSE(R2C14:R9C14),'Monthly Covariance y1'!R[12]C[0]:R[19]C[7]),'Portfolio Vola per Month'!R2C14:R9C14)))"
In the middle term " 'Monthly Covariance y1'!R[12]C[0]:R[19]C[7])" I want to express the bold numbers as variables. 0 should be j and 7 should be j+7.
When I try to replace the hard numbers by variables, Excel returns "Unable to set FormulaArray property of the Range class".
Any idea on how I can work around this problem?
Many thanks.
Just close the formula, add variables, and open again:
"=SQRT((MMULT(MMULT(TRANSPOSE(R2C14:R9C14),'Monthly Covariance y1'!R[12]C["& j & "]:R[19]C["& j + 7 & "]),'Portfolio Vola per Month'!R2C14:R9C14)))"
By "close the formula", I mean close with a ", then use & to connect the variables, then "reopen" with ". Simple example, with j being a row number, say 5:
myRange.FormulaR1C1 = "=Sum(A" & j & ":A" & j & ")"
is equivalent to
myRange.FormulaR1C1 = "=Sum(A5:A5)"
Also, this is another issue altogether, but try to avoid using .Select
Related
Cells(intRow, 2 + j).Formula = "=SUM(C" & intRow & ":" & colName & intRow & ")"
This is the code I am using to put a basic SUM-functionality into the cells at the end of certain rows. Upon entering this formula in the cell, the cell shows "#NAME?". If i click into the formula and then press "Enter", the formula works as intended (the formula input is also correct).
Is there a way to make VBA update these cells automatically, so the formula works as soon as it's entered into the cell by VBA?
Edit: Example values for the variables are:
intRow = 5
j = 7 (Column G)
colName = H (refers to j + 1)
So the finished formula in cell K5 would be:
=SUM(C5:J5)
Solved: Using
.FormulaR1C1
does not cause the same error.
Cells(intRow, 2 + j).FormulaR1C1 = "=SUM(RC[" & -(j - 1) & "]:RC[" &
(-1) & "])"
I just had the same problem, for a different reason, but I think it may help someone else :
It was due to a problem of localization (language)
I'm a French user and I was trying the following code :
For Each cel In selectedRange.Cells
cel.FormulaR1C1 = "=SI(R[0]C[-1]=1,1,0)"
Next cel
Where SI corresponds to the formula IF in English.
The problem is that FormulaR1C1 accept only English Formula: so to fix it I had to use :
For Each cel In selectedRange.Cells
cel.FormulaR1C1 = "=IF(R[0]C[-1]=1,1,0)"
Next cel
Otherwise I can also use my French formula thanks to FormulaR1C1Local such as :
For Each cel In selectedRange.Cells
cel.FormulaR1C1Local = "=SI(L(0)C(1)=1;1;0)"
Next cel
(note that L/C are for french words : Ligne/Colonne, that I needed to use the separator ; , that brackets are replaced by parenthesis in the Excel's French world ..)
I have a script that needs to place a formula into a cell but I'm getting a 1004 error from the first part I am sure I formatted something wrong. I had difficulty with the " marks in the string but got those worked out so I'm figuring I'm missing something else. The cells are also unprotected.
Worksheets(CurSheet + 1).Range("D" & Y).Value = "=IF(D52=1,0,IF(C52=" & """Saturday""" & ",0,'" & CurSheet & "!C" & Y & "))"
This is the section that gives the error. If it is removed code works.
"=IF(D52=1,0,IF(C52="
I am not sure what I am doing wrong with this part.
It looks like you're using CurSheet as a sheet index number and as a sheet name.
The index number just returns the relative position of the sheet in the workbook while the name is what you see on the sheet tab (there's also the CodeName but I won't go into that here).
Although I don't fully understand what you're after this code will place a formula on the sheet identified with the sheet index number, so if CurSheet= 1(+1) it will place the formula on the second sheet.
The formula itself will reference the name of the sheet that is before the sheet that the formula appears on (so if the formula is on the second sheet, the formula will reference the first sheet).
Sub Test()
Dim Y As Long
Dim CurSheet As Long
Y = 1
CurSheet = 1
Worksheets(CurSheet + 1).Range("D" & Y).Formula = _
"=IF(D52=1,0,IF(C52=" & """Saturday""" & ",0,'" & Worksheets(CurSheet).Name & "'!C" & Y & "))"
End Sub
Hope I made that clear enough. :)
You need to declare that you are inputting a formula, not a value:
Change:
Worksheets(CurSheet + 1).Range("D" & Y).Value
To:
Worksheets(CurSheet + 1).Range("D" & Y).Formula
While working on a macro in VBA I can't figure out the following issue:
This is a part of the code
ActiveCell.FormulaR1C1 = _
"=SUM(CreateResultsDebit!C[34])-DebitAnalysis!RC[-1]"
Range("B3").Select
ActiveCell.FormulaR1C1 = _
"=SUM(CreateResultsDebit!C[35])-DebitAnalysis!RC[-1]"
Range("B4").Select
Now, I have hardcoded the column reference C[34] and C[35]. Is there a way to use, for instance, an iterator as a variable between the brackets? If I try this myself it doesn't yield any result.
Thank you. Peter.
ok... you need to combine a variable and a string... it is like:
ActiveCell.FormulaR1C1 = "=SUM(CreateResultsDebit!C[" & i & "])-DebitAnalysis!RC[-1]"
to be a bit more specific: (in your code, you set the formula first and then select another cell... I assume that you want the C[34] in B2 and C[35] in B3......)
Dim i As Long
For i = 2 to 3
Range("B" & i).FormulaR1C1 = "=SUM(CreateResultsDebit!C[" & 32 + i & "])-DebitAnalysis!RC[-1]"
Next
In the first cycle (i = 2) it will go for cell B2 and use C[34] (32 + i). In the second cycle, those two values are 1 higher (if you go for i from 2 to 30, then it will go for cell B30 in last cycle and use C[62] in your formula)
EDIT
Another easy way is the use of R1C1 to set multiple cells. For this you would need to change your formula a bit and the code would look like this:
Range("B2:B9").FormulaR1C1 = "=SUM(INDEX(CreateResultsDebit!C[33]:C[51],,ROW()))-CreateResultsDebit!RC[-1]"
This would do the same without any loops ;)
Maybe this will be of help to you, if I understand you correctly. It will sum the columns from left to right and place them in one row consecutively.
Dim i as Integer
Dim iLastColumn as Long
Dim startRange as Range
Set startRange = Range("B3")
iLastColumn = Cells(1,Columns.Count).End(xlToLeft).Column
For i = 1 To iLastColumn
startRange.Offset(1,i).FormulaR1C1 = "=SUM(CreateResultsDebit!C[" & i & "])-DebitAnalysis!RC[-1]"
Next i
I am writing a small script and I would like to insert a function to a specific cell. I have tried the following piece of code:
end_time = ActiveCell.Offset(0, 2).Address
time_dif = "=TEXT(" + end_time + "-" + st_time + ";""[ωω]:μμ:δδ"")"
found = False
Workbooks("Template.xlsx").Worksheets(1).Range(ActiveCell.Offset(0, 3).Address).Select
'Var = ActiveCell.formula
ActiveCell.formula = time_dif 'Var
The last line in the above code is throwing an error:
'Application-Defined or Object-Defined Error'
The time_dif variable contains the following string:
"=TEXT($H$19-$H$13;"[ωω]:μμ:δδ")"
as seen in the local variables window
I have tried many different ways for the above to make it work but unfortunattely all of them failed.
What it actually worked is putting the exact same formula in another cell. Pause the execution an activate the cell with the formula. Put the formula in the variable Var and then move the activecell to the correct position and insert the variable Var value there.
Var = ActiveCell.formula 'Break point here / Run one step to take formula
ActiveCell.formula = Var 'Move to correct cell manually / Continue execution
Another thing I tried was removing the "=" from the formula and it puts the formula in the cell correctly as string.
Any advise or ideas about this?
The VBE uses English syntaxes.
Try using a sub to get the English syntax, e.g:
Sub ert()
InputBox Prompt:=" ", Default:=Selection.Formula
End Sub
The correct answer thus will be
"=TEXT(" + end_time + "-" + st_time + ",""[ωω]:μμ:δδ"")"
with the "," replacing the ";".
have you tried ActiveCell.FormulaR1C1 ?
Here you might change to include Chr(34), in your string, but as it seems to display correctly, I don't think that's the problem...
"=TEXT(" & end_time & "-" & st_time & ";" & Chr(34) & "[hh]:mm:ss" & Chr(34) & ")"
Pretty new to VBA but I'm learning quickly.
I'm finally getting used to using loops to perform repetitive tasks, and in this case, I want each pass through the loop to define a different variable. I'd be defining a list as a string and pushing a value to each part of the list for each loop.
Obviously, the number of loops is variable, so I need the end point of the defined list to be a variable. From what I've searched, that's not possible? I'm getting a "constant expression required" error.
Here is the code (lastRow is already defined):
NextAverage = 0
section = 1
Dim AverageSection(1 To section) As String
For section = 1 To PhraseSections
ActiveCell.Formula = "=MATCH(""average"",A" & NextAverage + 1 & ":A" & lastRow & ",0)"
Selection.Offset(1, 0).Select
ActiveCell.Formula = "=SUM(G1:G" & section & ")"
NextAverage = ActiveCell.Value
AverageSection(section) = ActiveCell.Value
Next section
Any help would be greatly appreciated!
Try using Redim Preserve:
Dim AverageSection() As String
For section = 1 To PhraseSections
Redim Preserve AverageSection(section)
...
Next section
Does this help?