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 7577269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:08:31+00:00 2026-05-30T17:08:31+00:00

I was wondering if anyone could help me with this. I have to create

  • 0

I was wondering if anyone could help me with this. I have to create a pivot table from a worksheet named as “raw”. Unfortunately, sometimes the name of the worksheet can be some other names such as test or even experiment.

My code is as follow to use a macro to create a pivot table.

Range("A1:Z1048576").Select

Sheets.Add

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"raw!R1C1:R1048576C12", Version:=xlPivotTableVersion12 _
).CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion12

As you can see, my ‘SourceData:= raw’ which is the name of the worksheet. As I have explained earlier that this raw can be any name by the user it self, so i was wondering if it is possible to create a pivot table from a worksheet that has names that user that uses the macro, self defined its name.

I have also tried using rename coding, but i have to know the worksheet name before i can do anything else..

Follow up:

My GUI has an open and start button to start the whole thing.

Private Sub testFinder_Click()
    'Open button
    Dim fileToOpen

    fileToOpen = Application _
    .GetOpenFilename("Text Files (*.txt), *.txt")

    If fileToOpen = False Then Exit Sub

    TextBox1.Value = fileToOpen
End Sub

Private Sub CommandButton2_Click()
    'start button

    Application.ScreenUpdating = False

    Workbooks.OpenText Filename:=TextBox1.Value, Origin:=437, _
    StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
    , Space:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End Sub

Then after that, will be the code for the pivot table.

  • 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-05-30T17:08:32+00:00Added an answer on May 30, 2026 at 5:08 pm

    You can use ActiveSheet from the macro but that might give you undesired results if the active sheet is not the actual sheet which has the data. Here is an alternative. Why not let the user select the Pivot range? You can then use that range in your code?

    Sub Sample()
        Dim Rng As Range
    
        On Error Resume Next
        Set Rng = Application.InputBox(Prompt:="Please select the range for the pivot", Type:=8)
        On Error GoTo 0
    
        If Rng Is Nothing Then Exit Sub
    
        MsgBox "The Pivot Range is " & Rng.Parent.Name & "!" & Rng.Address
    End Sub
    

    FOLLOWUP

    DISCLAIMER: I always test my code before posting but in the absence of the Text File in the current scenario, I cannot test the below code. Also I have not done any error handling so do let me know if you get any errors and we will take it from there.

    Button1 Code remains unchanged. I have changed the 2nd Button code slightly and added the 3rd button. Also note that I am not using hard coded numbers like 1048576. No point taking all rows into consideration if your data is say only till 2000 🙂

    TIP: When distributing the application to your user, remember to include error handling. Users often don’t behave like the way you expect them to behave. For example what if the user clicks on the 2nd button before clicking on the 1st button OR what if the user clicks on the 3rd button before clicking on the 1st or the 2nd button 🙂

    CODE

    Option Explicit
    
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Dim lastRow As Long
    
    Private Sub CommandButton1_Click()
        '~~> Remains Unchanged
    End Sub
    
    '~~> Start button
    Private Sub CommandButton2_Click()
        Application.ScreenUpdating = False
    
        Set wb1 = ThisWorkbook
    
        Workbooks.OpenText Filename:=TextBox1.Value, Origin:=437, _
        StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
        Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True
    
        Set wb2 = ActiveWorkbook
        Set ws2 = Sheets(1)
    
        lastRow = ws2.Cells.Find(What:="*", After:=ws2.Range("A1"), _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    End Sub
    
    '~~> 3rd button Code
    Private Sub CommandButton3_Click()
        Set ws1 = wb1.Sheets.Add
    
        wb1.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        ws2.Name & "!R1C1:R" & lastRow & "C12", Version:=xlPivotTableVersion12 _
        ).CreatePivotTable TableDestination:=ws1.Name & "!R3C1", TableName:= _
        "PivotTable1", DefaultVersion:=xlPivotTableVersion12
    End Sub
    

    FOLLOWUP

    TRIED AND TESTED

    Option Explicit
    
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Dim lastRow As Long, LastCol As Long
    Dim strPath As String, FileName As String
    
    Private Sub testFinder_Click()
        '~~> Open button
        Dim fileToOpen
    
        fileToOpen = Application _
        .GetOpenFilename("Text Files (*.txt), *.txt")
    
        If fileToOpen = False Then Exit Sub
    
        TextBox1.Value = fileToOpen
    
        FileName = GetFilenameFromPath(TextBox1.Value)
        strPath = Replace(TextBox1.Value, FileName, "")
    End Sub
    
    '~~> Start button
    Private Sub CommandButton2_Click()
        Set wb1 = ThisWorkbook
    
        Workbooks.OpenText FileName:=strPath & FileName, Origin:=437, _
        StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
        Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
        TrailingMinusNumbers:=True
    
        Set wb2 = ActiveWorkbook
        Set ws2 = Sheets(1)
    
        lastRow = ws2.Cells.Find(What:="*", After:=ws2.Range("A1"), _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
        LastCol = ws2.Cells.Find(What:="*", After:=ws2.Range("A1"), _
        Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious, MatchCase:=False).Column
    End Sub
    
    '~~> 3rd button Code
    Private Sub CommandButton3_Click()
        Set ws1 = wb2.Sheets.Add
    
        wb2.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        ws2.Name & "!R1C1:R" & lastRow & "C" & LastCol, _
        Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:= _
        "[" & wb2.Name & "]" & ws1.Name & "!R3C1", _
        TableName:="PivotTable1", DefaultVersion:= _
        xlPivotTableVersion12
    End Sub
    
    Public Function GetFilenameFromPath(ByVal strPath As String) As String
        If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
            GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, _
            Len(strPath) - 1)) + Right$(strPath, 1)
        End If
    End Function
    

    HTH

    Sid

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anyone could help with this. Let' say I have a
I was wondering if anyone could help me with this problem: I have to
I was wondering if anyone could help me answer this question. It is from
I was wondering if any one could help me out; I have a table
I was wondering if anyone could help me with this problem i'm having. I
Just wondering if anyone could help me with this. I'm new to actionscript, and
I was wondering if anyone could help me out with this. I am running
I was wondering if anyone could help me with this minor problem. I want
I was wondering if anyone could help clear up this issue I am having.
was wondering if anyone could help me out. I have been looking all over

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.