+ Reply to Thread
Results 1 to 12 of 12

VBA Code to Extract complete row if the criteria did not match with 4 columns

  1. #1
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    900

    Question VBA Code to Extract complete row if the criteria did not match with 4 columns

    Hello again,

    I have tring to fect a solution on the net from few weeks but fail to get it. That is why i am posting my problem here in hope someone will help me.

    I am always receiving a big list which is containing incorrect information usually. So, I want a help from this site experts to create a VBA Macros which will be named as ERROR LOG FILE. The script should generate a new workbook for the entire error records and which will highlight cell for wrong entry for a particular record.

    In column B only M or F letters are allowed. If more than 1 letter i.e. Space, Special character or any value which is not match to these 2 letters i.e. M or F then copy complete row to a new workbook with remarks will be added in column G & should say “Only 1 letter (M or F) is allowed as gender”.

    In column C only VIP, A, B & C is allowed. If any character which is not matching to these 4 items then copy complete row to a new workbook with remarks will be added in column G & should say “Only VIP, A, B & C is allowed in class”.

    In column D only 3 numeric values i.e. 0 to 999 is allowed but no spaces. If more than 3 numeric value then copy complete row to a new workbook with remarks will be added in column G & should say “Only 3 characters or numeric values are allowed in Age”.

    In column E only 3 words is allowed i.e. Employee, Spouse & Child. If any value in this column which doesn’t match with these 3 words then copy complete row to a new workbook with remarks will be added in column G & should say “Employee, Spouse or child is allowed in this column.

    In column F only 5 words is allowed i.e. African, Asian, Middle East, Saudi & Other. If any value in column F which doesn’t match with these 5 words then copy complete row to a new workbook with remarks will be added in column G & should say “African, Asian, Middle East, Saudi & Other is allowed in this column.

    Now the conditions. If the above mentioned 5 points have any error in any of the columns then it will copy complete row of error record to a newly generated workbook with remarks as mentioned above. Spaces, special character, spaces are not allowed i.e. only exactly match values are allowed otherwise the row will assume as error in record and copies to error log file which will be generated by your script.


    Copy of original list, sample of accepting output is attached for your refference.
    Attached Files Attached Files
    Last edited by hecgroups; 07-19-2012 at 11:26 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Extract complete row if the criteria did not match with 4 columns

    THis isn't that hard to put into a simple formula, then you can filter your data right there on the original sheet, no macro, no fuss.

    On your main sheet, put this formula into G2, then copy down far enough to cover the dataset:

    =IF(AND(B2<>"M",B2<>"F"), "Only 1 letter (M or F) is allowed as gender",
    IF(NOT(OR(C2="VIP",C2="A",C2="B",C2="C")), "Only VIP, A, B & C is allowed in class",
    IF(NOT(AND(ISNUMBER(D2), D2>=0, D2<=999)), "Only 3 characters or numeric values are allowed in Age",
    IF(NOT(ISNUMBER(SEARCH(E2, "EMPLOYEESPOUSECHILD"))), "Employee, Spouse or child is allowed in this column",
    IF(NOT(ISNUMBER(SEARCH(F2, "African, Asian, Middle East, Saudi, Other"))), "African, Asian, Middle East, Saudi & Other is allowed in this column", "")))))



    Now apply a filter to column G to show all non-blanks and voila! Make your corrections, turn off the filter and copy data to permanent file. Just reuse this sheet, drop your new data in and bingo.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    900

    Re: Extract complete row if the criteria did not match with 4 columns

    First of all i would like to thank you for your response.

    Yes you are right but if it be in VBA Macros then it is save my few hours. Because i am receiving about 8 to 10 files with around 25 to 43 thousand records each day & most of them is having difference number of records.

    Moreover i need to highlight the cell with yellow if the value is not correct. After chatching the errors i have to notify to sales to grabe correct information. If you can help me to write vba macros i will be very thankful to you for this help.

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract complete row if the criteria did not match with 4 columns

    I am always receiving a big list which is containing incorrect information usually.
    You might be able to better the sheet issued for the "big list" with data validation.

    See this workbook try filling with something incorrect.

    No fuss no errors no time wasted ...
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  5. #5
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    900

    Re: Extract complete row if the criteria did not match with 4 columns

    Thanks for your solution but i am looking for VBA Macros which will generate new workbook with error records

  6. #6
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    900

    Re: VBA Code to Extract complete row if the criteria did not match with 4 columns

    Can someone please

  7. #7
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    900

    Re: VBA Code to Extract complete row if the criteria did not match with 4 columns

    Hello Mr. JBeaucaire,

    Sorry for delay in replying. The formula below mentioned need correction.

    Please Login or Register  to view this content.
    Please check the new attachment below with remarks accepting.
    Attached Files Attached Files

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Code to Extract complete row if the criteria did not match with 4 columns

    OK, you don't want to test those things sequentially, you want to test them ALL every time and display ALL errors.

    Format column G to Text Wrap, then put this in G2, copy down:

    [B=IF(OR(B2<>"M",B2<>"F"), "", "Only 1 letter (M or F) is allowed as gender" & CHAR(10)) &
    IF(OR(C2="VIP",C2="A",C2="B",C2="C"), "", "Only VIP, A, B & C is allowed in class" & CHAR(10)) &
    IF(AND(ISNUMBER(D2), D2>=0, D2<=999), "", "Only 1-3 character numeric values are allowed in Age" & CHAR(10)) &
    IF(ISNUMBER(SEARCH(E2, "EMPLOYEESPOUSECHILD")), "", "Employee, Spouse or child is allowed in this column" & CHAR(10)) &
    IF(AND(LEN(TRIM(F2))>0, ISNUMBER(SEARCH(F2, "African, Asian, Middle East, Saudi, Other"))), "", "African, Asian, Middle East, Saudi & Other is allowed in this column")[/B]
    Last edited by JBeaucaire; 08-10-2012 at 04:17 PM. Reason: corrected final part of formula

  9. #9
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    900

    Re: VBA Code to Extract complete row if the criteria did not match with 4 columns

    Thanks for your response.


    Now the issue is formating in displaying the errors means no comma after message as mentioned.

    Appearing: Only 1-3 character numeric values are allowed in AgeEmployee Spouse or Child is allowed in this column

    Actual Result:Only 1-3 character numeric values are allowed in Age, Employee Spouse or Child is allowed in this column



    If the Column E and F are empty the formula not displaying the error message which should be as mentioned below.

    Employee Spouse or Child is allowed in this column OR African, Asian, Middle East, Saudi & Other is allowed in this column.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Code to Extract complete row if the criteria did not match with 4 columns

    No, follow my instruction and turn on the WRAP taxt function for those cells. Or replace the & CHAR(10) & with & "," &

  11. #11
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: VBA Code to Extract complete row if the criteria did not match with 4 columns

    hi hecgroups, please check attachment, option to make it with VB, press Run button
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor
    Join Date
    01-11-2012
    Location
    Riyadh, K.S.A.
    MS-Off Ver
    Windows 11 with Excel 2013 & 2016
    Posts
    900

    Re: VBA Code to Extract complete row if the criteria did not match with 4 columns

    Thanks watersev. It works fine as i want thank you for your valuable script. I will get back to you if i have any issue. Topic marked as closed and reputation added.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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