+ Reply to Thread
Results 1 to 13 of 13

insert three columns and bring the values based on matching with two workbooks

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    insert three columns and bring the values based on matching with two workbooks

    hello
    I have two workbooks report1,2 contains data should match in COL B,C,D with file OUTPUT Also IN COL B,C,D then inserting three columns and brings the values in COL sales and purchase and calculate in COL QTY i need show the formula in COL QTY and it should summing the values from files each COL SALES AND PURCHASE and add to file OUTPUT and if there are brands are existed in files and not existed in file output should add to file output as highlighted by yellow but when add them should match col A in files with COL A in file output and add it before total
    note : the files in the same folder and may be increase the files also the data in output is increasable in rows I mean in COL A,B,C,D
    I put the result in sheet after and the original data in sheet before but should show the result in sheet1 , and every time run the macro should insert three columns with same borders and formatting also formulas .
    if this is not clear please inform me
    Attached Files Attached Files
    Last edited by leap out; 04-24-2021 at 04:18 AM.

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

    Re: insert three columns and bring the values based on matching with two workbooks

    Option Explicit
    
    Sub test()
        Dim x, msg As String, dic As Object
        GetData ThisWorkbook.Path, x, msg
        If Not IsArray(x) Then MsgBox "No files in the forlder": Exit Sub
        If Len(msg) Then MsgBox msg: Exit Sub
        Set dic = CreateObject("Scripting.Dictionary")
        GetDic x, dic
        OutPut "Before", x, dic
        Set dic = Nothing
    End Sub
    
    Private Sub GetData(myDir As String, x, msg As String)
        Dim fn As String, cn As Object, rs As Object
        fn = Dir(ThisWorkbook.Path & "\*.xls")
        If fn = ThisWorkbook.Name Then fn = Dir()
        If fn = "" Then msg = "No file": Exit Sub
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
        With cn
            .Provider = "Microsoft.Ace.OLEDB.12.0"
            .Properties("Extended Properties") = "Excel 12.0;HDR=Yes;"
            .Open ThisWorkbook.Path & "\" & fn
        End With
        Do While fn <> ""
            If fn <> ThisWorkbook.Name Then
                If IsEmpty(x) Then
                    ReDim x(1 To 1)
                Else
                    ReDim Preserve x(1 To UBound(x) + 1)
                End If
                On Error Resume Next
                rs.Open "Select `CATOGERY`, `BRAND`, `TYPE`, `MONAFACTURE`, Sum(`Purchase`), Sum(`Sales`) From `PR$` " & _
                "In '" & ThisWorkbook.Path & "\" & fn & "' 'Excel 12.0;''HDR:=Yes;''' Group By `CATOGERY`, `BRAND`, `TYPE`, `MONAFACTURE`;", cn, 3
                If Err.Number <> 0 Then msg = fn & " has problem": Exit Do
                On Error GoTo 0
                x(UBound(x)) = rs.GetRows: rs.Close
            End If
            fn = Dir
        Loop
        Set cn = Nothing: Set rs = Nothing
    End Sub
    
    Private Sub GetDic(x, dic As Object)
        Dim i As Long, ii As Long, txt As String, w
        For i = 1 To UBound(x)
            For ii = 0 To UBound(x(i), 2)
                If Not dic.exists(x(i)(0, ii)) Then
                    Set dic(x(i)(0, ii)) = CreateObject("Scripting.Dictionary")
                End If
                txt = Join(Array(x(i)(1, ii), x(i)(2, ii)), "")
                x(i)(4, ii) = IIf(IsNull(x(i)(4, ii)), 0, x(i)(4, ii))
                x(i)(5, ii) = IIf(IsNull(x(i)(5, ii)), 0, x(i)(5, ii))
                If Not dic(x(i)(0, ii)).exists(txt) Then
                    dic(x(i)(0, ii))(txt) = Array(x(i)(1, ii), x(i)(2, ii), x(i)(3, ii), x(i)(4, ii), x(i)(5, ii))
                Else
                    w = dic(x(i)(0, ii))(txt): w(3) = w(3) + x(i)(4, ii)
                    w(4) = w(4) + x(i)(5, ii): dic(x(i)(0, ii))(txt) = w
                End If
            Next
        Next
    End Sub
    
    Private Sub OutPut(wsName As String, x, dic As Object)
        Dim i As Long, ii As Long, iii As Long, n As Long
        Dim a, b, e, s, txt As String, temp, r As Range
        Application.ScreenUpdating = False
        With Sheets(1).Cells(1).CurrentRegion
            .Columns(.Columns.Count - 2).Resize(, 3).AutoFill _
            Destination:=.Columns(.Columns.Count - 2).Resize(, 6)
            With .Cells(1).CurrentRegion.Offset(2).Resize(.Rows.Count - 2)
                .Columns(.Columns.Count - 2).Resize(, 3).ClearContents
                a = .Value: .ClearContents: .Borders.LineStyle = xlNone
                .Font.Bold = False
                ReDim b(1 To Rows.Count, 1 To UBound(a, 2))
                .Interior.ColorIndex = xlNone
                For i = 1 To UBound(a, 1)
                    If a(i, 1) <> "" Then temp = a(i, 1)
                    If a(i, 2) <> "TOTAL" Then
                        n = n + 1
                        For ii = 1 To UBound(a, 2) - 3
                            b(n, ii) = a(i, ii)
                        Next
                        If dic.exists(temp) Then
                            txt = Join(Array(a(i, 2), a(i, 3)), "")
                            If dic(temp).exists(txt) Then
                                b(n, UBound(a, 2) - 2) = dic(temp)(txt)(3)
                                b(n, UBound(a, 2) - 1) = dic(temp)(txt)(4)
                                b(n, UBound(a, 2)) = b(n, UBound(a, 2) - 2) - b(n, UBound(a, 2) - 1)
                                dic(temp).Remove txt
                                If dic(temp).Count = 0 Then dic.Remove temp
                            End If
                        End If
                    Else
                        If dic.exists(temp) Then
                            If dic(temp).Count Then
                                For Each e In dic(temp)
                                    n = n + 1
                                    For ii = 0 To 2
                                        b(n, ii + 2) = dic(temp)(e)(ii)
                                    Next
                                    b(n, UBound(b, 2) - 2) = dic(temp)(e)(3)
                                    b(n, UBound(b, 2) - 1) = dic(temp)(e)(4)
                                    b(n, UBound(a, 2)) = b(n, UBound(a, 2) - 2) - b(n, UBound(a, 2) - 1)
                                    dic(temp).Remove e
                                Next
                                If dic(temp).Count = 0 Then dic.Remove temp
                            End If
                        End If
                        n = n + 1: b(n, 2) = "TOTAL"
                    End If
                Next
                If dic.Count Then
                    For Each e In dic
                        n = n + 1: b(n, 1) = e
                        For Each s In dic(e)
                            For ii = 0 To 2
                                b(n, ii + 2) = dic(e)(s)(ii)
                            Next
                            b(n, UBound(b, 2) - 2) = dic(e)(s)(3)
                            b(n, UBound(b, 2) - 1) = dic(e)(s)(4)
                            b(n, UBound(a, 2)) = b(n, UBound(a, 2) - 2) - b(n, UBound(a, 2) - 1)
                            n = n + 1
                        Next
                        b(n, 2) = "TOTAL"
                    Next
                End If
                With .Resize(n)
                    .Value = b
                    For Each r In .Columns(3).SpecialCells(2).Areas
                        If r(r.Count + 1, 0) = "TOTAL" Then
                            r(r.Count + 1, 3).Resize(, UBound(b, 2) - 4).Formula = _
                            "=sum(" & r.Offset(, 2).Address(0, 0) & ")"
                            With r(r.Count + 1, 0).Resize(, UBound(b, 2) - 1)
                                .Interior.Color = 11573124
                                .Font.Bold = True
                            End With
                        End If
                    Next
                    .HorizontalAlignment = xlCenter
                    .Offset(, 1).Resize(, .Columns.Count - 1).Borders.Weight = 2
                    .Rows.AutoFit
                End With
            End With
        End With
        Application.ScreenUpdating = True
    End Sub

  3. #3
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: insert three columns and bring the values based on matching with two workbooks

    it's very impressive ! many thanks for astonishing achieving just if it's possible I would mod the formula in QTY when insert the three columns every time insert a new three columns should be the formula as put in COL QTY =QTY in previous COL + purchase in a new column - sales in a new column for more to understand it please check the formula in QTY
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: insert three columns and bring the values based on matching with two workbooks

    isn't possible?

  5. #5
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: insert three columns and bring the values based on matching with two workbooks

    any idea to mod the formula ?

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

    Re: insert three columns and bring the values based on matching with two workbooks

    You must clearly explain the difference of the formulae like in the other thread under different user name.

    I really don't want to deal with such lazy poster...

  7. #7
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: insert three columns and bring the values based on matching with two workbooks

    I thought post#3 is clear when I put the formula in last column in J the idea is when insert a new three columns the last column which calculate the values depends on precede the last column as in sheet result1 last column which calculate the values is G and the precede last column is COL G then when calculate and show the values in COL J= 32+65-25 =77 and if I insert a new three columns as in sheet result2 last column which calculate the values is M and the precede last column is COL J then when calculate and show the values in COL M= 77+65-20 =77 the same thing whit the others rows except TOATL row and every time insert a new three columns the formula should be the precede last column + a new purchase - a new sales
    I hope this help please check two result result1,2
    Attached Files Attached Files
    Last edited by leap out; 04-28-2021 at 04:36 AM.

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

    Re: insert three columns and bring the values based on matching with two workbooks

    Explain why formula in every TOTAL row differs from column by column, row by row??????

  9. #9
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: insert three columns and bring the values based on matching with two workbooks

    it shows the total for purchases and sales and qty(stock) for each category for instance category for OIL-208L it shows how calculate Total values the purchases and sales and qty in row total , it calculates for each category separately and I've found some cells when I sum them , they're not within the formula , I fixed it and updated the file
    Last edited by leap out; 04-28-2021 at 04:34 AM.

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

    Re: insert three columns and bring the values based on matching with two workbooks

    You must review the workbook for yourself if nobody answer.

    No one want to do such a "GUESS" work and no one but only you know the details of what you are trying to do.

    Sub test()
        Dim x, msg As String, dic As Object
        GetData ThisWorkbook.Path, x, msg
        If Not IsArray(x) Then MsgBox "No files in the forlder": Exit Sub
        If Len(msg) Then MsgBox msg: Exit Sub
        Set dic = CreateObject("Scripting.Dictionary")
        GetDic x, dic
        OutPut "Before", x, dic
        Set dic = Nothing
    End Sub
    
    Private Sub GetData(myDir As String, x, msg As String)
        Dim fn As String, cn As Object, rs As Object
        fn = Dir(ThisWorkbook.Path & "\*.xls")
        If fn = ThisWorkbook.Name Then fn = Dir()
        If fn = "" Then msg = "No file": Exit Sub
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
        With cn
            .Provider = "Microsoft.Ace.OLEDB.12.0"
            .Properties("Extended Properties") = "Excel 12.0;HDR=Yes;"
            .Open ThisWorkbook.Path & "\" & fn
        End With
        Do While fn <> ""
            If fn <> ThisWorkbook.Name Then
                If IsEmpty(x) Then
                    ReDim x(1 To 1)
                Else
                    ReDim Preserve x(1 To UBound(x) + 1)
                End If
                On Error Resume Next
                rs.Open "Select `CATOGERY`, `BRAND`, `TYPE`, `MONAFACTURE`, Sum(`Purchase`), Sum(`Sales`) From `PR$` " & _
                "In '" & ThisWorkbook.Path & "\" & fn & "' 'Excel 12.0;''HDR:=Yes;''' Where `CATOGERY` Is Not Null And `BRAND` " & _
                "Is NOt Null And `TYPE` Is Not Null And `MONAFACTURE` Is Not Null Group By `CATOGERY`, `BRAND`, `TYPE`, `MONAFACTURE`;", cn, 3
                If Err.Number <> 0 Then msg = fn & " has problem": Exit Do
                On Error GoTo 0
                x(UBound(x)) = rs.GetRows: rs.Close
            End If
            fn = Dir
        Loop
        Set cn = Nothing: Set rs = Nothing
    End Sub
    
    Private Sub GetDic(x, dic As Object)
        Dim i As Long, ii As Long, txt As String, w
        For i = 1 To UBound(x)
            For ii = 0 To UBound(x(i), 2)
                If Not dic.exists(x(i)(0, ii)) Then
                    Set dic(x(i)(0, ii)) = CreateObject("Scripting.Dictionary")
                End If
                txt = Join(Array(x(i)(1, ii), x(i)(2, ii)), "")
                x(i)(4, ii) = IIf(IsNull(x(i)(4, ii)), 0, x(i)(4, ii))
                x(i)(5, ii) = IIf(IsNull(x(i)(5, ii)), 0, x(i)(5, ii))
                If Not dic(x(i)(0, ii)).exists(txt) Then
                    dic(x(i)(0, ii))(txt) = Array(x(i)(1, ii), x(i)(2, ii), x(i)(3, ii), x(i)(4, ii), x(i)(5, ii))
                Else
                    w = dic(x(i)(0, ii))(txt): w(3) = w(3) + x(i)(4, ii)
                    w(4) = w(4) + x(i)(5, ii): dic(x(i)(0, ii))(txt) = w
                End If
            Next
        Next
    End Sub
    
    Private Sub OutPut(wsName As String, x, dic As Object)
        Dim i As Long, ii As Long, iii As Long, n As Long
        Dim a, b, e, s, txt As String, temp, r As Range
        Application.ScreenUpdating = False
        With Sheets(1).Cells(1).CurrentRegion
            .Columns(.Columns.Count - 2).Resize(, 3).AutoFill _
            Destination:=.Columns(.Columns.Count - 2).Resize(, 6)
            With .Cells(1).CurrentRegion.Offset(2).Resize(.Rows.Count - 2)
                .Columns(.Columns.Count - 2).Resize(, 3).ClearContents
                a = .Value: .ClearContents: .Borders.LineStyle = xlNone
                .Font.Bold = False
                ReDim b(1 To Rows.Count, 1 To UBound(a, 2))
                .Interior.ColorIndex = xlNone
                For i = 1 To UBound(a, 1)
                    If a(i, 1) <> "" Then temp = a(i, 1)
                    If a(i, 2) <> "TOTAL" Then
                        n = n + 1
                        For ii = 1 To UBound(a, 2) - 3
                            b(n, ii) = a(i, ii)
                        Next
                        If dic.exists(temp) Then
                            txt = Join(Array(a(i, 2), a(i, 3)), "")
                            If dic(temp).exists(txt) Then
                                b(n, UBound(a, 2) - 2) = dic(temp)(txt)(3)
                                b(n, UBound(a, 2) - 1) = dic(temp)(txt)(4)
                                dic(temp).Remove txt
                                If dic(temp).Count = 0 Then dic.Remove temp
                            End If
                        End If
                    Else
                        If dic.exists(temp) Then
                            If dic(temp).Count Then
                                For Each e In dic(temp)
                                    n = n + 1
                                    For ii = 0 To 2
                                        b(n, ii + 2) = dic(temp)(e)(ii)
                                    Next
                                    b(n, UBound(b, 2) - 2) = dic(temp)(e)(3)
                                    b(n, UBound(b, 2) - 1) = dic(temp)(e)(4)
                                    dic(temp).Remove e
                                Next
                                If dic(temp).Count = 0 Then dic.Remove temp
                            End If
                        End If
                        n = n + 1: b(n, 2) = "TOTAL"
                    End If
                Next
                If dic.Count Then
                    For Each e In dic
                        n = n + 1: b(n, 1) = e
                        For Each s In dic(e)
                            For ii = 0 To 2
                                b(n, ii + 2) = dic(e)(s)(ii)
                            Next
                            b(n, UBound(b, 2) - 2) = dic(e)(s)(3)
                            b(n, UBound(b, 2) - 1) = dic(e)(s)(4)
                            n = n + 1
                        Next
                        b(n, 2) = "TOTAL"
                    Next
                End If
                With .Resize(n)
                    .Value = b
                    For Each r In .Columns(3).SpecialCells(2).Areas
                        r.Offset(, .Columns.Count - 3).FormulaR1C1 = "=rc[-3]+rc[-2]-rc[-1]"
                        If r(r.Count + 1, 0) = "TOTAL" Then
                            r(r.Count + 1, 3).Resize(, UBound(b, 2) - 4).Formula = _
                            "=sum(" & r.Offset(, 2).Address(0, 0) & ")"
                            With r(r.Count + 1, 0).Resize(, UBound(b, 2) - 1)
                                .Interior.Color = 11573124
                                .Font.Bold = True
                            End With
                        End If
                    Next
                    .HorizontalAlignment = xlCenter
                    .Offset(, 1).Resize(, .Columns.Count - 1).Borders.Weight = 2
                    .Rows.AutoFit
                End With
            End With
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by jindon; 04-28-2021 at 05:48 AM.

  11. #11
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: insert three columns and bring the values based on matching with two workbooks

    thanks and I will take it seruiosly in the future

    suddenly shows this error "invalid use of null ' but if I returns download the files as in post#1 it works normally do you have any idea ?
    HTML Code: 
    last thing , can I show the formula in last column ?

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

    Re: insert three columns and bring the values based on matching with two workbooks

    Formula will only be inserted to the last column each time.
    So the formula other than the last column will be converted to the value.

    Code in my post #10 has been updated.

  13. #13
    Forum Contributor
    Join Date
    01-08-2021
    Location
    Asia
    MS-Off Ver
    2016
    Posts
    324

    Re: insert three columns and bring the values based on matching with two workbooks

    thanks for every thing ,my apologies about my post is not clear what I exactly want

+ 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. Find matching value from 2 Columns based on several lokup values
    By FlamingoCup in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 03-30-2020, 09:30 AM
  2. VBA Sum based on matching values in multiple columns
    By TheOneWeDreamOf in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-23-2019, 08:00 PM
  3. [SOLVED] How to highlight a row based on matching values from separate columns
    By MyStix01 in forum Excel General
    Replies: 2
    Last Post: 09-21-2017, 08:43 PM
  4. Align Rows based on Matching Values in 2 Columns
    By cmv040 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-05-2016, 07:49 PM
  5. [SOLVED] VBA to Insert Blank Row based on matching values
    By nirvehex in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-29-2015, 04:36 PM
  6. matching values based on criteria and return values from another columns
    By lizard54 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2012, 11:29 AM
  7. Matching columns of 2 Workbooks and copying the match values to a worksheet.
    By shrimic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-10-2012, 08:08 AM

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