Adding formula to a cell and referencing to variables - excel

I am trying to get a formula into a cell with variables from VBA, but I'm getting another Application or Object defined error. The error is triggered on the first row op Range("R" & i).FormulaR1C1 = "=Q" & i & "/" & DollarRate. I have tried using just Formula instead of FormulaR1C1.
Columns Q and R are empty. Column Q will contain values in € and Column R will show the value in $.
Dim LastRow As Integer, i As Integer
Dim DollarRate As Double
DollarRate = InputBox("Enter dollar rate:", "Dollar rate")
Range("Q1").Value = "$ POS"
Range("R1").Value = "€ POS"
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
Range("R" & i).FormulaR1C1 = "=Q" & i & "/" & DollarRate
Next i

Try replacing this:
For i = 2 To LastRow
Range("R" & i).FormulaR1C1 = "=Q" & i & "/" & DollarRate
Next i
with this:
Range("R2:R" & lastrow).FormulaLocal = "=Q2/" & DollarRate

Related

VBA: Calculating the value at every row, but variables change depending on one column

I would like to create a simple formula to calculate the net change on a certain row, and print it in the same row (column H). But what I plug into the formula will depend on the value at each row's column A.
This is the code that I tried:
Sub totalPnL()
Dim LR As Long
LR = Range("D" & Rows.Count).End(xlUp).Row
If Range("A2:A" & LR).Value = "WIN" Then
Range("H2:H" & LR).Formula = "=ABS(D2-F2)*G2"
ElseIf Range("A2:A" & LR).Value = "LOSS" Then
Range("H2:H" & LR).Formula = "=-ABS(D2-E2)*G2"
End If
Range("T2") = Application.WorksheetFunction.Sum(Range("H:H"))
End Sub
I feel something is wrong with the If statement, but I'm not sure how to edit it
Thanks for all the help
As written in the comment a formula will be easier and quicker but if you want to have your code "fixed" then the following code would do it.
Sub totalPnL()
Dim LR As Long, i As Long
LR = Range("D" & Rows.Count).End(xlUp).Row
' the original statement
' Range("A2:A" & LR).Value = "WIN"
' cannot work as Range("A2:A" & LR) will be an array in case LR > 2
' in this case you have to loop
' AGAIN: usage of a formula will be better
For i = 2 To LR
If Range("A" & i).Value = "WIN" Then
Range("H" & i).FormulaR1C1 = "=ABS(RC[-4]-RC[-2])*RC[-1]"
ElseIf Range("A" & i).Value = "LOSS" Then
Range("H" & i).FormulaR1C1 = "=ABS(RC[-4]-RC[-2])*RC[-1]"
End If
Next i
Range("T2") = Application.WorksheetFunction.Sum(Range("H:H"))
End Sub
To make the code easier to read you could use a Select statement
For i = 2 To LR
Select Case Range("A" & i).Value
Case "WIN"
Range("H" & i).FormulaR1C1 = "=ABS(RC[-4]-RC[-2])*RC[-1]"
Case "LOSS"
Range("H" & i).FormulaR1C1 = "=ABS(RC[-4]-RC[-2])*RC[-1]"
End Select
Next i
Another formula you could use would be
=IF(A2="WIN";1;(IF(A2="LOSS";-1)))*ABS(D2-F2)*G2

How can I create a macro to autofill data from the top row down to the last adjacent column with data in it?

I need to create a macro that can take data in columns F through I and autofill it down to the last cell in column E that has data in it. The code below almost does what I need it to do. However, I want to be able to use this macro with different data further down in columns F:I
Dim lastrow As Long
lastrow = Range("e" & Rows.Count).End(xlUp).Row
Dim x(1 To 4) As Variant
With ThisWorkbook.Sheets("sheet1")
x(1) = Range("f2")
x(2) = Range("g2")
x(3) = Range("H2")
x(4) = Range("I2")
.Range("F3:i3").Formula = x
.Range("f3:i" & lastrow).FillDown
End With
Like this:
With ThisWorkbook.Sheets("sheet1")
lastrow = Range("E" & Rows.Count).End(xlUp).Row
For i = 3 To lastrow
.Range("F" & i).Formula = .Range("F2").Formula
.Range("G" & i).Formula = .Range("G2").Formula
.Range("H" & i).Formula = .Range("H2").Formula
.Range("I" & i).Formula = .Range("I2").Formula
Next i
End With

