Need to Select a Cell if its Adjacent Cell is Equal to 0

Modify_inc

Board Regular
Joined
Feb 26, 2009
Messages
77
Office Version
  1. 2016
Platform
  1. Windows
How can I tell Excel to select the A4 cell if its adjacent cell B4 is equal to 0, but only if the A4 cell is NOT empty? If A4 is empty, then disregard and check the next cells, A5 and its adjacent cell B5, and check for the same conditions, doing this all the way down to A50.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I was able to get this far using vba, but I obviously need to make it into a for loop or something to have it check the complete A4:A50 range.

VBA Code:
If Range("A4") <> "" And Range("B4") = 0 Then
   Range("A4").Select
   
End If

Any suggestions how to get it to check A4:A50?
 
Upvote 0
Formula in C4 would be
Excel Formula:
=IF(ISBLANK(B4),IF(ISBLANK(A4),"",A4),B4)

Is this what you want?
 
Upvote 0
Formula in C4 would be
Excel Formula:
=IF(ISBLANK(B4),IF(ISBLANK(A4),"",A4),B4)

Is this what you want?
Thanks for the suggestion, but no that is not what I was going for. I apologize if my post wasn't clear. Fortunately I was able to get it to work in VBA, which really surprised me since I've never done a for loop in vba.

This is the code. If you have any suggestions to improve it or a better alternative, as I'm still very much learning:
VBA Code:
Dim i As Integer

For i = 4 To 50

If Cells(i, "A").Value <> "" And Cells(i, "B").Value = 0 Then
    Cells(i, "A").Select
End If

Next i
 
Upvote 0
I presumed you want to write result in column C

VBA Code:
Dim i As Integer

For i = 4 To 50
    If Cells(i, "B") = 0 Then
        If Cells(i, "A") = "" Then
            ' Do nothing
        Else
            Cells(i, "C") = Cells(i, "A")
        End If
    Else
        Cells(i, "C") = Cells(i, "B")
    End If
Next i
 
Upvote 0
If you want the 1st occurrence of A being not blank & B being 0, try
VBA Code:
Dim i As Integer

For i = 4 To 50

If Cells(i, "A").Value <> "" And Cells(i, "B").Value = 0 Then
    Cells(i, "A").Select
    Exit Sub
End If

Next i
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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