+ Reply to Thread
Results 1 to 4 of 4

Data Validation - List of invalid data needed

Hybrid View

  1. #1
    Registered User
    Join Date
    07-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    8

    Data Validation - List of invalid data needed

    Hi there,

    I have 12 massive sheets that I import into a access db once a month, the sheets have data validation on certain columns, but with 20000+ lines on each sheet and 40 odd columns of data looking for red circles has become a rather tiresome pastime... usually there are only 10 or so errors, but without knowing the number of errors or where they are its a slow dull process...

    Is there anyone out there who could help me create a macro that would create a new sheet within the workbook that will list the cell references of the invalid data? or any other ideas to help would be great! I'm running xl 2007 and fairly familiar with recording and editing the odd macro.

    TIA

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Data Validation - List of invalid data needed

    As far as I know the only solution available to you is to loop through the 12 worksheets, an per worksheet set a range object to include all cells that have a validation rule and then check for each cell if the validation rule is violated and if so output the details to a new worksheet. I have not come across an exposed validation object where you can easily get the error count and cells in error as properties.
    If you like my contribution click the star icon!

  3. #3
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Data Validation - List of invalid data needed

    Something like this may do the trick

    Public Sub validateWorksheetData()
    On Error Resume Next
    
    '#
    '# declare private variables
    '#
       Dim pvt_obj_Worksheet As Excel.Worksheet
       Dim pvt_obj_ValidationRange As Excel.Range
       Dim pvt_obj_ValidationCell As Excel.Range
       Dim pvt_obj_ValidationLog As Excel.Worksheet
       Dim pvt_lng_ValidationLog As Long
       
    '#
    '# initialise
    '#
       Set pvt_obj_ValidationLog = ThisWorkbook.Worksheets("ValidationLog")
       If pvt_obj_ValidationLog Is Nothing Then
          ThisWorkbook.Worksheets.Add.Name = "ValidationLog"
          Set pvt_obj_ValidationLog = ThisWorkbook.Worksheets("ValidationLog")
       End If
       
    '#
    '# clear the contents of the validation log worksheet
    '#
       pvt_lng_ValidationLog = 1
       With pvt_obj_ValidationLog
          .Cells.ClearContents
          .Cells(1, "A").Value = "Worksheet"
          .Cells(1, "B").Value = "Address"
          .Cells(1, "C").Value = "Input message"
          .Columns.AutoFit
       End With
       
    '#
    '# loop for all worksheets in the current workbook, skip the validation log
    '# itself and any other worksheet not relevant
    '#
       For Each pvt_obj_Worksheet In ThisWorkbook.Worksheets
          If pvt_obj_Worksheet.Name <> "ValidationLog" Then
          
          '#
          '# initialise a range object to contain all cells on the current worksheet
          '# that have a validation rule assigned - if the object does not hold any
          '# cells, skip the worksheet
          '#
             Set pvt_obj_ValidationRange = Nothing
             Set pvt_obj_ValidationRange = pvt_obj_Worksheet.Cells.SpecialCells(xlCellTypeAllValidation)
             
             If Not pvt_obj_ValidationRange Is Nothing Then
             
             '#
             '# check all cells for a validation violation and when found record the details on the
             '# validation log worksheet
             '#
                For Each pvt_obj_ValidationCell In pvt_obj_ValidationRange.Cells
                   If Not pvt_obj_ValidationCell.Validation.Value Then
                      pvt_lng_ValidationLog = pvt_lng_ValidationLog + 1
                      pvt_obj_ValidationLog.Cells(pvt_lng_ValidationLog, "A").Value = pvt_obj_Worksheet.Name
                      pvt_obj_ValidationLog.Cells(pvt_lng_ValidationLog, "B").Value = pvt_obj_ValidationCell.Address
                      pvt_obj_ValidationLog.Cells(pvt_lng_ValidationLog, "C").Value = pvt_obj_ValidationCell.InputMessage
                   End If
                Next pvt_obj_ValidationCell
             
             End If
             
          End If
       Next pvt_obj_Worksheet
    
    End Sub

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Data Validation - List of invalid data needed

    You can try this:
    Sub test2()
        On Error Resume Next
        Dim hasError As Boolean, ws, ws2 As Worksheet, c As Range
        hasError = False
        For Each ws In Worksheets
            For Each c In ws.Cells.SpecialCells(xlCellTypeAllValidation)
                If c <> "" And Not c.Validation.Value Then
                    If Not hasError Then
                        hasError = True
                        Set ws2 = Worksheets.Add
                        ws2.Range("A1") = "Errors"
                        ws2.Name = "Validation Errors"
                    End If
                    ws2.Hyperlinks.Add ws2.Range("A" & Rows.Count).End(xlUp).Offset(1), "", ws.Name & "!" & c.Address, , ws.Name & "!" & c.Address
                End If
            Next
        Next
        ws2.Columns(1).AutoFit
    End Sub
    Might want to try with a smaller set of data first though.

+ 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. Replies: 0
    Last Post: 03-10-2013, 11:17 PM
  2. Macro that detects and circles invalid data automatically (Data Validation)
    By dchubbock in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-02-2012, 09:07 AM
  3. Data Validation would like to highlight background when invalid data is present
    By moto485 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2011, 09:20 PM
  4. Removing multiple invalid characters for dependent data validation list
    By cuclay in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-15-2009, 01:34 AM
  5. Data Validation: Printing Invalid Data Circles
    By Andrew in forum Excel General
    Replies: 1
    Last Post: 05-04-2006, 03:15 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