Find last row based on a value, then clear only specific Columns

julhs

Active Member
Joined
Dec 3, 2018
Messages
407
Office Version
  1. 2010
Platform
  1. Windows
I’m trying to “Find” last row based on a value; then clear only specific Columns below it.

I’m using this code to find the specific last row:-
(which it does)
VBA Code:
lrow = sht.Range("T:T").Find(What:="Bla Bla ", LookIn:=xlValues, LookAt:=xlWhole).Row
I then want to clear the contents of SPECIFIC columns from THAT row ( +1 ) down.
eg: “Find” row = Row 50, then clear the contents of ONLY Columns X & Y from Row 51 down to end of sheet

I simply can’t marry up the “Find” row and the clearing of specified columns (+1) below it.
Any help is always greatly appreciated
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
VERY bazar.
I am using:-
VBA Code:
lrow = sht.Range("T:T").Find(What:="Bla Bla", LookAt:=xlWhole).Row
in several different situations, without any issues.
This is a test run Sub I put in.
VBA Code:
Sub ClearRows()
Dim lrow As Long
Dim rng As Range
Dim sht As Range
lrow = sht.Range("T:T").Find(What:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
Range("ar" & lrow + 1 & ":as" & lrow + 1).ClearContent ‘ x & y substituted with ar:as
End Sub
But it is bugging out on:-
VBA Code:
lrow = sht.Range("T:T").Find(What:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
Which I really don’t understand!!!I, I have half dozen Subs that have same Declared variables and use the same
lrow “Find” and they run WITHOUT any problem.
Any suggestions as to why??
 
Upvote 0
VERY bazar.
I am using:-
VBA Code:
lrow = sht.Range("T:T").Find(What:="Bla Bla", LookAt:=xlWhole).Row
in several different situations, without any issues.
This is a test run Sub I put in.
VBA Code:
Sub ClearRows()
Dim lrow As Long
Dim rng As Range
Dim sht As Range
lrow = sht.Range("T:T").Find(What:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
Range("ar" & lrow + 1 & ":as" & lrow + 1).ClearContent ‘ x & y substituted with ar:as
End Sub
But it is bugging out on:-
VBA Code:
lrow = sht.Range("T:T").Find(What:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
Which I really don’t understand!!!I, I have half dozen Subs that have same Declared variables and use the same
lrow “Find” and they run WITHOUT any problem.
Any suggestions as to why??
sht is declared as a range, but should be a worksheet. Something like Dim sht as worksheet: Set sht = Activesheet would be the correct variable declaration.
 
Upvote 0
ONCE I declared sht as Worksheet and “Set sht= ThisWorkbook.ActiveSheet!!!! (Mistake I make ONLY to often)
It didn’t bug out!!
Unfortunatley as it stands; it is ONLY clearing the 2nd row of the range in AR:AS (so 2nd row below “Find” row but nothing else)
 
Upvote 0
Sorry, I misread what you were trying to achieve. You'd want to first find the row beneath the Cash Paid in T and then the true last row in that dataset. Try the below out:

VBA Code:
Sub ClearRows()

'sheet declaration
Dim sht As Worksheet: Set sht = ActiveSheet
'finds the row beneath target row with Cash Paid in column T
Dim fRow As Long: fRow = sht.Range("T:T").Find(What:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row + 1
'finds actual last row based on column T
Dim lRow As Long: lRow = sht.Cells(sht.Rows.Count, "T").End(xlUp).Row

sht.Range("AR" & fRow & ":AS" & lRow).ClearContents

End Sub
 
Upvote 0
Solution
Excellent, thank you.

Just had to slightly alter;
VBA Code:
Dim fRow As Long: fRow = sht.Range("T:T").Find(What:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row + 1
To;
VBA Code:
Dim fRow As Long: fRow = sht.Range("T:T").Find(What:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
And;
VBA Code:
sht.Range("AR" & fRow & ":AS" & lRow).ClearContents
To;
VBA Code:
sht.Range("AR" & fRow & ":AS" & lRow).Clear
(As “.ClearContents” left the “Comments” but “.Clear” took “Comments” as well)
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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