setting a range from Active cell to last cell when Active cell change columns

Alroj

New Member
Joined
Jan 12, 2016
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hi Community,

I have this macro that I modified from a previous post. The macro finds a text , in this case, "ABC" and, then from this active cell down to the last cell, it deletes entire rows that contains a value of "1" or "2"
At the moment, the text "ABC" is located in column G but sometimes it is located in another column.

Ideally this macro will operate from the active cell down to the last row (yes, there are black cells down the range). At the moment I have locked column ":g" in the macro below. However, sometimes is column "p" or any other column. Would someone please assist to make this part dynamic? Much appreciated!!



VBA Code:
Sub DeletefromActiveCell()

LastRow = Cells(Rows.Count, "d").End(xlUp).Row

  Range("a1:r200").Select
 Selection.Find(what:="ABC", LookIn:=xlValues).Select

 For Each Cell In Range(ActiveCell.Address & ":g" & LastRow)
 If Cell.Value = "1" Or Cell.Value = "2" Then
 Cell.EntireRow.Delete
 End If
 Next


End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How many times would "ABC" be in the used range? More then once?
 
Upvote 0
Try this.
If you're happy with it, change the ".Interior.Color = vbGreen" to ".Delete"
Code:
Sub Maybe()
Dim rw As Long, col As Long, i As Long
rw = ActiveSheet.UsedRange.Find("ABC", , , 1).Row
col = ActiveSheet.UsedRange.Find("ABC", , , 1).Column
    For i = Cells(Rows.Count, col).End(xlUp).Row To rw Step -1
        If Cells(i, col).Value = CStr(1) Or Cells(i, col).Value = CStr(2) Then Cells(i, col).EntireRow.Interior.Color = vbGreen
    Next i
End Sub
 
Upvote 0
Thank yoy for your assistance

Apologies I didn't explain well.

"ABC" is just the key value of the cell and it is unique in the sheet. Once I find "ABC" this cell becomes the "Active Cell". In a way "ABC" is the heading and anything that goes below with value "1" or "2" would be deleted. The detail here is that sometimes there are 50 rows below "ABC" but sometimes is 10 or 100 and within this range some cells could be empty.
 
Upvote 0
Try this:

VBA Code:
Sub DeletefromActiveCell()
  Dim f As Range
  
  Application.ScreenUpdating = False
  Set f = Cells.Find("ABC", , xlValues, xlWhole, , , False)
  If f Is Nothing Then
    MsgBox "ABC not exists"
  Else
    Range("A" & f.Row, Cells(Rows.Count, f.Column).End(3)).AutoFilter f.Column, 1, xlOr, 2
    ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
    Range("A" & f.Row).AutoFilter
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Did you try the code from Post #3?

Hi Jolivanes,

I test it and it worked very well. Thank you!!
One question, How would the macro be adjusted to delete only value of "1" or "2" that has been formatted as "general" and not as "numbers"?
 
Upvote 0
Try this:

VBA Code:
Sub DeletefromActiveCell()
  Dim f As Range
 
  Application.ScreenUpdating = False
  Set f = Cells.Find("ABC", , xlValues, xlWhole, , , False)
  If f Is Nothing Then
    MsgBox "ABC not exists"
  Else
    Range("A" & f.Row, Cells(Rows.Count, f.Column).End(3)).AutoFilter f.Column, 1, xlOr, 2
    ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
    Range("A" & f.Row).AutoFilter
  End If
  Application.ScreenUpdating = True
End Sub

Gracias DanteAmor, ha!
It also worked very well. Good to know that there are other ways of getting the result I wanted.
Much appreciated!!
 
Upvote 1
Re Post #7
What happened when you tried it?

Code:
If Cells(i, col).Value * 1 = 1 Or Cells(i, col).Value * 1 = 2 Then Cells(i, col).EntireRow.Delete Shift: = xlUp
or
Code:
If CDbl(Cells(i, col).Value) = 1 Or CDbl(Cells(i, col).Value) = 2 Then Cells(i, col).EntireRow.Delete Shift: = xlUp
 
Upvote 0
Re Post #7
What happened when you tried it?

Code:
If Cells(i, col).Value * 1 = 1 Or Cells(i, col).Value * 1 = 2 Then Cells(i, col).EntireRow.Delete Shift: = xlUp
or
Code:
If CDbl(Cells(i, col).Value) = 1 Or CDbl(Cells(i, col).Value) = 2 Then Cells(i, col).EntireRow.Delete Shift: = xlUp

Jolivanes,

Thank you for your reply.
I tried both options as per your last response and I got the same error message in each case: Run-Time error '13', Type mismatch
 
Upvote 0

Forum statistics

Threads
1,222,684
Messages
6,167,630
Members
452,124
Latest member
lozdemr

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