VBA Create Range Not Working - What Am I doing Wrong?

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
As always, TIA to those who give of their time and expertise. I can't figure out why the range isn't being created. I expect it will be a flat forehead moment when someone else looks at it.

Ron

The KISS record a macro code is:

VBA Code:
Range("J2:P2").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Names.Add Name:="tbl_ws_Report", RefersToR1C1:= _
        "='Accounts Report'!R2C10:R15751C16"

What I typically use is a little simpler with all rows and all columns. This is erroring on the range statement.

VBA Code:
Sub m_ws_Report_Ranges()
'20211006
    '
  With Application
    .ScreenUpdating = False
    .DisplayStatusBar = True
    .StatusBar = "Making Report Ranges"
    .Volatile
    .DisplayAlerts = False
  End With 'Application
    '
    Dim ws_ReportLastRow          As Long
    Dim tbl_ws_Report             As Range
    Dim ThisWb                    As Workbook
    Dim ThisWs                    As Worksheet
    '
  Set ThisWb = ActiveWorkbook
  With ThisWb
  ws_summary.Activate
    Set ThisWs = ActiveSheet
'--Start delete old range names
    ThisWb.Names("tbl_ws_report").Delete
'--End delete old range names
'-- Start Name Report ranges --
  ws_report.Activate
  Set ThisWs = ActiveSheet
  With ThisWs
'--Note: column J is email records, no null cells, no formulas
  ws_ReportLastRow = Cells(Cells.Rows.Count, "J").End(xlUp).Row
  Range("J2:P", ws_ReportLastRow).Name = "tbl_ws_Report"
'-- End Name Report ranges --
  End With 'ThisWs
  ws_summary.Activate
  With ThisWs
  Application.ScreenUpdating = True
  Range("A1").Activate
  ActiveWindow.SmallScroll down:=3
  ActiveWindow.SmallScroll down:=-3
  Range("A1").Activate
  End With 'ThisWS
  With Application
    .ScreenUpdating = True 'False
    .EnableEvents = True 'False
    .DisplayAlerts = True ' False
    .Calculation = xlCalculationAutomatic 'xlManual
    .StatusBar = " "
    .StatusBar = "Completed creating Report Ranges."
  End With 'Application
  End With 'ThisWb
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You have no Names.Add method in your code.

Not tested:

VBA Code:
'--Note: column J is email records, no null cells, no formulas
  ws_ReportLastRow = Cells(Cells.Rows.Count, "J").End(xlUp).Row
  'Range("J2:P", ws_ReportLastRow).Name = "tbl_ws_Report"
  ThisWb.Names.Add Name:="tbl_ws_Report", RefersTo:=.Range("J2:P", ws_ReportLastRow)
'-- End Name Report ranges --
 
Upvote 0
Your code immediately throws an error, see screenshot. 2021-10-06 23_07_16-Window.png
 
Upvote 0
You have no Names.Add method in your code.

Not tested:

VBA Code:
'--Note: column J is email records, no null cells, no formulas
  ws_ReportLastRow = Cells(Cells.Rows.Count, "J").End(xlUp).Row
  'Range("J2:P", ws_ReportLastRow).Name = "tbl_ws_Report"
  ThisWb.Names.Add Name:="tbl_ws_Report", RefersTo:=.Range("J2:P", ws_ReportLastRow)
'-- End Name Report ranges --
Thanks, no joy. Not that names.add may be a best practice I'm happy to use, just an FYI, I've been using code like the following for years without using names.add
I try to use worksheet code names, like the ws_report or ws_summary, but the sheet below has been imported from another workbook and will be deleted after some of the records are copied. Just easier for me to code in the workbook than trying to ensure I get the correct ranges from the closed workbook.

VBA Code:
Sheets("New Badge Report").Activate
    With ThisWs
    NewBadgesLastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
    NewBadgesLastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Range("A2", Cells(NewBadgesLastRow, NewBadgesLastCol)).Name = "tbl_NewBadges"
 
Upvote 0
What error are you getting when you use my code?
 
Upvote 0
Try this instead

VBA Code:
  ThisWb.Names.Add Name:="tbl_ws_Report", RefersTo:=Range("J2:P", ws_ReportLastRow)
 
Upvote 0
What error are you getting when you use my code?
My apologies, poorly worded :rolleyes:. No error is thrown, but the range isn't added. Not listed in the Name Manager.
 
Last edited:
Upvote 0
Are you trying to give Range("J2:P2") the name "tbl_ws_Report"? If so and assuming the sheet it is on is active...

Range("J2:P2").Name = "tbl_ws_Report"

If the sheet is not active, just make the Range command reference it directly. For example, assuming Sheet1...

Sheets("Sheet1").Range("J2:P2").Name = "tbl_ws_Report"
 
Upvote 0
Looking at it again, your original line

VBA Code:
Range("J2:P", ws_ReportLastRow).Name = "tbl_ws_Report"

may have a typo. Try this instead

VBA Code:
Range("J2:P" & ws_ReportLastRow).Name = "tbl_ws_Report"
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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