VBA Delete Data in cell A1 if B1 <> "Y" , loop through each cell in column A in a given range

Will85

Board Regular
Joined
Apr 26, 2012
Messages
240
Office Version
  1. 365
Platform
  1. Windows
I have a list of ever growing data, I can define the start and last row dynamicaly.

I want a macro that will go through each cell in Range("A2:A" & LastRow)

and delete the data in that cell if the corresponding cell in column B does not equal "Y"

Basically, each cell in Column A has an outstanding balance, each cell in Column B Is a drop down data validation List (the only option is "Y").

If a user has marked, for example, B512 as "Y", then do not delete the data in cell A512, otherwise clear the contents.

I want it to loop through each cell in the range and do this.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
How about:

VBA Code:
Sub TestCellDelete()
'
    Dim ArrayRow    As Long
    Dim SourceArray As Variant
'
    SourceArray = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)                     ' Save Columns A & B into an array
'
    For ArrayRow = 1 To UBound(SourceArray, 1)                                              ' Loop through the rows of the array
        If SourceArray(ArrayRow, 2) <> "Y" Then SourceArray(ArrayRow, 1) = vbNullString     '   If Column B value <> 'Y' then delete value in column A
    Next                                                                                    ' Loop back
'
    Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row) = SourceArray                     ' Write the array back to the sheet
End Sub
 
Upvote 0
Another option
VBA Code:
Sub Will()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if(" & .Offset(, 1).Address & "=""Y""," & .Address & ","""")")
   End With
End Sub
 
Upvote 0
Another option
VBA Code:
Sub Will()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if(" & .Offset(, 1).Address & "=""Y""," & .Address & ","""")")
   End With
End Sub
Pretty slick, thank you very much. Worked perfectly.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,772
Members
449,049
Latest member
greyangel23

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