+ Reply to Thread
Results 1 to 7 of 7

Getting Power Query data to an Excel worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    03-30-2024
    Location
    Kansas City MO
    MS-Off Ver
    Excel 365
    Posts
    4

    Getting Power Query data to an Excel worksheet

    I'm having a bit of an issue with an Excel power query and VBA.

    In VBA:
    I create a new sheet in my workbook and name it.

    I get my data from a CSV file with the information I need into a power query in my workbook. I can see the query in the workbook and view the data, everything is there. It is also a connection only query. I don't need to worry about refreshing the data because each week the CSV file has a different name. Refreshing is irrelevant.

    Here is my issue. I want to use VBA code to take all the data in the query, all columns and all rows, and import it into the newly created sheet with the same formatting as if I was doing this all manually via the menu commands. Headers, Rows, shading, etc. Menu command: Data | From CSV | Choose CSV file | Load. Doing it manually creates a sheet with the name of the CSV file with, in my case, green shadings.

    Once I have it in a sheet, then the rest of my script will do some data manipulation, I.e., converting customer ID# to customer name, delete a few columns, and create and format a pivot table and pivot chart.

    Everything is working fine except putting the data from the query into a sheet.

    Any help would be appreciated.

    Thank you
    Michael.
    Last edited by MichaelMorris; 03-30-2024 at 08:29 PM. Reason: Added more information

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Getting Power Query data to an Excel worksheet

    You already have the data in the Query if I understand you correctly.
    When you set this up the right way, you can dynamically refer to the latest CSV file, which you just load to your sheet. Do your transformations in the query and not in VBA.

  3. #3
    Registered User
    Join Date
    03-30-2024
    Location
    Kansas City MO
    MS-Off Ver
    Excel 365
    Posts
    4

    Re: Getting Power Query data to an Excel worksheet

    Thanks for the reply. That is not the issue.

    I need to find the code to copy the data from the query and paste it to a sheet.
    We need the data in the sheet so it can be looked at by the person the report is going to.
    The rest is not in question.

    I'm just trying to figure out the code to ready the query, column and rows, and put that is a blank sheet starting at
    Cell A1.

    Thanks

  4. #4
    Registered User
    Join Date
    03-30-2024
    Location
    Kansas City MO
    MS-Off Ver
    Excel 365
    Posts
    4

    Re: Getting Power Query data to an Excel worksheet

    Solution found for my situation:

    After I create my blank sheet and have loaded the query, and the data is there as a connection only query.

    Add this line:

    LoadQuery "QueryName", ActiveSheet
    QueryName is the name you used when creating the query.



    Then have the following Private Sub

    Private Sub LoadQuery(ByVal QueryName As String, ByVal LoadDataSheet As Worksheet)
    
        With LoadDataSheet.ListObjects.Add(SourceType:=0, Source:= _
                                         "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=" & QueryName & ";Extended Properties=""""", _
                                         Destination:=LoadDataSheet.Range("$A$1")).QueryTable
    
            .CommandType = xlCmdSql
            .CommandText = Array("SELECT * FROM [" & QueryName & "]")
       
    
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = False
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = False
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "Table_" & QueryName
            .Refresh BackgroundQuery:=False
    
        End With
    
    End Sub


    This will take all your data from the query and dump it into a sheet.
    I cannot take credit for the solution, found on another site, but thought it might be helpful to others.
    Last edited by MichaelMorris; 04-01-2024 at 03:15 PM. Reason: Solution.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,691

    Re: Getting Power Query data to an Excel worksheet

    crosspost: https://www.mrexcel.com/board/thread...sheet.1256530/

    Rule 7: Cross-posting Without Telling Us

    Your post does not comply with Rule 3 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Read this to understand why we ask you to do this.

    I have added the crosspost reference for you today. Please comply with this and all our rules in the future

    However, if you continue to crosspost, you can expect to have your thread BLOCKED until you update it yourself.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  6. #6
    Registered User
    Join Date
    03-30-2024
    Location
    Kansas City MO
    MS-Off Ver
    Excel 365
    Posts
    4

    Re: Getting Power Query data to an Excel worksheet

    Since I was unaware that the sites I was posting to were connected, I apologize.

    I hadn't received any help after posting on either and just happened to stumble on the solution myself and wanted to get it on the thread so others could see that it had been solved.

    Thanks
    Michael

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,691

    Re: Getting Power Query data to an Excel worksheet

    The sites are not connected. Read the link highlighted in RED in my post on why this is important to notify each forum of the crosspost.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Power Query: data difference PQ screen vs Excel worksheet
    By jonas-martens in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-07-2024, 08:32 AM
  2. Replies: 5
    Last Post: 10-31-2023, 11:12 AM
  3. [SOLVED] Why is my Excel file so big even though I uploaded the data using Power Pivot/Power Query?
    By Olivia Ludwig in forum Excel Charting & Pivots
    Replies: 12
    Last Post: 09-21-2023, 05:18 AM
  4. [SOLVED] Power Query - excel formula translation into Power Query
    By afgi in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 02-19-2020, 03:38 AM
  5. Replies: 4
    Last Post: 02-17-2020, 06:03 AM
  6. Excel Power Query Refresh or Access Query - 2nd Query Run is faster
    By Steveapa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2020, 10:16 AM
  7. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1