I'm trying to use the formula FIND in order to search for the position of the first character # from the text on my cell A1 as: FIND("#";A1) using the following the code:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
Range("Q" & i).Formula = "=FIND(" & "#" & "," & cel & ")"
Next i
Any help to solve the problem of my code is highly appreciated
Thanks in advance
You could use Instr() function to directly write searched character position:
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
Range("Q" & i).Value = Instr(Range("A" & i).Value2, "#")
Next
Related
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
In excel vba, i am trying to update a cell value based on vlookup on multiple columns. As per the suggestion online i tried using index/match function of vba but somehow it does not works.
ActiveCell.Offset(0, 6) = Application.WorksheetFunction.Index(ExWs.Range("I:I"), _
Application.WorksheetFunction.Match(inv, ExWs.Range("B:B"), 0), _
Application.WorksheetFunction.Match("Planning Readiness Tollgate", ExWs.Range("H:H"), 0) _
, 0)
If in the above code I keep only one conditions things are working fine. Please help !!
Also I am not allowed to update anything on the lookup sheet, it's read only.
Thanks in Advance.
Regards,
Bhavesh Jain
An alternative approach would be to use the Evaluate method. Assuming that Column B contains numerical values, try...
Dim LastRow As Long
With ExWs
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
ActiveCell.Offset(0, 6).Value = Evaluate("INDEX('" & .Name & "'!I2:I" & LastRow & ",MATCH(1,IF('" & .Name & "'!B2:B" & LastRow & "=" & inv & ",IF('" & .Name & "'!H2:H" & LastRow & "=""Planning Readiness Tollgate"",1)),0))")
End With
However, if Column B contains text values, you'll need to enclose the criteria within quotes. If so, try the following instead...
Dim LastRow As Long
With ExWs
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
ActiveCell.Offset(0, 6).Value = Evaluate("INDEX('" & .Name & "'!I2:I" & LastRow & ",MATCH(1,IF('" & .Name & "'!B2:B" & LastRow & "=""" & inv & """,IF('" & .Name & "'!H2:H" & LastRow & "=""Planning Readiness Tollgate"",1)),0))")
End With
Note that the Evaluate method has a 255 character limit.
I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"
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
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