Link to home
Start Free TrialLog in
Avatar of Justin
Justin

asked on

DoLoop which populates column G with a Negative value if Number in Column E starts with 93.

Hi Guys, I am trying to do a Do Loop where I loop down Column E in a spreadsheet and if the number in the cell starts with "93",
it populates the cell 2 columns right of it in column G as a negative, eg: if Range "E2"  = 937019784 and "G2" = 1000. It populates G2 as -1000.
If I had a Formula it would be =IF(LEFT(E2,2) = "93", G2*-1,G2).
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When data is large, avoid looping through the range object instead use array like below...
Sub ChangeToNegative()
Dim lr As Long, i As Long
Dim x, y
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "E").End(xlUp).Row
x = Range("E2:E" & lr).Value
y = Range("G2:G" & lr).Value

For i = 1 To UBound(x, 1)
    If x(i, 1) <> "" And Left(x(i, 1), 2) = "93" Then y(i, 1) = y(i, 1) * -1
Next i

Range("G2").Resize(UBound(y, 1)).Value = y
Application.ScreenUpdating = True
End Sub

Open in new window

Why not use a formula?
Avatar of Justin
Justin

ASKER

superb!