John_JingleHymerSmith
New Member
- Joined
- Jan 4, 2018
- Messages
- 4
Hi All,
I am still fairly new to Macros and VBA however I am learning from Mr. Excel's 2013 VBA and Macros book. I am attempting to write a macro (see below) that will Loop through the current workbook sheet by sheet and find the specific text to derive Top of Range and Bottom of Range, deliver the row values and then resize the top of the range value by the difference and delete the rows. I have gotten so far however when I attempt to incorporate an IF statement to account for errors (i.e. some worksheets in the workbook will not have the Find Text string I am looking for in either top or bottom) I get the error shown in the comments I placed below. I would like to understand why this is occurring and what am I doing wrong? As a kicker, I would also like this to then take the current worksheet copy to a new workbook then jump back and continue to loop through the sheets in the workbook. It may be more efficient to loop through all then perform the loop to open in new workbook? Appreciate the help and a learning moment!
Sub AutoDelete( )
Dim TopRow As Range
Dim BottomRow As Range
Dim wsCount As Integer
Dim i As Integer
wsCount = ActiveWorkbook.Worksheets.Count
For i = 1 To wsCount
With Worksheets(i)
Set TopRow = .Find("Latest Valuation", LookIn:=xlValues)
Set BottomRow = .Find("Key Areas of Focus", LookIn:=xlValues)
If Not TopRow Or BottomRow Is Nothing Then 'Error Object variable or With block variable not set
TopRowNum = TopRow.Row
BottomRowNum = BottomRow.Row
Worksheets(i).Resize(BottomRowNum - TopRowNum - 1).EntireRow.Delete
End If
Next i
'Additional code to follow to move all sheets finished in loop above to new workbook
End Sub
I am still fairly new to Macros and VBA however I am learning from Mr. Excel's 2013 VBA and Macros book. I am attempting to write a macro (see below) that will Loop through the current workbook sheet by sheet and find the specific text to derive Top of Range and Bottom of Range, deliver the row values and then resize the top of the range value by the difference and delete the rows. I have gotten so far however when I attempt to incorporate an IF statement to account for errors (i.e. some worksheets in the workbook will not have the Find Text string I am looking for in either top or bottom) I get the error shown in the comments I placed below. I would like to understand why this is occurring and what am I doing wrong? As a kicker, I would also like this to then take the current worksheet copy to a new workbook then jump back and continue to loop through the sheets in the workbook. It may be more efficient to loop through all then perform the loop to open in new workbook? Appreciate the help and a learning moment!
Sub AutoDelete( )
Dim TopRow As Range
Dim BottomRow As Range
Dim wsCount As Integer
Dim i As Integer
wsCount = ActiveWorkbook.Worksheets.Count
For i = 1 To wsCount
With Worksheets(i)
Set TopRow = .Find("Latest Valuation", LookIn:=xlValues)
Set BottomRow = .Find("Key Areas of Focus", LookIn:=xlValues)
If Not TopRow Or BottomRow Is Nothing Then 'Error Object variable or With block variable not set
TopRowNum = TopRow.Row
BottomRowNum = BottomRow.Row
Worksheets(i).Resize(BottomRowNum - TopRowNum - 1).EntireRow.Delete
End If
Next i
'Additional code to follow to move all sheets finished in loop above to new workbook
End Sub