• Skip to main content
  • Skip to primary sidebar
  • Home
  • Blog
  • VBA Programming
  • Open-Libre Office
  • Web Stuff
  • Videos
    • VBA Articles
    • LibreOffice And Open Office
  • Excel VBA Glossary
  • About
  • Contact

My Best Stuff Is In The Newsletter

No charge. No spam. Unsubscribe anytime.

Submitting...

How To Resize A Named Range In Excel VBA

To resize a named range in Excel VBA it’s best to get an object reference to the range and then use the Excel VBA resize method to resize the range.

With the resized range object you then set it’s name property to the range name that you want resized.

Excel will behind the scenes delete the old range name and replace it with the one you have just created.

Copy cells from one range to another using vba

The spreadsheet above (left) has a named range called "nmeCustomerData"

As you can see "nmeCustomerData" needs to be readjusted to refer to the complete data range.

This can be done via the user interface, however the focus here is on how to do this via vba.

Image

Get the spreadsheet used in the tutorial video above.

Also get updates when new blog or video tutorials are released.

Submitting…

The VBA Code Below Will Resize The Range Variable

Line 4 gets a reference to the Excel named range and then sets it to the current region for that range.

This is the same as manually clicking in the range and pressing "F5" then clicking "Special" then "Current Region".

01  Sub ResizeNamedRange()
02    Dim rngData As Range    ' Create rngData object variable
03 
04    Set rngData = ThisWorkBook.Names("nmeCustomerData").RefersToRange.CurrentRegion
05    Set rngData = rngData.Resize(rngData.Rows.Count -1).Offset(1)
06    rngData.Name = "nmeCustomerData"
07    Set rngData = Nothing
08  End Sub 

Line 5 then resizes the range which now refers to all of the data including the header (which we don’t want) to one row less than all of the data and then offsets the range by one row.

The purpose of this is to account for the header and then move the selection down so the data excluding the header is referenced.

Line 6 then applies the existing range name to the name property of this resized range variable.

And that’s it you have now resized an existing range name in Excel VBA.

You can get the macro spreadsheet that goes with this article in the yellow box above, this will also notify you when I publish other articles that may be of interest to you. You can easily opt out of these article notifications at any time.

If you found this article really helpful, then consider grabbing me a coffee, it takes a lot of work and expense to keep this site going, the more caffine I have the harder I can work.

And if you do grab me a coffee, thanks for your support 😉

Filed Under: VBA Tagged With: Excel vba, Excel VBA Macros

Reader Interactions

Comments

  1. ash says

    16/10/2018 at 23:48

    Great work and very helpful tutorial, it makes it easier to understand other more experienced user code

    Reply
  2. Ian says

    29/10/2019 at 13:11

    Perfect, I’d forgotten how to do this!

    Reply
    • Sean Johnson says

      23/11/2021 at 10:14

      Glad I could help

      Reply
  3. D Barinsky says

    06/08/2021 at 20:03

    Doesn’t CurrentRegion grab all adjacent cells with data in them? Is this therefore assuming no adjacent data in the cells adjacent to the named region, or am I missing something?

    Reply
    • Sean Johnson says

      23/11/2021 at 10:14

      You’re correct, the idea is to ensure in your spreadsheet design that you don’t have adjacent cells to your main data ranges.
      And teach other users of your spreadsheet to have the same approach, i.e. data ranges consist of a header and data and adjacent cells must not be populated.
      Said another way, there must be a blank row and column surrounding the data range if it’s not at the top and or left of the spreadsheet.

      Reply
  4. Far says

    21/07/2022 at 15:48

    Hi Sean, this is very helpful.. How can I adjust this code if my range is a single column and I just want to adjust the number of rows in the range? Thanks

    Reply
    • Sean Johnson says

      14/12/2022 at 23:05

      Yes, it works for single columns also.

      Reply

Leave a Reply to Far Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Free Training Videos

Make VBA Listbox Work as Database Grid

Image

Ulitmate Excel VBA Advanced Filter Tutorial

Click video and press f for full screen or watch on YouTube

Dynamically Link Images to LibreOffice Base Form

Click video and press f for full screen or watch on YouTube

Create Database with LibreOffice Base

Click video and press f for full screen or watch on YouTube

Create VBA Form In Excel

Click video and press f for full screen or watch on YouTube

Affiliate Disclamer

We hope you love the products we recommend! Just so you know, we may collect a share of sales or other compensation from the links on this page.
Thank you if you use our links, we really appreciate it!

Terms Of Service | Privacy Statement | Copyright © 2024 BusinessProgrammer.com.