VBA if statement depending on result of 2 columns

colbecd

New Member
Joined
Apr 30, 2009
Messages
25
Hi

Im trying to have text enter cell depending on the result of 2 cells in columns D & F being true

so if D2 = VM: and F >= 0 result in R2 = My text

so far I have the below code which keeps failing please help been stuck on this for 2 hours now :(

Sub IM()
Dim rng As Range
Dim I As Long
Set rng = Range("A2:A100")
For Each cell In rng
If Range("D2:D100") = "IM:" And Range("F2:F100") >= 0 Then??
cell.Offset(0, 15).Value = "My Text"
End If
Next
End Sub

There are 100 rows of data
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Code:
Sub IM()
Dim i As Long


Application.ScreenUpdating = False


For i = 2 To 100
    If Cells(i, 4) = "IM:" And Cells(i, 6) >= 0 Then Cells(i, 18) = "My Text"
Next i


Application.ScreenUpdating = True


End Sub
 
Upvote 0
Try changing to this, there maybe another way but it does work:

Code:
Sub IM()
Range("A2").Select
Do Until ActiveCell.Value = ""
If ActiveCell.Offset(0, 3) = "IM:" And ActiveCell.Offset(0, 5) >= 0 Then
ActiveCell.Offset(0, 15).Value = "My Text"
 End If
ActiveCell.Offset(1).Select
Loop
 End Sub
 
Upvote 0
Try changing to this, there maybe another way but it does work:

Code:
Sub IM()
Range("A2").Select
Do Until ActiveCell.Value = ""
If ActiveCell.Offset(0, 3) = "IM:" And ActiveCell.Offset(0, 5) >= 0 Then
ActiveCell.Offset(0, 15).Value = "My Text"
 End If
ActiveCell.Offset(1).Select
Loop
 End Sub


It's rarely advisable to select cells. Looping through an integer is much faster, and doesn't result in the activecell changing.
 
Upvote 0

Forum statistics

Threads
1,222,908
Messages
6,168,967
Members
452,228
Latest member
just4jeffrey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top