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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Which line is highlighted when that happens?
Put the values between double quotation marks, as below, and see what happens

BTW, if cell values are numbers, the code should be
Code:
If Cells(i, col).Value = 1 Or Cells(i, col).Value = 2 Then Cells(i, col).EntireRow.Delete
If the values are text, the code should be
Code:
If Cells(i, col).Value = "1" Or Cells(i, col).Value = "2" Then Cells(i, col).EntireRow.Delete
To convert a value that could be a number, either use CDbl or multiply it by one (1)
 
Upvote 0
On an trial sheet, I formatted the used range as "General", ran the macro, formatted as "Text", ran the macro and formatted as "Number" and ran the macro.
With all three, it worked as advertised.
Code:
If Cells(i, col).Value = CStr(1) Or Cells(i, col).Value = CStr(2) Then Cells(i, col).EntireRow.Interior.Color = vbGreen
 
Upvote 0

Forum statistics

Threads
1,222,684
Messages
6,167,628
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