Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:33:49+00:00 2026-06-11T15:33:49+00:00

I am trying to turn the current active selection into a named ranged that

  • 0

I am trying to turn the current active selection into a named ranged that I can reference as the data source for a pivot table. My function selectByUsedRows provides a selection based on how many rows are in the usedCol and starting and stoping at selectStartCol and selectEndCol. This is usefull when you only want your selection to contain cells that match the amount of rows of a column that is outside the selection. I am out of my depth on naming the selection on this one. Any help would be great.

Excel Data

     A        B         C
1    CaseNum  Branch    Name
2    1234     APL       George
3    2345     VMI       George
4    3456     TEX       Tom
5    4567     NYC       Tom
6    5678     VMI       Sam
7    6789     TEX       Tom
8    7890     NYC       George

VBA

'Check reference column and select the same number of rows in start and end columns
Sub selectByUsedRows(usedCol As String, selectStartCol As String, selectEndCol As String)
n = Range(usedCol & "1").End(xlDown).Row
Range(selectStartCol & "1:" & selectEndCol & n).Select
End Sub

'Dynamically select columns A to C with as many rows as are in A
Sub test()
refCol = "A"
selectStartCol = "A"
selectEndCol = "C"
selectByUsedRows refCol, selectStartCol, selectEndCol

'Code works until this point. There is now an active selection of A1:C8. 
'The following is hypothetical

Dim rngSelection As Range
Set rngSelection = ActiveSelection
Range(rngSourceData).CurrentRegion.Name = "rngSourceData"

Set objTable = Sheet5.PivotTableWizard

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    rngSourceData, Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="Sheet5!R1C4", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion14
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:33:50+00:00Added an answer on June 11, 2026 at 3:33 pm

    I think you should consider creating a dynamic range, rather than a static one. A static range like A1:C8 will populate your pivot table just fine until someone enters more data in RefColumn. If you create a dynamic range like

    =$A$1:INDEX($C:$C,COUNTA($A:$A),1)
    

    Then you won’t have to create the pivot table every time the range changes.

    Sub MakePT()
    
        Dim sh As Worksheet
        Dim pc As PivotCache
        Dim pt As PivotTable
        Dim rUsed As Range
    
        'Make a dynamic range name and return a reference to it
        Set rUsed = MakeNamedRange(Sheet1, 1, 1, 3, "rngSourceData")
    
        'Add a new sheet for the pivot table
        Set sh = ThisWorkbook.Worksheets.Add
    
        'Create a blank pivot table on the new sheet
        Set pc = ThisWorkbook.PivotCaches.Add(xlDatabase, rUsed)
        Set pt = pc.CreatePivotTable(sh.Range("A3"), "MyPivot")
    
    
    End Sub
    
    Public Function MakeNamedRange(sh As Worksheet, lRefCol As Long, lStartCol As Long, lEndCol As Long, sName As String) As Range
    
        Dim sRefersTo As String
        Dim nm As Name
    
        'Comments refer to lRefCol = 1, lStartCol = 1, lEndCol = 3
    
        '=$A$2:
        sRefersTo = "=" & sh.Cells(1, lStartCol).Address(True, True) & ":"
    
        '=$A$2:INDEX($C:$C,
        sRefersTo = sRefersTo & "INDEX(" & sh.Cells(1, lEndCol).EntireColumn.Address(True, True) & ","
    
        '=$A$2:INDEX($C:$C,COUNTA($A:$A),1)
        sRefersTo = sRefersTo & "COUNTA(" & sh.Cells(1, lRefCol).EntireColumn.Address(True, True) & "),1)"
    
        Set nm = ThisWorkbook.Names.Add(sName, sRefersTo)
    
        Set MakeNamedRange = sh.Range(sName)
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to turn my data into a burndown chart to monitor the progress
Trying to turn this: href=/wp-content/themes/tray/img/celebrity_photos/photo.jpg into: href=/img/celebrity_photos/photo.jpg So I'm simply trying to remove /wp-content/themes/tray/
I'm trying to turn this XML string into a select I have @Schedule XML
So i'm trying to turn a bunch of from x import x statements, that
I am trying to turn specific words or phrases into links with jQuery. So
I am trying to turn div#sidebar into a sidebar in my app. My code
I'm trying to turn some query results into click-able links through PHP. I'm a
I'm trying to create a hotkey toggle(f12) that will turn on a loop when
I am trying to clone a class that contains inputs and textareas, and turn
I'm trying to set up an htaccess rule that'll work with the current permalink

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.