View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
JLGWhiz JLGWhiz is offline
external usenet poster
 
Posts: 3,986
Default Delete rows of data in multiple sheets

His Yossy, Sorry to take so long to get back to you. It looks like the
problem is in the TargetCol = "A". If your "Title for Month" is not in
column A, then the object variable will not Set as a result of your Find
statement. That will leave f = Empty and result in fRow = f.Row throwing the
error message because f.Row cannot be found until f = a valid address. When
I put the Find criteria in column A the code ran as expected. When I moved
it to another column, it threw the error message. The easy way to fix it is
to eliminate the TargetCol variable and just use Sh.Cells(Find...., etc.
Below is the code that worked for me.

Sub DeleteData()
Dim fRow As Long
For Each Sh In ThisWorkbook.Sheets
With Sh
'If ActiveSheet.Name < sh.Name Then
'Sheets(sh.Name).Select
'End If
Set f = .Columns(TargetCol).Find(What:="Title for Month")
MsgBox f.Row
fRow = f.Row
LastRowToDelete = .Cells(fRow, TargetCol).End(xlDown).Row
.Range(.Rows(fRow + 1), .Rows(LastRowToDelete)).Delete
End With
Next
End Sub




"Yossy" wrote:

I still get th same error. Please what am i missing. Helppppp me. Thanks

"JLGWhiz" wrote:

You are getting the error message on fRow = f.Row because you did not Dim fRow.

Add at the top of your code: Dim fRow As Long

"Yossy" wrote:

Anyone know why I am getting this error - "Object Variable or with block
variable not set" Please help... I want to delete rows of data below "Title
of Month" in multiple sheets. Thus where there is "Title of Month in all
sheets, the code should look below the title and delete the data only in the
row beneath the title until the last data. In this case my data are in Column
A.

Sub DeleteData()
TargetCol = "A"
For Each sh In ThisWorkbook.Sheets
If ActiveSheet.Name < sh.Name Then
Sheets(sh.Name).Select
End If
Set f = Columns(TargetCol).Find(What:="Title for Month")
fRow = f.Row
LastRowToDelete = Cells(fRow, TargetCol).End(xlDown).Row
Range(Rows(fRow + 1), Rows(LastRowToDelete)).Delete
Next
End Sub


Thanks a big bunch