+ Reply to Thread
Results 1 to 19 of 19

Macro to do a data validation check before exporting to text file

  1. #1
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Macro to do a data validation check before exporting to text file

    Hi

    I have an excel macro enable spreadsheet, that exports the contents into a text file. It has three columns in the xls that is used to capture amounts with two decimal places and these are formatted as text fields. When the macro runs it left pads the amount columns with zeroes to get a 16 width result in the exported text file and removes the decimals as target system implies the decimals. If decimals are left out and a whole number is entered it screws the result up

    Because of this, I can't add normal data validation within the xls to ensure that the amount fields are entered with two decimals and because I need the fields as text I can't use formatting to force the result. Hence I want to build into the macro, a step at the beginning that select the ranges as the three columns I am interested in from rows 2 to 1000 and checks for any cells with no decimal followed by 2 numbers, if found, it should highlight the cell as well as pop up a MsgBox with an error message and end the rest of the macro from running (i.e. abend) until the user fixes all the cells and re-runs

    In another forum post a guy did a script that checked for specific values and highlighted cells where the condition was not met. just need mine to be slightly different as it is looking for a decimal + 2 number pattern in the cell and I need the cells not just highlighted but the msgbox and error and rest of macro stops / exits at that point

    the other forum post was this one

    https://www.mrexcel.com/forum/excel-...alidation.html

    thanks for the help guys

  2. #2
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    Try this:
    I assumed your range is Range("A2:C2000") & the decimal separator is "."
    Important note:
    'Even the range in question start at row 2 this part must set the range start at row 1
    Set r = Range("A1:C2000") '


    Please Login or Register  to view this content.
    Last edited by Akuini; 02-17-2019 at 10:24 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    I think there’s a flaw in the code above, in the IF THEN part, so use this one instead:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    Hi Akuini

    Thanks very much for this

    I tried running macro and it gave a run time error 2004 at the point below (row below was highlighted yellow)

    r.Interior.Color = xlNone

  5. #5
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    Sorry runtime error = 1004 not 2004

  6. #6
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    Quote Originally Posted by troygeri View Post
    Hi Akuini

    Thanks very much for this

    I tried running macro and it gave a run time error 2004 at the point below (row below was highlighted yellow)

    r.Interior.Color = xlNone
    Maybe it has something to do with your Excel version Excel 2003 ?
    That line is supposed to clear the cell color before it get highlighted.
    Just delete that line, then run the code again, see what happen.

  7. #7
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    Could it be because there is more than one worksheet in the spreadsheet and worksheet name needs to be specified when defining the range?

  8. #8
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    Using Excel 2016.. but ill try it..

  9. #9
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    Quote Originally Posted by troygeri View Post
    Could it be because there is more than one worksheet in the spreadsheet and worksheet name needs to be specified when defining the range?
    I don't think so. The code work only in activesheet.
    Let's try something simple first. Run this code in a new blank sheet. See if it gets error.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    OK that did fix it... thankjs

    Only remaining issue is I have is, user presses button that runs another macro (call it parent macro), parent executes this qualitycheck macro first, and then calls and runs remaining macros. If running your qualitycheck macro detects a problem, I want it to stop the remaining macros from running, pressing OK on the Msg Box should result in user being sent back into the worksheet where they will see the highlighted cell with the error

  11. #11
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    One other point. Highlighted cell is grey, no matter what color is specified in the macro. Secondly, it only seems to highlight the first cell it encounters that breaches the condition. Running you above color sub on a blank sheet worked as expected without an error, column A was highlighted blue and the others had no highlighting

  12. #12
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    Quote Originally Posted by troygeri View Post
    OK that did fix it... thankjs

    Only remaining issue is I have is, user presses button that runs another macro (call it parent macro), parent executes this qualitycheck macro first, and then calls and runs remaining macros. If running your qualitycheck macro detects a problem, I want it to stop the remaining macros from running, pressing OK on the Msg Box should result in user being sent back into the worksheet where they will see the highlighted cell with the error
    You mean it’s fixed by deleting the line?
    Well, you need to find a way to clear the range before running the macro, otherwise if you run it for the second round then the highlighted cell (from the first run) is still there so it will distort the result.
    Try changing the line with this:
    Please Login or Register  to view this content.
    I don’t quite understand your macro set up, but basically you need put my macro inside your macro (that runs by hitting a button) & you need to put it above the existing lines of your macro.

  13. #13
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    OK vbWhite seems to work ok.. no error.

    Ignore my comment about highlighting not applied to all cells.. it does highlight all cells in error

    For the parent macro. it starts off something like this

    Sub xls_txt_convert()

    Dim DestinationFile, CellValue, Filler_Char_To_Replace_Blanks As String
    Dim FileNum, ColumnCount, RowCount, FieldWidth As Integer
    Dim sht As Worksheet

    Call QualityCheck
    Call zeroPad
    Call RowSelect

    So what I want is if QualityCheck finds cells with error, then it should exit the parent macro and not keep running after pressing OK on the msg box that your macro pops up

  14. #14
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    Ok, it should be something like this:

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    Almost there!! Tried your suggestion above. It stops at Call QualityCheck(deflag), with (deflag) highlighted and Compile Error (ByRef Argument Type Mismatch)

  16. #16
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    The above code works for me, so I don't understand why it doesn't work for you.
    But try this this one:

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    Thanks. Adding deFlag As Boolean to the DIM statement resolved it. The only remaining issue I have is that the cells without the decimals still don't get highlighted. I've tried different ways of writing the Interior.Color statement to no avail

  18. #18
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Macro to do a data validation check before exporting to text file

    Quote Originally Posted by troygeri View Post
    Thanks. Adding deFlag As Boolean to the DIM statement resolved it. The only remaining issue I have is that the cells without the decimals still don't get highlighted.
    I forgot to change the decimal separator back to ".", so replace "," with "." in red part:
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    03-22-2010
    Location
    Ballarat, Australia
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Macro to do a data validation check before exporting to text file

    yeh I already had that

    Please Login or Register  to view this content.
    The check works as if whole numbers are in the cells it pops up the MsgBox but if all numbers are with two decomal places all the other macros run as intended. So, basically, the check is working, just not highlighting the cells

+ 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. [SOLVED] Data Validation Check Macro
    By Cortlyn in forum Excel General
    Replies: 1
    Last Post: 04-04-2018, 09:22 AM
  2. Exporting Excel Data to Text file.
    By manjunath16 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-29-2016, 12:14 AM
  3. Excel 2007 : Exporting data to text file
    By Sebas123 in forum Excel General
    Replies: 1
    Last Post: 08-10-2011, 05:19 AM
  4. Generating muiltple sheets from cell data and exporting as a text file
    By troth in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2011, 01:25 PM
  5. [SOLVED] Exporting data in comma delimited format in text file
    By Robert M. Lincoln in forum Excel General
    Replies: 1
    Last Post: 09-30-2005, 08:05 AM
  6. Exporting data to text file in excel
    By Excel user via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 02:05 PM
  7. Exporting block of data to a Text File
    By mluetkem in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2005, 04:20 PM

Tags for this Thread

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