dynamic named ranges with charts

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
741
Office Version
  1. 365
  2. 2010
my chart is linked to data , so to make it dynamic, the chart is referencing named ranges ...the named ranges have the formula ="offset(A2,0,0,counta(A:A)-1,1)" for column A, then the same formulas for columns B , C, D, and E (ie. "offset(b2,0,0,counta(b:b)-1,1)", etc. etc.

i'm starting from A2 since row 1 has headers

but occassionally to keep the chart from not going too long, I will need to delete some rows starting from row 2

the problem is that if I delete row 2, it'll cause references errors for offset formula since they are referring to A2, B2, etc

is there a way around this?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
One can often have many named ranges and keeping track of what they are is difficult.

I write some code to create the named ranges so that they are both documented and can be recreated as
and when necessary. I sometimes run the code when the workbook is opened.

This code will create your named ranges from scratch and overwrite the existing ones.

All you have to do is alter the code to indicate the worksheet name and the names of the named ranges that you want to create..

VBA Code:
Public Sub CreateNamedRanges()
Dim Ws As Worksheet
Dim strFormula As String
Dim rng As Range
Dim strCol As String

    Set Ws = Worksheets("NamedRanges")
    
    For Each rng In Range("A2:E2").Cells
        strCol = Chr(64 + rng.Column)
        strFormula = "=OFFSET(" & Ws.Name & "!" & rng.Address & ",0,0,COUNTA($" & strCol & ":$" & strCol & ") -1,1)"
        Ws.Names.Add Name:="myRangeName" & strCol, RefersTo:=strFormula
    Next rng

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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