Runtime Error 91

rtr1811

New Member
Joined
Jun 3, 2020
Messages
24
Office Version
  1. 2007
Platform
  1. Windows
I got runtime error 91 on executing the following code. Can anyone help me to resolve this error?



Option Explicit

Sub MergeSheets5()

Dim wsD As Worksheet
Dim ws As Worksheet

Set wsD = ThisWorkbook.Sheets("Wardwise")

'delete previous data
wsD.Range("B6:R10000").Clear

Dim data_lastrow As Long
Dim FoundCell As Range
Dim data_lastrow1 As Long


For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Wardwise" Then
Else
'unmerge all cells
ws.Range("B8:R10000").UnMerge


'count the lastrow of each sheets
data_lastrow = wsD.Cells(Rows.Count, 4).End(xlUp).Row + 1

'Find net total as last row
Const WHAT_TO_FIND As String = "Net Total"
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
data_lastrow1 = FoundCell.Row

'copy sheets into Destination sheet
ws.Range("C8:Q" & data_lastrow1).Copy Destination:=wsD.Range("D" & data_lastrow)
ws.Range("B8:B" & data_lastrow1).Copy Destination:=wsD.Range("B" & data_lastrow)

wsD.Range("C" & data_lastrow & ":C" & data_lastrow + data_lastrow1 - 8).Value = ws.Name

End If
Next ws

data_lastrow = wsD.Cells(Rows.Count, 3).End(xlUp).Row + 1

'remove row contains TOTAL
Dim d As Long 'row number
d = 6
Do Until d = data_lastrow 'loop through each row
If wsD.Cells(d, 2).Value Like "*" & "Total" & "*" Then
wsD.Rows(d).EntireRow.Delete
Else
d = d + 1
End If
Loop

'refresh last row of destination sheet
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("C5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlYes
wsD.Range("B5:R" & data_lastrow).Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlYes

wsD.AutoFilterMode = False

Set wsD = Nothing
Set ws = Nothing

MsgBox "Done merged"

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I don't see your data but if this range = nothing
VBA Code:
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
Then this row will throw an Error 91
VBA Code:
data_lastrow1 = FoundCell.Row
 
Upvote 0
I don't see your data but if this range = nothing
VBA Code:
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
Then this row will throw an Error 91
VBA Code:
data_lastrow1 = FoundCell.Row
Shall I share you the Excel file? How to share the file?
 
Upvote 0
No need to do that. What line of code is highlighted if you click on debug when you get the error message...
 
Upvote 0
Runtime Error 91 is thrown when it passes this line of code:

VBA Code:
Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
 
Upvote 0
As @igold has pointed out erroring on the next line is far more likley.
What happens if you add the code in blue to your code:
Rich (BB code):
        'Find net total as last row
        Const WHAT_TO_FIND As String = "Net Total"
        Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
        
        If FoundCell Is Nothing Then
            MsgBox "The search term: " & WHAT_TO_FIND & "  was not found"
            Exit Sub
        End If
        
        data_lastrow1 = FoundCell.Row
 
Upvote 0
As @igold has pointed out erroring on the next line is far more likley.
What happens if you add the code in blue to your code:
Rich (BB code):
        'Find net total as last row
        Const WHAT_TO_FIND As String = "Net Total"
        Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND)
       
        If FoundCell Is Nothing Then
            MsgBox "The search term: " & WHAT_TO_FIND & "  was not found"
            Exit Sub
        End If
       
        data_lastrow1 = FoundCell.Row
Upon running the code above, the meesage box "Net Total" was not found is displayed.
 
Upvote 0
Can you make Net Total the active cell. Take a screenshot showing the row and column references and include the formula box.
Also if in a cell you type =Len(select cell with Net Total) what does it return 9 or something else.
 
Upvote 0
Thanks for the reply. Sorry for the delayed response from me. I was in a hectic schedule.
LEN() function Returns 9 only. Screenshot of Net Total

1720259962990.png
 
Upvote 0
What happens if you change your Find line to this:
VBA Code:
        Set FoundCell = ws.Range("B:B").Find(What:=WHAT_TO_FIND, MatchCase:=False)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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