Making a Dynamic named range

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
I work with pivot tables a lot and needed a way to make the various datasets dynamic in case I want to add or delete rows or columns.
I created a FUNCTION that I got to work once but can't seem to duplicate it.

STEPS:
1. Add function to workbook
2. Create a named range Formulas/Name Manager/New with a "Refers to:" as =indirect(poprange("Sheet1"))
3. Create a pivot table and set its source as the name of the named range. IT NOW FAILS HERE. "Invalid Ref"

See function:
Code:
Function poprange(wsname As String, Optional starthead As String)
'can supply optional header key word to start or leave blank and use for populated row and column
'must supply target sheet as name of sheet.  Example =poprange("Sheet1")
Dim firstrow, lastrow, firstcol, lastcol, startrow, startcol, endcol As Long
Dim ws As Worksheet
Dim rng As String
On Error Resume Next
Set ws = ActiveWorkbook.Sheets(wsname)
firstrow = ws.Cells.Find(What:="*", SearchDirection:=xlNext, SearchOrder:=xlByRows).Row
lastrow = ws.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
firstcol = ws.Cells.Find(What:="*", SearchDirection:=xlNext, SearchOrder:=xlByColumns).Column
lastcol = ws.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
If Trim(findhead) <> "" Then
startrow = ws.Cells.Find(What:=starthead, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
        MatchCase:=False, SearchFormat:=False).Row
startcol = ws.Cells.Find(What:=findhead, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
        MatchCase:=False, SearchFormat:=False).Column
        End If


        Resume Next
If startrow = 0 Then
startrow = firstrow
startcol = firstcol
End If


rng = ws.Cells(startrow, startcol).Address & ":" & ActiveSheet.Cells(lastrow, lastcol).Address
poprange = "'" & ws.Name & "'!" & rng
End Function

Again, I got it to work fine but can't duplicate it again.

Also, I want to add an "endhead" optional arg so user can indicate where the range columns should end. How does Excel/VBA know if the optional arg the user provides it arg1 or arg2?

See modified:
Code:
Function poprange(wsname As String, Optional starthead As String, Optional endhead As String)
'can supply optional header key word to start or leave blank and use for populated row and column
'must supply target sheet as name of sheet.  Example =poprange("Sheet1")
Dim firstrow, lastrow, firstcol, lastcol, startrow, startcol, endcol As Long
Dim ws As Worksheet
Dim rng As String
On Error Resume Next
Set ws = ActiveWorkbook.Sheets(wsname)
firstrow = ws.Cells.Find(What:="*", SearchDirection:=xlNext, SearchOrder:=xlByRows).Row
lastrow = ws.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
firstcol = ws.Cells.Find(What:="*", SearchDirection:=xlNext, SearchOrder:=xlByColumns).Column
lastcol = ws.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
If Trim(findhead) <> "" Then
startrow = ws.Cells.Find(What:=starthead, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
        MatchCase:=False, SearchFormat:=False).Row
startcol = ws.Cells.Find(What:=findhead, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
        MatchCase:=False, SearchFormat:=False).Column
endcol = ws.Cells.Find(What:=endhead, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
        MatchCase:=False, SearchFormat:=False).Column
        End If
        Resume Next
If startrow = 0 Then
startrow = firstrow
startcol = firstcol
End If
If endcol = 0 Then
lastcol = endcol
End If
rng = ws.Cells(startrow, startcol).Address & ":" & ActiveSheet.Cells(lastrow, lastcol).Address
poprange = "'" & ws.Name & "'!" & rng
End Function
 
ugh, it's still not working

Code:
Dim crgn As Range
If Trim(endhead) = "" Then
crgn = ws.Range(ws.Cells(startrow, startcol).Address).CurrentRegion.Address
MsgBox crgn
End If

Still returning $C$18 which is what ws.Cells(startrow, startcol).Address is. Expecting $G$23
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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