+ Reply to Thread
Results 1 to 26 of 26

Macro to Combine Data

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Macro to Combine Data

    I have a macro to combine all sheets from BR1 Cat to sheet before Consolidate


    When Running the macro the data is not combining the values to give the correct result


    I have manually shown the correct result on sheet Man Result

    It would be appreciated if someone could kindly amend my code

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Howardc1001; 03-31-2023 at 08:48 AM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    According to your attachment a variation of the same way you already have in your previous threads as a beginner starter
    to paste only to the Sheet3 (Consolidate) worksheet class module :

    PHP Code: 
    Sub Demo1()
        
    Dim L&, oDic As ObjectS&, VR&
            
    1
            UsedRange
    .Clear
            Set oDic 
    CreateObject("Scripting.Dictionary"):  oDic.CompareMode 1
            Application
    .ScreenUpdating False
        With CreateObject
    ("Scripting.Dictionary")
            .
    CompareMode 1
        
    For Sheets("BR1 Cat").Index To Index 1
            V 
    Sheets(S).[A1].CurrentRegion
            Cells
    (L1).Resize(UBound(V), 3) = V
            L 
    UBound(V)
        For 
    1 To UBound(V)
            .
    Item(V(R1)) = .Item(V(R1)) + V(R2)
             
    oDic(V(R3)) = oDic(V(R3)) + V(R2)
        
    Next RS
            
    [A1].CurrentRegion.RemoveDuplicates 1
            
    [B1].Resize(.Count) = Application.Transpose(.Items)
           .
    RemoveAll
        End With
            
    [I1:J1].Resize(oDic.Count) = Application.Transpose(Array(oDic.KeysoDic.Items))
            
    Application.ScreenUpdating True
            oDic
    .RemoveAll:   Set oDic Nothing
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Thanks for the help.


    I get a run time error -Object required and the code below is highlighted



    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Macro to Combine Data


    No issue on my side with your attachment, works as expected …

  5. #5
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Macro to Combine Data

    Hello Howardc1001.
    In the version I attached, the two sheets that are the source of the data have to be to the right of the 'Man Result' sheet:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  6. #6
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Thanks Beyond Excel. Your code works 100%

  7. #7
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Hi Marc


    I have attached the workbook with your code and have tried on my Laptop and I still get a run time error


    Please test & correct
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Macro to Combine Data


    You felt into the bad reader trap ‼ So just delete the useless Module1 and
    as plain text explained in post #2 paste the VBA demonstration where it must be : into the worksheet module …

  9. #9
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Macro to Combine Data

    Quote Originally Posted by Howardc1001 View Post
    Thanks Beyond Excel. Your code works 100%
    It was my pleasure to provide you with a short, efficient and "clean" (understandable on first reading) VBA code: Thanks for the +Rep!


  10. #10
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Hi Marc


    I have amended your code ad Object required no longer appears


    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Macro to Combine Data


    All your mods are useless - one is even wrong ! - if you just well read and follow the post #2 dark red direction …

    And according to VBA help - a must read, at kid level ! - you are wrong as there is obviously not any missng Next statement
    or my VBA demonstration can't work so I never would post it !
    Last edited by Marc L; 03-31-2023 at 03:05 PM.

  12. #12
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Thanks for your input Marc and comments which I have noted

    My apologies. I should have taken the time to read your post #2 instead of just copying your code

    Your instruction was simple and clear
    Last edited by Howardc1001; 03-31-2023 at 10:52 PM.

  13. #13
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    I have a another example to copy data from sheets F and I BR1 to the last sheet by copying the data in Cols A to J up to the last row containing data in Col A on these sheets and to combine the data and paste into sheet "Consolidated"

    I have tried to amend the orignal code, but cannot get the code to work

    Kindly test & amend
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Macro to Combine Data


    Quote Originally Posted by Howardc1001 View Post
    Your instruction was simple and clear
    Thanks ! No problemo, I'll be back !

  15. #15
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Thanks Marc. Look forward to your updated Code

    I have Data in Cols A:J on sheets BR1 F and I Data to the last sheet


    I need to combine the values In Col E where the descriptions In Col B are the same as well as combining the values in Col J where the descriptions in Col I are the same and paste these in the same cols as on these sheets on sheet consolidated


    I have manually added the values to show the result on workbook "Consolidated Sales Manual Calc.xlsm"


    Kindly test and provide me with code to combine the values
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Macro to Combine Data


    As the worksheets order is not the same than your initial post attachment so you must fit the worksheets loop accordingly …

    So is your real workbook e x a c t l y like your previous post attachment or is it 'cause of post #5 ?!

  17. #17
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Hi Marc

    It must be as per post # 15

  18. #18
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Macro to Combine Data


    The data worksheets have always the same number of rows in the same order ?

    As a reminder : poor explanation & attachment, poor code …

  19. #19
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Macro to Combine Data

    Hello.
    What follows is the code shown in post #5, but adapted to the data as you have it in post #15.
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    The number of rows will vary as this is sample data. The Account numbers in Col A will be in numerical order

  21. #21
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Thanks for the help -Beyond Excel

    Your code is perfect.

    Please explain significance of code below

    Please Login or Register  to view this content.

  22. #22
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this ...


    According to your post #15 attachment an Excel / VBA basics demonstration to paste to the Sheet3 (Consolidate) worksheet module :

    PHP Code: 
    Sub Demo2()
        
    Dim S&, R&, VT#(), L&
            
    UsedRange.Clear
        With Application
            
    .ScreenUpdating False
        
    For 2 To Sheets.Count
            With Sheets
    (S).UsedRange
                
    .Columns("A:B").Copy Cells(11)
                 
    + .Rows.Count
            End With
        Next
            Range
    ("A1:B" R).RemoveDuplicates 1
            V 
    = [A1].CurrentRegion.Columns(1)
            
    ReDim T(1 To UBound(V), 0)
        
    With New Collection
                
    For 1 To UBound(V):  .Add RCStr(V(R1)):  Next
            
    For 2 To Sheets.Count
            
    For 1 To Sheets(S).UsedRange.Rows.Count
                L 
    = .Item(Sheets(S).Cells(R1).Text)
                
    T(L0) = T(L0) + Sheets(S).Cells(R5)
            
    Next RS
        End With
            
    [E1].Resize(UBound(T)) = T
            
    [I1:J1] = Array(Sheets(2).[I1], .Sum(T))
            .
    ScreenUpdating True
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  23. #23
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Try this ...

    Many Thanks Marc.

    I have tested your code and it works perfectly

    Should there be values in Col C & D do I amend this part of your code :

    Please Login or Register  to view this content.


    to

  24. #24
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Macro to Combine Data

    Quote Originally Posted by Howardc1001 View Post
    Thanks for the help -Beyond Excel. Your code is perfect.
    Please explain significance of code below:
    With pleasure:
    a) The data is collected in a recordset: Rst
    b) The recordset has 3 fields: F1, F2 and F3 with the following characteristics:

    F1: Long
    F2:String
    F3:Double

    c) The part of the VBA code that you show is the way to define such fields.

    d) This has not been the case, but before downloading the data to the sheet using the ".CopyFromRecordset" method, an ordering could be done by any of the 3 fields, for example.

  25. #25
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,725

    Re: Macro to Combine Data

    Thanks for the explanation

  26. #26
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Try this ...


    Quote Originally Posted by Howardc1001 View Post
    Should there be values in Col C & D
    Maybe yes, maybe no : no clue as - again - it depends on the exact layout and the exact expected result !
    Proceeding like you do just means you have the Excel / VBA skills to fit yourself any code any helper can share
    but when it's not the case, that's just a non sense, a waste of time …

    Anyway, thanks for the rep' !

+ 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] VBA Macro to combine data in Worksheets
    By mackeroni in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-29-2020, 01:32 AM
  2. Macro to combine data
    By GCLIFTON in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-20-2017, 12:52 PM
  3. macro to combine data
    By recain in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-04-2014, 09:35 PM
  4. Macro to combine data
    By Madalene in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2014, 05:46 AM
  5. Macro to Combine Rows of Data
    By jsolder in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-26-2013, 12:58 PM
  6. [SOLVED] Using Combine macro to combine multiple worksheets - need to modify to paste formulas
    By DLSmith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2012, 09:07 AM
  7. Macro to rearrange data, plot chart and combine data
    By francisobrien2007 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 11-13-2010, 11:11 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