Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Copy data from Unique files into Masterfile all the files in the same folder.

  1. #11
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Rep Power
    0
    Hi Alan,
    The project is simple. Our team consists of 16 team members.

    Our team leader imports system generated data into the zMaster.xlxm file populating columns B, C and D. The file he imports has nearly 1000 lines daily.

    He then runs a macro to populate column N with the names of the team members. Column A populates automatically just a serial number.

    He then distributes the work to all the 16 members by running the macro which I used from the following site and you helped modify.

    https://stackoverflow.com/questions/...-a-column?rq=1

    The above macro also populates the column F.

    Once the work is distributed, team members completed their tasks and populate columns G to K and Column M

    Later in the evening the team leader then runs the following and consolidates the work. That way he knows the work that is completed.

    Code:
    Sub BringInAllCompletedData()
        Call SortAllFiles
        Call LoopThroughDirectory
        Call UpdateDateInSheet1ColK
        Call UpdateOriginalData
        Call ClearSheet1
    End Sub
    Previously all this was done with MS Access. Our office recently had new hardware installed and for financial reason decided not to have MS Access and so we had to come up with this solution.

    I had to run the macro SortAllFiles because for some reason, if sorting was not done data was not being imported into the zMaster.xlsm

    Now it is all working well. I will try and put it all in one macro later down the track.

    I hope this explanation has helped you understand my project.

    One small question please. How do I modify the macro to use a template named template.xlsm placed within a folder in the same folder instead of creating a xlsx file?

    https://stackoverflow.com/questions/...-a-column?rq=1

    The path were the template is, is ThisWorkbook.Path & ""Template”\”Template.xlsm

    Alan, thank you for all your encouragement and feedback.

  2. #12
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,521
    Rep Power
    10
    Hi Raghu,
    Most things seem very simple when you are familiar – A good friend of mine I had to help a lot in the school and still do in further life as he is very slow at understanding the simplest things. But he was very motivated and now is a very successful pilot flying the largest passenger machines – he is very familiar with flying machines and all that goes with it, but that’s about all he can do, Lol.. ( Even amongst his colleagues he is still known as a bit of an idiot ( but a confident Pilot I might add )
    I have a large File used daily by my wife for her necessary diets. I find it so simple that it bores me to try to explain it to her. But this winter I have been trying for days to explain it to her so that she takes over it all herself.. but after days she still understands very little… I expect I will give up and try again next Winter, Lol…

    I am not a professional programmer, but some I know tell me that the most difficult thing they ever have to do is understand someone else’s code. So once again , it is very good that you have persevered and got your own solution. I personally would always use a more complicated code that I undrsatand rather than a “seemingly” simpler which I do not understand. My goal is always to have a clever/ simple code which I also understand. Only sometimes I manage that, lol..

    For two years I have been learning VBA programming now and I am continually surprised how many very experienced programmers use some very elegant codes which they do not understand. I find that quite worrying.. Much of the coding controlling our lives is understood by nobody. I can’t help thinking that will lead to a catastrophe one day….


    _.___________-

    Thanks for the further explanation.
    I see now that the start point, as far as the code discussions in this thread are concerned is the running of the final code from your other Thread, http://www.excelfox.com/forum/showth...them#post10543 . That is perhaps obvious with hindsight , but it wasn’t initially.
    So, just for future reference…
    From Zip Foldere, “WorkDistributed 15MAR18”
    running the macro which I used from the following site and you helped modify.
    https://stackoverflow.com/questions/...-a-column?rq=1
    The above macro also populates the column F.


    WorkDistributed 15MAR18
    Work Distribution
    All Files there are the “Afters
    File “zMaster.xlsm” would be the “Before” if the dates were removed from column F
    So this was your after
    Row\Col
    D
    E
    F
    G
    H
    1
    Qty
    Total
    Distributed
    Task1
    Task2
    2
    22
    $ 1,210.00
    15. Mrz 18
    3
    7
    $ 95.62
    15. Mrz 18
    4
    5
    $ 64.95
    15. Mrz 18

    Row\Col
    E
    F
    G
    H
    I
    44
    $ 33,011.55
    15. Mrz 18
    45
    $ 11.99
    15. Mrz 18
    46
    $ 74,940.99
    15. Mrz 18
    47
    48
    Worksheet: OriginalData
    If I delete the dates in column F then I get the before:
    Row\Col
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    1
    Price
    Qty
    Total
    Distributed
    Task1
    Task2
    Task3
    Task4
    Completed
    Consolidated
    Comments
    Team Member
    2
    $ 55.00
    22
    $ 1,210.00
    Raghu
    3
    $ 13.66
    7
    $ 95.62
    John
    4
    $ 12.99
    5
    $ 64.95
    Greg
    5
    $ 8.51
    12
    $ 102.12
    Margaret
    Worksheet: OriginalData

    If in the before I run the code Sub ExportByName on the before then I get
    _ the after ( but with today’s data )
    Row\Col
    E
    F
    G
    H
    1
    Total
    Distributed
    Task1
    Task2
    2
    $ 1,210.00
    02.Apr.2018
    3
    $ 95.62
    02.Apr.2018
    4
    $ 64.95
    02.Apr.2018
    Worksheet: OriginalData
    ¬_ In addition to the above change to the master file I get the 4 data files , Raghu , John , Greg, Margaret.
    For example “Raghu.xlsx”
    Row\Col
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    1
    Total
    Distributed
    Task1
    Task2
    Task3
    Task4
    Completed
    Consolidated
    Comments
    Team Member
    2
    $ 1,210.00
    02.Apr.2018 Raghu
    3
    $ 447.64
    02.Apr.2018 Raghu
    4
    $ 74,940.99
    02.Apr.2018 Raghu
    Worksheet: Tabelle1
    This is the same as the File from Zip File, “WorkDistributed 15MAR18” you gave , but just the date, as expected is different.
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    1
    S No
    Item
    Price
    Qty
    Total
    Distributed
    Task1
    Task2
    Task3
    Task4
    Completed
    Consolidated
    Comments
    Team Member
    2
    1
    ABC01
    $ 55.00
    22
    $ 1,210.00
    15. Mrz 18
    Raghu
    3
    5
    ABC05
    $ 7.22
    62
    $ 447.64
    15. Mrz 18
    Raghu
    4
    9
    ABC09
    $ 741.99
    101
    $ 74,940.99
    15. Mrz 18
    Raghu
    Worksheet: FromRaghu

    So that is the situation finished with the Work Distribution

  3. #13
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,521
    Rep Power
    10
    Zip Folder “WorkDistributedAndConsolidated 16MAR18” .. etc
    ( and further to final code solution )
    So the last post produced the “work” which was distributed to the team members at the start of the day.
    At some time through out the day the members may complete their work and so…..” ….Once the work is distributed, team members completed their tasks and populate columns G to K and Column M……”

    So for example from the last post we had the situation for “Raghu.xlsx” of this before any entry by you ( Raghu ).. _..
    http://www.excelfox.com/forum/showth...0596#post10596

    _... Once the team member ( you, Raghu ) , has completed the work and filled in the worksheet appropriately, those above screenshots could change to something like this:
    http://www.excelfox.com/forum/showth...0597#post10597

    I could make a similar set of Files for all team members, having finally_...
    Files:
    “Raghu.xlsx” https://app.box.com/s/at411zdf611vjuds3kfo6ymdpecfzmis
    “John.xlsx” https://app.box.com/s/l05b9but8fc315q9d39nes7s19vi8vdm
    “Greg.xlsx” https://app.box.com/s/zoxbm3aok8g27xc0ceaxr084xtore6o4
    “Margaret.xlsx” https://app.box.com/s/i28p8ukgggwm4ndb3p692vu6tm0epn09
    _...Screenshots:
    http://www.excelfox.com/forum/showth...0598#post10598

    Those above would be typical situations at the end of the day, just before the “Consolidating” is to be done:

    Consolidating
    If I now understand the problem correctly, we want a code or codes to be used at the end of the day
    This required coding must:
    _ bring the information from columns G-K and M of the data files, for all rows of completed work, that is to say where the team Member has filled in a date in column K. ( This will also be the work not yet shown as consolidated in those data files in column L ).
    ( This “bringing in of the information for newly completed work at the end of the day” is what we are referring to as “Consolidating”)
    _ The current day of the consolidating is to be filled into the data sheets. '_-##)

    The reason why my code Sub Consolidate() is not doing what you want is that
    _1) I did not know that the code would be run again with additional data added somewhere.
    _2) In the example data from post #1 ( http://www.excelfox.com/forum/showth...he-same-folder : 040.zipUniqueNameColumnMovedToTheEnd.zip ) the list of names was in sections for each team member in column N of the master file “zMaster.xlsm”
    _3) I probably assumed/ guessed a few other things based on the supplied info

    Once again, with hindsight it appears obvious now what is now needed, but initially it is not always obvious..

    So I would have a zMasterAfterDistribution which could also be called a zMasterBeforeConsolidation:
    File: “zMasterBeforeConsolidation.xlsm”
    https://app.box.com/s/818q2ev3owpini2202n3dqp3xxicfeif
    Screenshot:
    http://www.excelfox.com/forum/showth...age6#post10599
    http://www.excelfox.com/forum/showth...0599#post10599

    So.... just for fun....
    My code alternative:
    It does not seem to difficult to do. You have your own code, and in your position I would probably prefer to use a code I had written myself. But for completeness, this is the code I would probably use http://www.excelfox.com/forum/showth...0611#post10611


    If I run that code using_..
    Files:
    “Raghu.xlsx” https://app.box.com/s/at411zdf611vjuds3kfo6ymdpecfzmis
    “John.xlsx” https://app.box.com/s/l05b9but8fc315q9d39nes7s19vi8vdm
    “Greg.xlsx” https://app.box.com/s/zoxbm3aok8g27xc0ceaxr084xtore6o4
    “Margaret.xlsx” https://app.box.com/s/i28p8ukgggwm4ndb3p692vu6tm0epn09
    _...Screenshots:
    http://www.excelfox.com/forum/showth...0598#post10598
    File: “zMasterBeforeConsolidation.xlsm”
    https://app.box.com/s/818q2ev3owpini2202n3dqp3xxicfeif
    Screenshot:
    http://www.excelfox.com/forum/showth...age6#post10599
    http://www.excelfox.com/forum/showth...0599#post10599
    _... then I receive these results for the updated master Worksheet_..
    http://www.excelfox.com/forum/showth...0600#post10600
    _.. Those results look very similar to your results for a file from you:
    http://www.excelfox.com/forum/showth...0601#post10601
    http://www.excelfox.com/forum/showth...0602#post10602

    _. And here is a typical data File with the column L updated appropriately with the consolidation date:
    “Raghu.xlsx”:
    http://www.excelfox.com/forum/showth...0603#post10603

    _....
    If I add data as per your description of how your team leader goes about at the start of the _..
    _ add data,
    _..File: “zMasterAfter1stConsolidationNewData.xlsm” : https://app.box.com/s/ascky2qg47dzl85b4y7l8sy5qmr3goby
    Screenshot :
    http://www.excelfox.com/forum/showth...0604#post10604
    _.. and then run code Sub ExportByName() _..
    t.-.. I get then The results for
    _Updated master:
    http://www.excelfox.com/forum/showth...0605#post10605
    _Updated data worksheets
    http://www.excelfox.com/forum/showth...0606#post10606


    For the sake of this demo, I then assume most of the work is then done, so that for example we have data Files just before second consolidation:
    http://www.excelfox.com/forum/showth...0607#post10607

    Finally I run the code Sub consolidateToo() a second time
    Here the final results:
    Master File:
    http://www.excelfox.com/forum/showth...0608#post10608
    http://www.excelfox.com/forum/showth...0609#post10609
    data files:
    http://www.excelfox.com/forum/showth...0610#post10610





    _._____


    I could not follow the logic of your coding, but as I mentioned it is always difficult to follow someone else’s coding. I could not get a code run of Sub BringInAllCompletedData() to do anything close to what I perceive as required. But I may not be applying it correctly.
    I did the new code for completeness.
    If I were in your position then I would stay with the code you did yourself, if it is doing what you want.
    It is much easier for you to understand and maintain your own code in the future. You have actually done some interesting codes , so thanks again for sharing your solution.


    _._______________

    Finally I will try to answer your last question in next post.
    Last edited by DocAElstein; 04-03-2018 at 12:38 PM.

  4. #14
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,521
    Rep Power
    10
    Quote Originally Posted by RaghavendraPrabhu View Post
    ...
    One small question please. How do I modify the macro to use a template named template.xlsm placed within a folder in the same folder instead of creating a xlsx file?
    https://stackoverflow.com/questions/...-a-column?rq=1
    The path were the template is, is ThisWorkbook.Path & ""Template”\”Template.xlsm.
    I am not sure exactly what you are asking here. I am not too familiar with the code you referenced at the stack overflow Forum.
    If we are talking about the code, Sub ExportByName(), then it does not make a file if it already exists.
    If you always have a file ready at the specified location, then no new file will be made.

    Possibly you are asking for when you have no file for a team Member, then rather than make a new one, you open the template and save that with the missing team member file.?

    If that is your question, then you would remove
    Workbooks.Add
    and replace that with coding to open that template file, and then save it with the name for the new member, but remember to save it at the correct File path, which I assume would still want to be that for where the master file is.

    If you need more help on that modification then let me know.

    Alan

  5. #15
    Banned
    Join Date
    Jan 2019
    Posts
    37
    Rep Power
    0
    Quote Originally Posted by RaghavendraPrabhu View Post
    Hi Doc Elstein,

    I have sorted out the problem. It is a bit convoluted, but it is working. Please have a look at the code below.
    Code:
    Option Explicit
    Sub BringInAllCompletedData()
        Call SortAllFiles
        Call LoopThroughDirectory
        Call UpdateDateInSheet1ColK
        Call UpdateOriginalData
        Call ClearSheet1
    End Sub
    
    'https://www.mrexcel.com/forum/excel-questions/471802-vba-open-file-run-code-close-save-open-next-file.html
    Sub SortAllFiles()
        Dim folderPath As String
        Dim filename As String
        Dim wb As Workbook
        
        Application.DisplayAlerts = False
        
        folderPath = ActiveWorkbook.Path & "\" 'change to suit
        If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
            filename = Dir(folderPath & "*.xlsx")
                Do While filename <> ""
                    Application.ScreenUpdating = False
                    Set wb = Workbooks.Open(folderPath & filename)
                    'Call a subroutine here to operate on the just-opened workbook
                    If filename = "zmaster.xlsm" Then
                        Exit Sub
                    Else
                        Call SortSheet1InAllFiles
                    End If
                    filename = Dir
                Loop
            Application.ScreenUpdating = True
            Application.DisplayAlerts = True
    End Sub
    
    Sub SortSheet1InAllFiles()
    Dim MyFile As String
    Dim eRow As Long
    Dim RowsConsolidated As Long
    Dim LastRow As Long
    Dim i As Long
    
        eRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
        Cells.Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("K2:K" & eRow) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A1:N" & eRow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWorkbook.Save
        Range("A1").Select
        ActiveWorkbook.Close
        
    End Sub
    
    'http://www.exceltrainingvideos.com/transfer-data-multiple-workbooks-master-workbook-automatically/
     
    Sub LoopThroughDirectory()
    Dim MyFile As String
    Dim eRow As Long
    Dim LRL As Long
    Dim LRK As Long
    Dim i As Long
     
    Dim FilePath As String
    FilePath = ActiveWorkbook.Path & "\"
            
            Application.DisplayAlerts = False
            Application.ScreenUpdating = False
            Sheets("Sheet1").Activate
    MyFile = Dir(FilePath)
        Do While Len(MyFile) > 0
            If MyFile = "zmaster.xlsm" Then
                Exit Sub
            End If
           
            Workbooks.Open (FilePath & MyFile)
            LRK = Cells(Rows.Count, 11).End(xlUp).Offset(1, 0).Row 'Column L
            LRL = Cells(Rows.Count, 12).End(xlUp).Offset(1, 0).Row 'Column K
            
            For i = LRL To LRK
                    Range("A" & LRL & " : " & "K" & LRK).Copy
            Next
            ActiveWorkbook.Close
       
            eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(eRow, 1), Cells(eRow, 11))
    
            
            If MyFile = "zmaster.xlsm" Then
                Exit Sub
            End If
           
            Workbooks.Open (FilePath & MyFile)
            For i = LRL To LRK - 1
                If Range("L" & i).Value = "" Then
                    Range("L" & i).Value = Date
                    Columns("L:L").NumberFormat = "[$-C09]dd-mmm-yy;@"
                End If
            Next
            Range("A1").Select
            ActiveWorkbook.Save
            ActiveWorkbook.Close
           
            MyFile = Dir
            ActiveWorkbook.Save
        Loop
            
        Columns("A:D").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A" & eRow) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A1:D" & eRow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
                  
            Application.ScreenUpdating = True
            Application.DisplayAlerts = True
    End Sub
    
    Sub UpdateDateInSheet1ColK()
    Dim eRow As Long
    Dim i As Long
    
        Sheets("Sheet1").Activate
        eRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            For i = 2 To eRow
                If Range("K" & i) <> "" Then
                    Range("L" & i).Value = Format(Date, "dd/mmm/yyyy")
                End If
            Next
    End Sub
    
    'https://www.youtube.com/watch?v=AzhQ5KiNybk
    Sub UpdateOriginalData()
    Dim i As Integer
    Dim j As Integer
    Dim LastRow1 As Integer
    Dim LastRow2 As Integer
    Dim SNo As Double
    
    LastRow1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    LastRow2 = Sheets("OriginalData").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To LastRow1
        SNo = Sheets("Sheet1").Cells(i, "A").Value
            Sheets("OriginalData").Activate
            For j = 2 To LastRow2
                If Sheets("OriginalData").Cells(j, "A").Value = SNo Then
                        Sheets("Sheet1").Activate
                        Sheets("Sheet1").Range(Cells(i, "G"), Cells(i, "L")).Copy
                        Sheets("OriginalData").Activate
                        Sheets("OriginalData").Range(Cells(j, "G"), Cells(j, "L")).Select
                        ActiveSheet.Paste
                End If
            Next j
            Application.CutCopyMode = False
    Next i
        Sheets("OriginalData").Activate
        Cells.Select
        ActiveWorkbook.Save
        Selection.Columns.AutoFit
        Range("A1").Select
    
    End Sub
    
    Sub ClearSheet1()
    Dim eRow As Long
    
        Sheets("Sheet1").Activate
        eRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
        Range("A2:O" & eRow).Select
        Selection.ClearContents
        Selection.Columns.AutoFit
        Range("A1").Select
        ActiveWorkbook.Save
    End Sub
    Thank you for all your suggestions and input. What I have done is a long drawn process. Maybe it can be written into one macro.

    I will consider this as solved.

    Thank you for all your help, time and patience.Lucky Patcher 9Apps VidMate

    Regards

    Raghu
    i can't download the files why ?
    Last edited by klimbo123; 01-21-2019 at 05:32 PM.

  6. #16
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,521
    Rep Power
    10
    Quote Originally Posted by klimbo123 View Post
    i can't download the files why ?
    Hi klimbo123
    Welcome to excelfox
    Please explain yourself, in much more detail, if you want any help here, at excelfox
    Alan

    P.s. If the probem for you is the files from here http://www.excelfox.com/forum/showth...lder#post10591
    Then here they are again at a file sharing site:

    WorkCondolidated 18MAR18 _____https://app.box.com/s/tythnc8zge5g1ezqnd03uzzsgufb1mnp
    WorkDistributed 15MAR18 ___ https://app.box.com/s/jfpeq908wc122x18vvjub2avtmcrg8p0
    WorkDistributedAndConsolidated 16MAR18 : _ https://app.box.com/s/oxih69lkebmenqi6rgshzdhonf1lbddy
    Last edited by DocAElstein; 01-21-2019 at 01:54 PM.

Similar Threads

  1. Replies: 7
    Last Post: 03-23-2018, 02:02 PM
  2. Replies: 2
    Last Post: 03-09-2015, 11:26 PM
  3. Code to open up files in folder and sub-folder
    By Howardc in forum Excel Help
    Replies: 7
    Last Post: 08-26-2014, 07:01 AM
  4. Replies: 15
    Last Post: 08-23-2013, 12:03 PM
  5. Macro to copy data from a set of excel files
    By Sreejesh Menon in forum Excel Help
    Replies: 5
    Last Post: 11-15-2012, 11:17 AM

Posting Permissions

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