Page 1 of 2 12 LastLast
Results 1 to 10 of 16

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

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Rep Power
    0

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

    I have had a look at various data merging macros that merge data from different workbooks into the master workbook. They work but are not doing what I want.

    Please see 40.zip

    I want to copy column G to column L where Column K has a date and column L is blank from the unique workbooks into the Masterfile

    Paste this data into the Masterfile, lining with the serial numbers in column A. Update the column L with the date the macro is run.

    Go back to the unique file, update the date in column L and save and close the file.

    In Raghu.xlsx, record with Serial number 3 and 8 need not be copied as they have not been completed.

    I physically copied the data I want to be copied from Raghu.xlsx into zMaster.xlsm

    Thanks in advance and regards

    Raghavendra Prabhu040.zipUniqueNameColumnMovedToTheEnd.zip

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837
    https://www.eileenslounge.com/viewtopic.php?f=21&t=40701&p=314836#p314836
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314621#p314621
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314619#p314619
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314600#p314600
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314599#p314599
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314274#p314274
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314229#p314229
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314195#p314195
    https://www.eileenslounge.com/viewtopic.php?f=36&t=39706&p=314110#p314110
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40597&p=314081#p314081
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40597&p=314078#p314078
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314062#p314062
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40597&p=314054#p314054
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313971#p313971
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313909#p313909
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40574&p=313879#p313879
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313859#p313859
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313855#p313855
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313848#p313848
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313843#p313843
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313792#p313792
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313771#p313771
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313767#p313767
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313746#p313746
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313744#p313744
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40560&p=313741#p313741
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313622#p313622
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313575#p313575
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313573#p313573
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313563#p313563
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313555#p313555
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533
    https://www.eileenslounge.com/viewtopic.php?f=39&t=40265&p=313468#p313468
    https://www.eileenslounge.com/viewtopic.php?f=42&t=40505&p=313411#p313411
    https://www.eileenslounge.com/viewtopic.php?f=32&t=40473&p=313384#p313384
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40501&p=313382#p313382
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40501&p=313380#p313380
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40501&p=313378#p313378
    https://www.eileenslounge.com/viewtopic.php?f=32&t=40473&p=313305#p313305
    https://www.eileenslounge.com/viewtopic.php?f=44&t=40455&p=313035#p313035
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312889#p312889
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312886#p312886
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312752#p312752
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312734#p312734
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312727#p312727
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312724#p312724
    https://www.eileenslounge.com/viewtopic.php?f=44&t=40374&p=312535#p312535
    https://www.eileenslounge.com/viewtopic.php?p=312533#p312533
    https://www.eileenslounge.com/viewtopic.php?f=44&t=40373&p=312499#p312499
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 02-29-2024 at 09:37 PM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi Raghavendra

    I think this code will do some of what you want

    Code:
    Sub Raghavendra2() ' http://www.excelfox.com/forum/showthread.php/2238-Copy-data-from-Unique-files-into-Masterfile-all-the-files-in-the-same-folder
    Dim LisWb As Workbook
     Set LisWb = ThisWorkbook
    Dim Ws2 As Worksheet, Ws1 As Worksheet
     Set Ws2 = LisWb.Worksheets.Item(2): Set Ws1 = LisWb.Worksheets.Item(1):
    Dim strWb As String: Let strWb = Dir(ThisWorkbook.Path & "\" & "*" & ".xlsx", vbNormal)
       Do '    Loop  through all .xlsx Files in same Folder as this workbook
        Let Ws2.Range("A2:A1000").Value = "=" & "'" & ThisWorkbook.Path & "\[" & strWb & "]Sheet1'!$A2"
       Dim Lr As Long
        Let Lr = Ws2.Range("A2:A1000").Find(what:=0, after:=Ws2.Range("A2"), LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext).Row - 1
    '    Let Ws2.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Value = "=" & "'" & ThisWorkbook.Path & "\[" & strWb & "]Sheet1'!G2"
    '    Let Ws2.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Value = Evaluate("=IF(ISERR(" & Ws2.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Address & ")," & """""" & ",IF(" & Ws2.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Address & "=0," & """""" & "," & Ws2.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Address & "))")
    '    Let Ws2.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").NumberFormat = "d.mmm yy"
        Let Ws1.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Value = "=" & "'" & ThisWorkbook.Path & "\[" & strWb & "]Sheet1'!G2"
        Let Ws1.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Value = Evaluate("=IF(ISERR(" & Ws1.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Address & ")," & """""" & ",IF(" & Ws1.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Address & "=0," & """""" & "," & Ws1.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").Address & "))")
        Let Ws1.Range("G" & Ws2.Range("A2").Value + 1 & ":L" & Ws2.Range("A" & Lr & "").Value + 1 & "").NumberFormat = "d.mmm yy"
        
        Let strWb = Dir
       Loop While strWb <> ""
    End Sub

    _._____________________



    Here for example is one of your data worksheets ( "Ravi.xlsx" )

    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    1
    S No
    Item
    Price
    Qty
    Total
    Date Distributed
    Task1
    Task2
    Task3
    Task4
    Date Tasks Completed
    Date Consolidated
    Comments
    Team Member
    2
    26
    A2
    $ 35.00
    8
    $ 280.00
    17. Mrz 18
    Done N/A Done N/A
    17. Mrz 18
    Ravi
    3
    27
    A6
    $ 78.00
    63
    $ 4,914.00
    17. Mrz 18
    Done N/A Done N/A
    17. Mrz 18
    Ravi
    4
    28
    B2
    $ 11.00
    47
    $ 517.00
    17. Mrz 18
    N/A Done N/A Done
    17. Mrz 18
    Ravi
    5
    29
    B6
    $ 96.00
    8
    $ 768.00
    18. Mrz 18
    Need more information Ravi
    6
    30
    C2
    $ 74.00
    63
    $ 4,662.00
    18. Mrz 18
    Need more information Ravi
    7
    31
    C6
    $ 365.00
    47
    $ 17,155.00
    20. Mrz 18
    Need more information Ravi
    8
    32
    D2
    $ 33.00
    8
    $ 264.00
    20. Mrz 18
    N/A Done N/A Done
    20. Mrz 18
    Ravi
    9
    33
    D6
    $ 55.00
    63
    $ 3,465.00
    22. Mrz 18
    Done N/A Done N/A
    22. Mrz 18
    Ravi
    10
    Worksheet: Sheet1

    Here is the results in the master file for the area near that persons entry
    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    O
    25
    24
    D4
    $ 22.00
    41
    $ 902.00
    20. Mrz 18
    N/A Done N/A Done
    20.Mrz 18
    Ramesh
    26
    25
    D8
    $ 332.00
    32
    $ 10,624.00
    22. Mrz 18
    Done N/A Done N/A
    22.Mrz 18
    Ramesh
    27
    26
    A2
    $ 35.00
    8
    $ 280.00
    17. Mrz 18
    Done N/A Done N/A
    17.Mrz 18
    Ravi
    28
    27
    A6
    $ 78.00
    63
    $ 4,914.00
    17. Mrz 18
    Done N/A Done N/A
    17.Mrz 18
    Ravi
    29
    28
    B2
    $ 11.00
    47
    $ 517.00
    17. Mrz 18
    N/A Done N/A Done
    17.Mrz 18
    Ravi
    30
    29
    B6
    $ 96.00
    8
    $ 768.00
    18. Mrz 18
    Ravi
    31
    30
    C2
    $ 74.00
    63
    $ 4,662.00
    18. Mrz 18
    Ravi
    32
    31
    C6
    $ 365.00
    47
    $ 17,155.00
    20. Mrz 18
    Ravi
    33
    32
    D2
    $ 33.00
    8
    $ 264.00
    20. Mrz 18
    N/A Done N/A Done
    20.Mrz 18
    Ravi
    34
    33
    D6
    $ 55.00
    63
    $ 3,465.00
    22. Mrz 18
    Done N/A Done N/A
    22.Mrz 18
    Ravi
    35
    34
    A9
    $ 12.00
    65
    $ 780.00
    22. Mrz 18
    Sangeeta
    36
    35
    B9
    $ 45.00
    47
    $ 2,115.00
    22. Mrz 18
    Done N/A Done N/A
    21.Mrz 18
    Sangeeta
    Worksheet: Sheet1


    Here are the full results in the master workbook, "zMaster.xlsm", after running the code with your 5 data files.
    http://www.excelfox.com/forum/showth...0571#post10571

    The data files should be in the same Folder as the Master File. The code should go in the Master workbook
    MasterFileAnd5DataWorkbookFolder.JPG : https://imgur.com/za3oGLi
    MasterFileAnd5DataWorkbookFolder.JPG

    Notes:
    _
    The code uses the second worksheet column 1 temporarily
    _ The data worksheets can be open or closed when the code is run. They are not opened by the code. The data from them is taken whilst they are closed. ( But the code will also work if the data files are open )
    _ This code assumes that serial numbers are always continuous. If that was not always the case , then the code would need to be modified
    _________________________

    I am not 100% clear on excactly what should happen to the date in Cloumn L, and also I am not sure exactly what should be done to the date column in the data worksheets. If you clarify that and need more help I will take another look.

    Alan



    Ref:
    https://www.thespreadsheetguru.com/t...a-given-folder

    Last edited by DocAElstein; 03-23-2018 at 02:41 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Rep Power
    0
    Once Again Doc Elstein,

    Thank you very much for your input. I am sure this will save a lot of problems we are experiencing at our work.

    The date column L should have the date the macro was run on for the record's data that was copied.

    For Example in Raghu.xlsx, record with serial no.3 and 8 are not completed. When they are completed, the column L for these two records should have the date they were copied.

    In zMaster.xlsm, again the column L should have the date the records were copied.

    Hopefully, from Monday, we will have fewer problems thanks to your input and help.

    Regards
    Raghavendra

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837
    https://www.eileenslounge.com/viewtopic.php?f=21&t=40701&p=314836#p314836
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314621#p314621
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314619#p314619
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314600#p314600
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314599#p314599
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314274#p314274
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314229#p314229
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314195#p314195
    https://www.eileenslounge.com/viewtopic.php?f=36&t=39706&p=314110#p314110
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40597&p=314081#p314081
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40597&p=314078#p314078
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314062#p314062
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40597&p=314054#p314054
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313971#p313971
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313909#p313909
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40574&p=313879#p313879
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313859#p313859
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=313855#p313855
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 03-02-2024 at 02:59 PM.

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi Raghavendra,
    Quote Originally Posted by RaghavendraPrabhu View Post
    ...I am sure this will save a lot of problems we are experiencing at our work.
    we will have fewer problems thanks to your input and help.
    I think often automaton helps keep things in order and correct, which is an extra Bonus to the saving of time which it should also give
    _._____________________________________________---

    Quote Originally Posted by RaghavendraPrabhu View Post
    ..The date column L should have the date the macro was run on for the record's data that was copied.
    In zMaster.xlsm, again the column L should have the date the records were copied...
    I expected It was something like that.

    My last code attempt may not be the most efficient as , as far as I know, it is not possible to write information to a closed file, so it would be necessary to open the data files to add the data in column L in the data files anyway.

    But for now I will just modify the existing code.
    Towards the start of the Looping through the data workbooks the data files are now opened.
    The closed workbook references will still work even with the workbook open, so the same data is copied across.
    The updated date information is included now in the master Workbook and the data workbooks.

    So for example , I ran the latest code today, ( the 24th March ) , using the data files supplied in Post #1


    Master Worksheet:
    The master worksheet after running the code to merge data into it from the data worksheets, looks like this here:
    http://www.excelfox.com/forum/showth...0576#post10576

    Data worksheet example, ( For Workbook “Ravi.xlsx” , first worksheet)
    Here is the data worksheet Before the code runs

    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    1
    S No
    Item
    Price
    Qty
    Total
    Date Distributed
    Task1
    Task2
    Task3
    Task4
    Date Tasks Completed
    Date Consolidated
    Comments
    Team Member
    2
    26
    A2
    $ 35.00
    8
    $ 280.00
    17. Mrz 18
    Done N/A Done N/A
    17. Mrz 18
    Ravi
    3
    27
    A6
    $ 78.00
    63
    $ 4,914.00
    17. Mrz 18
    Done N/A Done N/A
    17. Mrz 18
    Ravi
    4
    28
    B2
    $ 11.00
    47
    $ 517.00
    17. Mrz 18
    N/A Done N/A Done
    17. Mrz 18
    Ravi
    5
    29
    B6
    $ 96.00
    8
    $ 768.00
    18. Mrz 18
    Need more information Ravi
    6
    30
    C2
    $ 74.00
    63
    $ 4,662.00
    18. Mrz 18
    Need more information Ravi
    7
    31
    C6
    $ 365.00
    47
    $ 17,155.00
    20. Mrz 18
    Need more information Ravi
    8
    32
    D2
    $ 33.00
    8
    $ 264.00
    20. Mrz 18
    N/A Done N/A Done
    20. Mrz 18
    Ravi
    9
    33
    D6
    $ 55.00
    63
    $ 3,465.00
    22. Mrz 18
    Done N/A Done N/A
    22. Mrz 18
    Ravi
    10
    Worksheet: Sheet1

    Here the same worksheet After the code has run

    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    1
    S No
    Item
    Price
    Qty
    Total
    Date Distributed
    Task1
    Task2
    Task3
    Task4
    Date Tasks Completed
    Date Consolidated
    Comments
    Team Member
    2
    26
    A2
    $ 35.00
    8
    $ 280.00
    17. Mrz 18
    Done N/A Done N/A
    17. Mrz 18
    24.Mrz 18
    Ravi
    3
    27
    A6
    $ 78.00
    63
    $ 4,914.00
    17. Mrz 18
    Done N/A Done N/A
    17. Mrz 18
    24.Mrz 18
    Ravi
    4
    28
    B2
    $ 11.00
    47
    $ 517.00
    17. Mrz 18
    N/A Done N/A Done
    17. Mrz 18
    24.Mrz 18
    Ravi
    5
    29
    B6
    $ 96.00
    8
    $ 768.00
    18. Mrz 18
    Need more information Ravi
    6
    30
    C2
    $ 74.00
    63
    $ 4,662.00
    18. Mrz 18
    Need more information Ravi
    7
    31
    C6
    $ 365.00
    47
    $ 17,155.00
    20. Mrz 18
    Need more information Ravi
    8
    32
    D2
    $ 33.00
    8
    $ 264.00
    20. Mrz 18
    N/A Done N/A Done
    20. Mrz 18
    24.Mrz 18
    Ravi
    9
    33
    D6
    $ 55.00
    63
    $ 3,465.00
    22. Mrz 18
    Done N/A Done N/A
    22. Mrz 18
    24.Mrz 18
    Ravi
    10
    Worksheet: Sheet1

    Here the code:
    http://www.excelfox.com/forum/showth...0577#post10577


    Alan
    Last edited by DocAElstein; 03-24-2018 at 09:42 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Rep Power
    0
    Doc Elstein

    Thank you this is what I wanted. But, it is not updating th xMaster the second time. Doing it for once.

    Input more new work for members and when clicked the first macro, it is working and updating the unique files.

    Once the unique files are completed, this macro is not updating the xMaster second time.

    Regards

    Raghavendra

    Regards

    Raghu

  6. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi Raghu, Hi Raghavendra
    Sorry I do not understand.

    Try to explain carefully what should happen.

    If possible show me a Before and then an After

    Before : what you have:
    After: You do this manually to show me what the code should do


    Take your time. I am busy today. From Tomorrow I can look again

    Alan
    Last edited by DocAElstein; 03-26-2018 at 02:12 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  7. #7
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Rep Power
    0
    Hi Doc Doc Elstein

    Please see the attached files.

    It is not working when the macro is run the second time.

    No hurry. I am away from work for a week due to Easter etc.

    Take your time to fix and thank you for your valuable time and knowledge input.

    Regards
    Raghavendra
    Attached Files Attached Files

  8. #8
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi Raghavendra
    I may be beginning to understand what you want.
    But some things are still not clear to me.

    I do not know which codes you are running when and to which data is being used when.

    You need to carefully explain a full run from start to finish of whatever it is that is being done.

    _1) First run of code ( which one?? )
    Which data is used.
    What are the befores and afters

    _2) What do you do now ? Run a different code? Which one
    Which data is used.
    What are the befores and afters

    _3) you now run a code a second time ( which one?? )
    Which data is used.
    What are the befores and afters

    _4) What do you do now ? Run a different code? Which one
    Which data is used.
    What are the befores and afters



    Please remember it is obvious to you what is being done and when and which codes you are using. You know the project well
    I am having to try and guess often to which codes you are referring and when.

    I do not know what is the typical data structure.
    I do not know what might be typical data structure in the future.
    I do not know how exactly new data is added.
    I do not know the typical sequence of events
    That is why I need to see befores and afters for all situations. Otherwise I must guess, and I will probably guess wrong..


    A clear explanation of exactly what is happening will also help anyone else to benefit from the Thread in the future.
    Every time you refer to a code, name it, so as to avoid confusion.
    Explain the exact sequence of events. Give example data for each stage showing exactly what you have before and fill in manually to show what you want the code to produce as the after.

    Keep the example data to the minimum. But choose it carefully so that it can be use do demonstrate and check all possible scenarios.

    I expect that when I finally know exactly what the requirement is, then I may need to start again from the beginning with a new code or codes.
    So please take the time to explain very carefully with examples, and then I will try to do this for you over the next week or so.

    Alan
    Last edited by DocAElstein; 03-27-2018 at 03:26 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  9. #9
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Rep Power
    0

    Talking

    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.

    Regards

    Raghu
    Attached Files Attached Files

  10. #10
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi Raghu,

    Thanks for the feedback and sharing your solution.
    You have clearly worked hard to solve your problem. Great , well done!

    I had a quick look through your files. It is still a bit difficult for me to understand what you are doing or wanting to do: I do not know your project so to understand would need a clear explanation from start to finish of what you are doing or wanting to do.
    I see lots of codes and files but have no idea which codes and files are used for which and when. I have to work very hard to try to guess what is going on. This will be obvious to you, but without a careful explanation from you it will be impossible for anyone like me to have any idea what is going on.

    But you appear to have a solution , so great.
    The codes may not be the most efficient, but I think those considerations are less and less relevant for an application like yours as computers get faster and have larger memories etc..
    It can be advantageous to have a long drawn out process, as this will be easier for you to follow and modify or de bug in the future. I personally prefer long drawn out codes for those reasons, but that is just my opinion, and I am just a hobby computer user and am not a professional programmer.

    I expect you may be doing a lot of unnecessary sorting of data, but I cannot be sure as I do not understand what and when things are to be done.

    Good luck with the project, and let us know if you need more help.
    But please in future try to explain more clearly in words exactly what is to be done , and when and which data is being used for which code .. etc.. etc… You have supplied some excellent detailed Test data and files. It is a shame more explanations to them were missing: Because an explanation was missing, I still could not use them effectively to give a better solution.
    You simply need to write in words the action that you take to produce the files, which codes are run and when etc… You need to understand that for someone that does not know the project details as you do, will need them to be explained carefully.

    For someone with little MS Excel VBA experience you have done very well to get your working solution. Well done again.


    Alan
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

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
  •