My range creation macro crashes when there's nothing in range and it's a bummer.

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
485
Office Version
  1. 365
Platform
  1. Windows
Good afternoon.

I have some code sourced from powerspreadsheets.com that creates a named range called FloorPlanRequestsFormulas.

It works as intended when there is data in the target cells, but gives this error when there is none:

Run-time error '91':

Object variable or With block variable not set


More specifically, it doesn't seem to like this line:

VBA Code:
myLastRow = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Here is the complete code:

VBA Code:
Sub FloorPlanRequestsFormulasRange()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim myWorksheet As Worksheet
 
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
 
    Dim myNamedRangeDynamic As Range
 
    Dim myRangeName As String
 
    Set myWorksheet = ThisWorkbook.Worksheets("FloorPlanRequests")
 
    myFirstRow = 1
    myFirstColumn = 5
    myLastColumn = 8
 
    myRangeName = "FloorPlanRequestsFormulas"
 
    With myWorksheet.Cells
 
        myLastRow = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        myLastColumn = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 
        Set myNamedRangeDynamic = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
 
End With
 
    ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Here's the current code:

VBA Code:
Sub FloorPlanRequestsFormulasRange()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim myWorksheet As Worksheet
 
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
 
    Dim myNamedRangeDynamic As Range
 
    Dim myRangeName As String
 
    Set myWorksheet = ThisWorkbook.Worksheets("FloorPlanRequests")
 
    myFirstRow = 1
    myFirstColumn = 5
    myLastColumn = 8
 
    myRangeName = "FloorPlanRequestsFormulas"
 
    With myWorksheet.Cells
 
        myLastRow = myWorksheet.[match(2,1/(l:l<>""))]
        myLastColumn = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
 
        Set myNamedRangeDynamic = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
 
End With
 
    ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic
   
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub

Now giving me a Run-time '13' error.
 
Upvote 0
How about
VBA Code:
Sub FloorPlanRequestsFormulasRange()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim myWorksheet As Worksheet
 
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim Fnd As Range
    
    Dim myNamedRangeDynamic As Range
 
    Dim myRangeName As String
 
    Set myWorksheet = ThisWorkbook.Worksheets("FloorPlanRequests")
 
    myFirstRow = 1
    myFirstColumn = 5
    myLastColumn = 8
 
    myRangeName = "FloorPlanRequestsFormulas"
 
    With myWorksheet.Cells
 
        myLastRow = myWorksheet.[match(2,1/(l:l<>""))]
        Set Fnd = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
        If Fnd Is Nothing Then
            MsgBox "no data"
            Exit Sub
         End If
        myLastColumn = Fnd.Column
        Set myNamedRangeDynamic = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
 
End With
 
    ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic
   
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub FloorPlanRequestsFormulasRange()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim myWorksheet As Worksheet
 
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim Fnd As Range
   
    Dim myNamedRangeDynamic As Range
 
    Dim myRangeName As String
 
    Set myWorksheet = ThisWorkbook.Worksheets("FloorPlanRequests")
 
    myFirstRow = 1
    myFirstColumn = 5
    myLastColumn = 8
 
    myRangeName = "FloorPlanRequestsFormulas"
 
    With myWorksheet.Cells
 
        myLastRow = myWorksheet.[match(2,1/(l:l<>""))]
        Set Fnd = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
        If Fnd Is Nothing Then
            MsgBox "no data"
            Exit Sub
         End If
        myLastColumn = Fnd.Column
        Set myNamedRangeDynamic = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
 
End With
 
    ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic
  
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub

That gives me a Run-time error '13' Type mismatch at this line:

VBA Code:
myLastRow = myWorksheet.[match(2,1/(l:l<>""))]
 
Upvote 0
I didn't make any changes to that row, so was it giving you an error before?
 
Upvote 0
Then just change that line back to what it was & do it in the same way as I showed for the column.
 
Upvote 0
Then just change that line back to what it was & do it in the same way as I showed for the column.

OK, did that. Back to this error:

Run-time error '91':

Object variable or With block variable not set
 
Upvote 0
Did you do it in the same way I showed for the column?
 
Upvote 0
Did you do it in the same way I showed for the column?

I did (I think). Here's the current code:

VBA Code:
Sub FloorPlanRequestsFormulasRange()

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim myWorksheet As Worksheet
 
    Dim myFirstRow As Long
    Dim myLastRow As Long
    Dim myFirstColumn As Long
    Dim myLastColumn As Long
    Dim Fnd As Range
    
    Dim myNamedRangeDynamic As Range
 
    Dim myRangeName As String
 
    Set myWorksheet = ThisWorkbook.Worksheets("FloorPlanRequests")
 
    myFirstRow = 1
    myFirstColumn = 5
    myLastColumn = 8
 
    myRangeName = "FloorPlanRequestsFormulas"
 
    With myWorksheet.Cells
 
        myLastRow = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Set Fnd = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
        If Fnd Is Nothing Then
            MsgBox "no data"
            Exit Sub
         End If
        myLastColumn = Fnd.Column
        Set myNamedRangeDynamic = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
 
End With
 
    ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic
   
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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