This code is to alter the named Range size

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
It says the Method Range of object worksheet failed.
What do I have to alter?
The "GantrySizes" is the named range


VBA Code:
Private Sub Gantry_Height_Width_Change()

Dim ws As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range

Set ws = ThisWorkbook.Worksheets("GantryID")
Set StartCell = ws.Range("A1")

  LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
  LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column
 
ws.Range("GantrySizes").Value = ws.Range(StartCell, ws.Cells(LastRow, LastColumn))

End Sub
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
It suggests you don't have a range called GantrySizes on the GantryID sheet.
 
Upvote 0
It is there but every time I try to run the code it deletes the table I`ve just realized
 
Upvote 0
The code that you have there is trying to put the values from ws.Range(StartCell, ws.Cells(LastRow, LastColumn)) into the Named Range GantrySizes. If those two ranges aren't the same size, bad things happen.

The title of the thread mentions altering the size of a named range. If you are trying to change the cells that are in the named range, you might use this syntax

VBA Code:
ws.Range(StartCell, ws.Cells(LastRow, LastColumn)).Name = "GantrySizes"
 
Upvote 0
I tried what you advised but it now says the "Wrong number of arguments or invalid property assignment"

VBA Code:
Private Sub Gantry_Height_Width_Change()

Dim ws As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range

Set ws = ThisWorkbook.Worksheets("GantryID")
Set StartCell = ws.Range("A1")

  LastRow = ws.Cells(ws.Rows.Count, StartCell.Column).End(xlUp).Row
  LastColumn = ws.Cells(StartCell.Row, ws.Columns.Count).End(xlToLeft).Column
  
ws.Range(StartCell, ws.Cells(LastRow, LastColumn)).Name = "GantrySizes"

End Sub
 
Upvote 0
Are you sure you are taking the right route to achieve what you are wanting to do?
Without code, or referring to code that doesn't work, can you tell us in words what you are trying to accomplish?
 
Upvote 0
There are details from an access spreadsheet that fills into the excel spreadsheet. This part works fine
I want the named range to change size according to the amount of data filled into the excel spreadsheet
 
Upvote 0
What is the address of the existing named range GantrySizes ?
Where (what address range) are the details from the access spreadsheet table being written to in the spreadsheet ?
 
Upvote 0
Try
VBA Code:
Range(StartCell, ws.Cells(LastRow, LastColumn)).Name = "GantrySizes"

The two argument form of Range really shouldn't be qualified to a sheet. Its unnecessary. (omit long rant masquerading as an explanation.)
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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