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

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
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
 
You have just put it back as it was before & not changed like I showed. It should be
VBA Code:
        Set Fnd = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Fnd Is Nothing Then
            MsgBox "no data"
            Exit Sub
         End If
        myLastRow = Fnd.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
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You have just put it back as it was before & not changed like I showed. It should be
VBA Code:
        Set Fnd = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Fnd Is Nothing Then
            MsgBox "no data"
            Exit Sub
         End If
        myLastRow = Fnd.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

We're getting closer! Getting this error:

Run-time error '1004': Application-defined or object-defined error

at this point:

VBA Code:
ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic

Here's the current full 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
 
        Set Fnd = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Fnd Is Nothing Then
            MsgBox "no data"
            Exit Sub
         End If
        myLastRow = Fnd.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
 
End With
 
    ThisWorkbook.Names.Add Name:=myRangeName, RefersTo:=myNamedRangeDynamic
  
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
 
End Sub
 
Upvote 0
You haven't assigned anything to myNamedRangeDynamic
 
Upvote 0
You haven't assigned anything to myNamedRangeDynamic

Yes! You are correct. I had inadvertently deleted a line. Here's the code that works:

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
 
        Set Fnd = .Find(What:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Fnd Is Nothing Then
            'MsgBox "no data"
            Exit Sub
         End If
        myLastRow = Fnd.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

Thanks!
 
Upvote 0
Glad it's sorted & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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