+ Reply to Thread
Results 1 to 7 of 7

How to pull data into other columns when row #'s change between reports

Hybrid View

  1. #1
    Registered User
    Join Date
    06-27-2019
    Location
    U.S.
    MS-Off Ver
    Office 16
    Posts
    3

    How to pull data into other columns when row #'s change between reports

    book1.JPG
    book2.JPG

    Kind of lost on writing this macro, as I just began a few weeks ago. book1 and book2 images attached, and also workbook also attached to explain the below:

    Sheet1 has customer information, including customer name, #, and all the related documents (invoice/payment info). The Customer ID and Customer Name are only at the heading of the information, and I need it to be pulled into each row to be included with the documents (invoice/payment info). However, the number of documents can change on a weekly basis, therefore, changing what rows all the data is on.

    Basically, I need Sheet1 to end up looking like Sheet1(2). As previously stated, the number of rows per customer can change weekly, etc.

    Is this even possible?
    Attached Files Attached Files

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: How to pull data into other columns when row #'s change between reports

    You can use Power Query to normalise the data from your source file:

    let
        Source = Excel.Workbook(File.Contents("C:\temp\vbahelp.xlsx"), null, true),
        WorkSheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(WorkSheet, [PromoteAllScalars=true]),
        #"Filtered Rows" = Table.SelectRows(#"Promoted Headers", each not Text.Contains([Type], "Total")),
        #"Added Reference" = Table.AddColumn(#"Filtered Rows", "Reference", each try Number.FromText([Ref Nbr]) otherwise null, type number),
        #"Added Company Number" = Table.AddColumn(#"Added Reference", "Company Number", each if [Reference] = null then [Type] else null, type text),
        #"Filled Down Company Number" = Table.FillDown(#"Added Company Number",{"Company Number"}),
        #"Added Company Name" = Table.AddColumn(#"Filled Down Company Number", "Company Name", each if [Reference] = null then [Ref Nbr] else null, type text),
        #"Filled Down Company Name" = Table.FillDown(#"Added Company Name",{"Company Name"}),
        #"Filtered null Reference" = Table.SelectRows(#"Filled Down Company Name", each [Reference] <> null and [Reference] <> ""),
        #"Choose Columns" = Table.SelectColumns(#"Filtered null Reference",List.Combine({{"Company Number", "Company Name"},Table.ColumnNames(#"Promoted Headers")})),
        #"Changed Type" = Table.TransformColumnTypes(#"Choose Columns",{{"Type", type text}, {"Terms", type text}, {"Ref Nbr", Int64.Type}, {"Doc Date", type date}, {"Current", Currency.Type}, {"1 To 30", Currency.Type}, {"31 To 60", Currency.Type}, {"61 To 90", Currency.Type}, {"Over 90", Currency.Type}, {"Total", Currency.Type}})
    in
        #"Changed Type"
    Change filepath / name and worksheet name to suit.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,675

    Re: How to pull data into other columns when row #'s change between reports

    VBA
    Sub test()
        Dim x, i As Long
        x = Filter([transpose(if((c1:c50000="Customer Total")+(c1:c50000="Company:"),row(1:50000)))], False, 0)
        For i = 0 To UBound(x) - 1
            With Cells(x(i) + 2, "a").Resize(x(i + 1) - 2 - x(i))
                .Value = Cells(x(i) + 1, "d")
                .Columns(2).Value = Cells(x(i) + 1, 3)
            End With
            Cells(x(i) + 2, "m").Resize(, 2).Value = Cells(x(i) + 1, "g").Resize(, 2).Value
        Next
    End Sub

  4. #4
    Registered User
    Join Date
    06-27-2019
    Location
    U.S.
    MS-Off Ver
    Office 16
    Posts
    3

    Re: How to pull data into other columns when row #'s change between reports

    Thanks Jindon, that works like a beauty. I do have one final thing I'm struggling with on this macro, and it will be completed. I need macro to calculate the Percentage of Total lines:

    sheet4.JPG

    Each "Percentage of Total" line needs to calculate the percentage of the Grand Total. For example, row 4 (columns G-L), needs to calculate =G3/G33, or =H3/H33, and so forth. However, as previously stated, the company totals, percentage of totals and grand totals' row numbers will change every time I run my report. The final "Percentage of Total" line located on row 34, is just calculating the percentage of the total (i.e. =G33/L33, or =H33/L33, etc.)

    I have already used conditional formatting macro to format the percentage of total rows to 0.00% format.

    Attached is excel workbook as well (see Sheet4)
    Attached Files Attached Files
    Last edited by AliGW; 07-03-2019 at 09:18 AM. Reason: Please don't quote unnecessarily!

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,675

    Re: How to pull data into other columns when row #'s change between reports

    Try
    Sub test()
        Dim x, i As Long, r As Range
        Application.ScreenUpdating = False
        With Sheets("sheet1")
            If .[a2] <> "Company:" Then Exit Sub
            .Columns("a:b").Insert
            With .[a1:b1]
                .Value = Array("Customer Name", "Customer ID")
                .Font.Bold = True
            End With
            x = Filter([transpose(if((c1:c50000="Customer Total")+(c1:c50000="Company:"),row(1:50000)))], False, 0)
            For i = 0 To UBound(x) - 1
                With Cells(x(i) + 2, "a").Resize(x(i + 1) - 2 - x(i))
                    .Value = Cells(x(i) + 1, "d")
                    .Columns(2).Value = Cells(x(i) + 1, 3)
                End With
                Cells(x(i) + 2, "m").Resize(, 2).Value = Cells(x(i) + 1, "g").Resize(, 2).Value
            Next
            .Rows("2:3").Delete
            With .Cells(1).CurrentRegion.Offset(1).Columns(1).SpecialCells(2)
                For Each r In .Areas
                    With r(r.Count + 1).Resize(2, 12)
                        .ClearContents
                        .Range("a1:a2").Value = _
                        Application.Transpose(Array(r(1).Value & " Total", "Percentage of Total"))
                        .Rows(1).Range("g1:l1").Formula = _
                        "=subtotal(9," & r.Offset(, 6).Address(0, 0) & ")"
                        .Font.Bold = True
                    End With
                Next
            End With
            With .Range("a" & Rows.Count).End(xlUp)(2).Resize(4)
                .EntireRow.ClearContents
                With .Rows("3:4")
                    .Font.Bold = True
                    .Range("a1:a2").Value = Application.Transpose(Array("Grand Total", "Percentage of Total"))
                    .Rows(1).Range("g1:l1").FormulaR1C1 = "=subtotal(9,r2c:r[-5]c)"
                End With
            End With
            .Columns.AutoFit
        End With
        Application.ScreenUpdating = True
    End Sub

  6. #6
    Registered User
    Join Date
    06-27-2019
    Location
    U.S.
    MS-Off Ver
    Office 16
    Posts
    3

    Re: How to pull data into other columns when row #'s change between reports

    Last edited by AliGW; 07-03-2019 at 09:17 AM. Reason: Please don't quote unnecessarily!

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,675

    Re: How to pull data into other columns when row #'s change between reports

    Don't quote full post unless you really need it to do, the forum doesn't like it.

    No way to look at your attachment.

    No error at all.
    Attached Files Attached Files

+ 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] Pull-data-from-a-closed-workbook Format change VBA
    By rajeshn_in in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2017, 08:55 AM
  2. [SOLVED] Pull data from one tab to another (but change how it's displayed)
    By bkarhu in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-10-2014, 11:36 AM
  3. Please Help - Summary/Reports (Multiple columns calculation into different reports)
    By funguy757 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-17-2014, 12:50 PM
  4. Excel Reports - Using pull downs for multiple categories
    By davidingilbert in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-09-2014, 01:58 PM
  5. [SOLVED] Query to pull out change and replace data
    By lengwer in forum Access Tables & Databases
    Replies: 3
    Last Post: 08-29-2013, 01:42 PM
  6. Pull Data from Multiple Columns Based On Data Validation List Selection
    By CHRISOK in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2013, 11:41 PM
  7. Change the data set your Vlookup's pull from Via a drop box
    By transition in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-27-2013, 06:19 PM

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