Easy Macro Question

seanbanks

New Member
Joined
Apr 8, 2009
Messages
12
Hi all,

I'm trying to figure out a macro and I'm sure its an easy fix but I can't figure it out. I need to delete all rows when a specific column has a blank cell. The problem is that column isn't always the same. But it always has the same name, "keep_it".

In summary, I need to find a column "keep_it" and delete all rows in which "keep_it" has blank values, but in some worksheets "keep_it" is in column C (for example) and in others it is in a different location.

Thank you in advance,
Sean
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Keep_It exists in Row 1 in an unkown column right?

This will do it on the active sheet.
Need more details about where the sheets are if you want to make it loop over several sheets...
Code:
Sub Delete_Keep_It()
 
MyCol = Application.Match("Keep_It",Range("1:1"),0)
Columns(MyCol).SpecialCells(xlcelltypeblanks).EntireRow.Delete
 
End Sub
 
Upvote 0
i know why you got that error the code was for the columns being namded keep_it click on your column and in the name box in the left hand side type in Keep_it and hit enter . then the code will work. click on the "I" column in the workbook i sent you you will see in the left side a dropdown boxes with it in it.
 
Upvote 0
sean

try this code: It will find the column where "keep_it" exists in an active sheet and delete all records that do not have the value "keep_it" in such column.

Hope this helps!

Code:
Sub test_()

Dim c As Variant

    Dim cell1, cell2, rng, newrng As Range
    Dim frstrow, lstrow As Long
    
    frstrow = ActiveSheet.UsedRange.Rows.Count
    lstrow = ActiveSheet.UsedRange.Columns.Count
    
    Set cell1 = Cells(1, 1)
    Set cell2 = Cells(frstrow, lstrow)
    Set rng = Range(cell1, cell2)
    
For Each c In rng

If c.Value = "keep_it" Then

mycolumn = c.Column

End If
Next c

Set newrng = Range(Cells(frstrow, mycolumn), Cells(lstrow, mycolumn))

For iRow = frstrow To lstrow Step -1

If Cells(iRow, mycolumn).Value <> "keep_it" Then

Cells(iRow, mycolumn).EntireRow.Delete

End If

Next iRow
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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