Results 1 to 10 of 60

Thread: Vba button Comparison data between two file or two tabs

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Hi Thai,

    Something like this requirement can be done in very many ways in VBA. I only know a few of the ways.

    I find it very difficult to understand and follow the logic of your coding.
    I think it is always difficult to understand other people’s code.
    So for now I will start from the beginning with a new code.

    It is not 100 % clear to me exactly what output you want. It appears that you want to show the new data in the second sheet, along with the original data… This seems strange to me?
    For testing purposes, I will give the output a little bit different to how your code does it, because that makes it a bit easier to test for now,

    I think you have potentially a very complicated situation. It might be more sensible to insist on certain rules for those adding data, in order to simplify the possible scenarios.


    However, I will make some attempt on a code

    I give you the code first.
    Check if it does what you want.

    Then if you need modifications or want me to explain anything , then let me know

    I assume a customer will add an entire row or change data . This makes the logic very tricky to get right. Not all possible scenarios will be covered with my code: My code may not work in all possible scenarios


    My general code logic:
    Both data ranges from workSheet1 and workSheet2 are captured and put into an Array, arrSht1() and arrSht2().
    I make a new array, arrSht1b() , which is intended to have possibly extra rows at the place where new rows appear to have been added in workSheet2. So it is given the size of arrSht2()**
    Initially the values from arrSht1() are placed in arrSht1b(). ( But as arrSht1b() has the size of arrSht2() , then initially there may be some empty elements at the end, ( end = last rows of data )
    I also create an array, arrout() , to be used to hold the required output data.
    **Both arrays, arrSht1b() and arrout() , can be given their types ( String ), and final size as that of arrSht2()


    I loop through the data in sheet1 ( or rather array, arrSht1b() ) and compare with sheet2 ( or rather arrSht2() ).
    ( arrSht1b() effectively monitors, ( is adjusted to ) , become equal to the new data as the looping continues) .

    A Condition check is made.
    __If either cell in a row from the sheet 1 data does not match the new data, AND the next row from sheet1 data does, then I assume a row had data changed, but a row was not inserted.
    (arrSht2(Cnt, 1) <> arrSht1b(Cnt, 1) Or arrSht2(Cnt, 2) <> arrSht1b(Cnt, 2)) And (arrSht2(Cnt + 1, 1) = arrSht1b(Cnt + 1, 1) And arrSht2(Cnt + 1, 2) = arrSht1b(Cnt + 1, 2))

    For this condition satisfied, the monitoring array , arrSht1b() , is adjusted to the new values and the cell differences checked and counted and the relevant info given to the output array, arrOut().


    A second Condition check is made, ( which will only be made else if, ( ElseIf ) , the first was not satisfied)
    __ElseIf either cell in a row from the sheet 1 data does not match the new data, AND the next row in sheet2 data does not match the next data from sheet 1, then this is one possibility** that a row has been inserted/ added
    ((arrSht2(Cnt, 1) <> arrSht1b(Cnt, 1) Or arrSht2(Cnt, 2) <> arrSht1b(Cnt, 2)) And (arrSht2(Cnt + 1, 1) <> arrSht1b(Cnt + 1, 1) Or arrSht2(Cnt + 1, 2) <> arrSht1b(Cnt + 1, 2)))

    I then add that data row then to monitoring array, arrSht1b(), after all values above are shifted one place. This modified array is then further used to compare further with array, arrSht2()
    At this check , the value at the row in the arrSht2() is added to the Output array, arrOut()

    Eventually arrSht1b() will be identical to arrSht2(). But, the important point, in the process of looping it is built, and at each loop is used to check if the current row matches the row in the second sheet data.

    _.___________________



    This is almost certainly not the most professional or efficient code, and it will not work in all possible scenarios** but may be easier to understand as an initial attempt.
    **I have not tested it extensively in all possible situations.: I leave this to you to test thoroughly and tell me if it is working OK for you, ( free excel help forum is for help and to help learn but not to do work for you )


    I have not included all your original cell formatting. If you need help with this then ask

    For testing purposes, I give the output in a third worksheet, Tabelle3

    I will gladly explain the code in more detail if you ask me to.

    _._____________

    Code is here:
    http://www.excelfox.com/forum/showth...0736#post10736
    ( Run code Sub TestieCalls() )

    ( code also in uploaded files:
    Compare2WorkSheets Testings2.xlsm
    Compare2WorkSheets Testings1.xlsm
    )

    _.______________________________________

    Example testing
    For testing purposes, I give the output in a third worksheet, Tabelle3

    Test 2 ( “Compare2WorkSheets Testings2.xlsm” )
    Sheet1 Sheet1 Test Output Test Output Sheet2 Sheet2
    Customer Assembly Customer Assembly
    Nu Torque
    13456
    Nu Torque
    13456
    Blu Origin Spaceship Blu Origin Spaceship
    Jet Blue12 ABC456 _____ <> Jet Blue12 _______ <> ABC456
    Alaska
    789
    Jet Blue12 ABC456
    Toyota Supra Alaska
    789
    Emirate ABC12345 Toyota Supra
    Emirate ABC12345
    Spaceship on last row is new 12 on last row is new Spaceship
    12
    Worksheet: Tabelle3
    Testing2.JPG https://imgur.com/ISbeHaJ
    Attachment 2081


    Test 1 ( “Compare2WorkSheets Testings1.xlsm” )
    Sheet1 Sheet1 Test Output Test Output Sheet2 Sheet2
    Customer Assembly Customer Assembly
    Nu Torque
    13456
    Nu Torque
    13456
    Blu Origin Spaceship Blu Origin Spaceship
    Jet Blue21 ABC Jet Blue12 <> Jet Blue21 ABC12345 <> ABC Jet Blue12 ABC12345
    Alaska
    789
    Alaska
    789
    Toyota Supra Toyota Supra
    Emirate ABC12345 Emirate ABC12345
    Spaceship on last row is new 12 on last row is new Spaceship
    12
    Worksheet: Tabelle3
    Testing1.JPG : https://imgur.com/eCjAGOL
    Testing1.JPG

    _.__________________________-

    The above results seem OK....
    **But remember: This code does not work in all scenarios. We may need to modify and improve it. You must check all possible scenarios and report back.

    Alan
    Attached Files Attached Files
    Last edited by DocAElstein; 07-08-2018 at 02:20 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!!

Similar Threads

  1. Link spin button or scroll button to chart.
    By Thainguyen in forum Excel Help
    Replies: 1
    Last Post: 06-08-2018, 04:19 AM
  2. VBA Macro to open a file and extract data
    By jeremiah_j2k in forum Excel Help
    Replies: 0
    Last Post: 05-22-2017, 03:17 PM
  3. Button to export data to a master file
    By aryanaveen in forum Excel Help
    Replies: 0
    Last Post: 01-17-2015, 02:35 AM
  4. Replies: 7
    Last Post: 05-20-2014, 02:10 AM
  5. Replies: 3
    Last Post: 08-28-2013, 02:02 PM

Tags for this Thread

Posting Permissions

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