VBA excel - range.formula issue

There is a particular part of my code which I cannot make work,
I'm trying to do the following command on VBA =RIGHT(LEFT(X1;Z1-2);LEN(LEFT(X1;Z1-2))-FIND(":";X1))
On cell X1, there is a text: RESULTS:NG & MODEL:IJ
My VBA code is:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "Y" & i
cel3 = "Z" & i
cel4 = "X" & i
Range("M" & i).Formula = "=RIGHT(LEFT(" & cel4 & "," & cel3 & "-" & 2 & "),LEN(LEFT(" & cel4 & "," & cel3 & "-" & 2 & "))-FIND(:" & cel4 & "))"
Next i
I'm open for a better approach for this issue as well
Thanks in advance
Try writing all the formulas at once and reduce using quotes within the formula as much as possible.
Range(Cells(1, "M"), cells(lr, "M")).Formula = _
"=RIGHT(LEFT(X1, Z1-2), LEN(LEFT(X1, Z1-2))-FIND(char(58), X1))"
All range and cells reference within a sub procedure are better with a properly defined parent worksheet reference.
dim lr as long
with worksheets("sheet1")
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range(.Cells(1, "M"), .cells(lr, "M")).Formula = _
"=RIGHT(LEFT(X1, Z1-2), LEN(LEFT(X1, Z1-2))-FIND(char(58), X1))"
end with

VBA Range.Formula issue

Im confused with the range.formula on excel VBA,
Im trying to use the formula RIGHTas follows on my VBA code:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("R" & i).Formula = "=RIGHT("cel", "cel2" & "" - "" & "cel3")"
The last row of my code, I trying to do right(A1, P1-Q1)
However, Im getting syntax error and Im struggling to solve
Thanks in advance
You were very, very close:
Sub marine()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("R" & i).Formula = "=RIGHT(" & cel & "," & cel2 & " - " & cel3 & ")"
Next i
End Sub

Run Time Error 1004 SumIf function

I'm trying to use the Sumif function in VBA, however, I'm getting a run-time error. The error started when I tried to add the variable CritLastCol into the formula. If I replace that with an actual cell reference it works. I want to be able to autofill the rows below this and my criteria will change based on the row. In other words my sum range and criteria range are fixed and my criteria is the cell to the left of my activecell (but not one over, it could be 20, 30 columns over).
Sub SumBydimcode()
Dim lastCol As Integer
Dim CritLastCol As String
lastCol = Cells(3 & Columns.Count).End(xlToLeft).Column
lastRow = Range("A" & Rows.Count).End(xlUp).Row
NewLastRow = Range("I" & Rows.Count).End(xlUp).Row
CritLastCol = ActiveCell.End(xlToLeft).Value
ActiveCell.FormulaR1C1 = "=SUMIF(R3C8:R" & lastRow & "C8," & CritLastCol & ",R3C10:R" & lastRow & "C" & lastCol & ")"
ActiveCell.AutoFill Destination:=Range(ActiveCell.Address & ":J" & NewLastRow)
End Sub
You need to surround your CritLastCol variable in quotation marks. To do so in VBA, you use the double-quote twice "". As a result, your code would look like this:
ActiveCell.FormulaR1C1 = "=SUMIF(R3C8:R" & lastRow & "C8,""" & CritLastCol & """,R3C10:R" & lastRow & "C" & lastCol & ")"
Notice the extra double-quotes surrounding CritLastCol
Ok, I figured out a way to do it... Instead of autofill, I used a loop starting at the end of my data and working back up.
Sub SumBydimcode()
Dim lastCol As Integer
Dim CritLastCol As String
lastCol = Cells(3 & Columns.Count).End(xlToLeft).Column
lastRow = Range("A" & Rows.Count).End(xlUp).Row
NewLastRow = Range("I" & Rows.Count).End(xlUp).Row
NewFirstRow = lastRow + 7
S = ActiveCell.Row
If S > NewLastRow - NewFirstRow Then
For S = ActiveCell.Row To NewFirstRow Step -1
CritLastCol = ActiveCell.End(xlToLeft).Value
ActiveCell.FormulaR1C1 = "=SUMIF(R3C8:R" & lastRow & "C8,""" & CritLastCol & """,R3C10:R" & lastRow & "C" & lastCol & ")"
ActiveCell.Offset(-1, 0).Select
Next S
End If
End Sub

Resources