Need a VBA code to Validate columns in spreadsheet

levanoj

Active Member
Joined
Oct 25, 2007
Messages
311
I need a code that validates my spreadsheet by checking the following:
  • All cell values under the 'Button Number' header (column A) must be a numeric value within range of 1-600
  • All cell values under the 'Button Type' header (column B) can only be one of the following values: Speedial, ResourceAndSpeedDial, Resource, HuntAndSpeedDial, ICM, InvalidButtonType and must also be case sensitive
  • All cell values under the 'Button Label' header (column C) can contain any alphanumeric value except when the cell value in column B of the same row is 'InvalidButtonType', in which case there should be no value present
  • All cell values under the 'Button Lock' header (column D) can only be one of the following values: true, false and must also be case sensitive
  • All cell values under the 'SpeedDialType' header (column E) can only be one of the following values: none, home, office, mobile and must also be case sensitive, except when the cell value in column B of the same row is 'InvalidButtonType', in which case there should be no value present
  • All cell values under the 'Incoming Action Rings' header (column F) can only be one of the following values: none, repeat, single and must also be case sensitive, except when the cell value in column B of the same row is 'InvalidButtonType', in which case there should be no value present
  • All cell values under the 'Incoming Action Priority' header (column G) can only be one of the following values: high, low and must also be case sensitive, except when the cell value in column B of the same row is 'InvalidButtonType', in which case there should be no value present
  • All cell values under the 'Incoming Action Float' header (column H) can only be one of the following values: Float, NoFloat and must also be case sensitive, except when the cell value in column B of the same row is 'InvalidButtonType', in which case there should be no value present
  • All cell values under the 'Display Incoming CLI' header (column I) can only be one of the following values: CLI, noCLI and must also be case sensitive, except when the cell value in column B of the same row is 'InvalidButtonType', in which case there should be no value present
Once the code is executed it should produce and X under the 'Error' header (column J) if any of the above listed rules are not met within that row. It would also be ideal if the code highlighted the cell where the rule was not met to be able to easily locate the cell and rectify issue. If no errors exist then the result would be a Pop-Up Notification of "Validation Successful"

I know much of this can be done via Conditional Formatting but because I receive many spreadsheets from different sources then being able to run a macro would be much more convenient.
The following is an example of how this would result:

Book2
ABCDEFGHIJ
1Button NumberButton TypeButton LabelButton LockSpeedDialTypeIncoming Action RingsIncoming Action PriorityIncoming Action FloatDisplay Incoming CLIError
21MWITest1falsenonerepeathighFloatnoCLI
32ResourceAndSpeedDialTest2falsenonenonehighFloatnoCLI
43ResourceTest3falsenonerepeathighNoFloatnoCLI
54HuntAndSpeedDialTest4falsehomesinglehighNoFloatnoCLI
65ResourceTest5truemobilesinglehighNoFloatnoCLI
76InvalidButtonTypeTest6falseX
87ResourceTest7falsenonerepeatlowNoFloatnoCLI
98ICMTest8falsenonerepeatlowNoFloatCLI
109ResourceTest9falseofficerepeatlowNoFloatCLI
1110ResourceTest10FALSEofficerepeatlowFloatCLIX
1211ResourceTest11falsenonerepeatlowFloatCLI
1312ResourceTest12falsenonerepeatlowFloatCLI
1413ResourceTest13falsenonerepeatlowfloatCLIX
1514ResourceTest14falsenonerepeatlowNoFloatCLI
1615ResourceTest15falsenonerepeatlowNoFloatCLI
1716ResourceTest16falsenonerepeatlowNoFloatCLI
1817ResourceTest17falsenonerepeatlowNoFloatCLI
Sheet1
Cells with Data Validation
CellAllowCriteria
D2:D10Listtrue,false
D12:D18Listtrue,false
I2:I18ListnoCLI,CLI
H2:H13ListFloat,NoFloat
H15:H18ListFloat,NoFloat
E2:E18Listnone,home,office,mobile
F2:F18Listnone,single,repeat
G2:G18Listlow,high
A2:A18List,null,null,null,null
B1:B18ListInvalidButtonType,Resource,ResourceAndSpeedDial,HuntAndSpeedDial,Speedial,ICM
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
This is relatively easy to implement in VBA. if you have any experience with VBA then i can get you started and you can tinker and see how you go. one thought i have is that you are flagging errors in some cases which it is clear what the data is supposed to be (eg FALSE:false) instead of flagging them it would be much less work for the user to just have them corrected.
 
Upvote 0
I've dabbled a bit with VBA. I was hoping to get a preliminary code if possible and then I'd tweak it as necessary.

Regarding your other point, I'd actually prefer to see the error first rather than automatically having it corrected since I may have to verify directly with the source who provided the file before I can make that decision to correct the error.
It's really a work protocol I have to adhere to before editing these particular files. Hope that makes sense.
 
Upvote 0
sure... well here is a basic starting point and i can add to it or you can have a go :)

VBA Code:
Sub Validate()
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Row = 2 To lastrow
        ' first column
        If Val(Cells(Row, 1)) < 1 Or Val(Cells(Row, 1)) > 600 Then
            Cells(Row, 1).BackColor = &H80FF&
            Cells(Row, 10) = "X"
        End If
        ' second column
        If Cells(Row, 2) <> "Speedial" And Cells(Row, 2) <> "ResourceAndSpeedDial" And Cells(Row, 2) <> "Resource" _
                    And Cells(Row, 2) <> "HuntAndSpeedDial" And Cells(Row, 2) <> "ICM" And Cells(Row, 2) <> "InvalidButtonType" Then
            Cells(Row, 2).BackColor = &H80FF&
            Cells(Row, 10) = "X"
        End If
        ' third column
        If Val(Cells(Row, 2)) = "InvalidButtonType" And Val(Cells(Row, 3)) <> "" Then
            Cells(Row, 3).BackColor = &H80FF&
            Cells(Row, 10) = "X"
        End If
        ' try some yourself
        
    Next Row
End Sub
 
Upvote 0
This was the result when running it on the example data in my original post:
Error.JPG
Error2.JPG

Any suggestions?
 
Upvote 0
VBA Code:
Cells(row,2).Interior.ColorIndex = 46

sorry about that. thinking of a textbox background color
 
Upvote 0
Solution
So the highlight seems to work but got a different error this time
 

Attachments

  • new error.jpg
    new error.jpg
    41.6 KB · Views: 5
Upvote 0
VBA Code:
        If Cells(Row, 2) = "InvalidButtonType" And Cells(Row, 3) <> "" Then

that will teach me for copy pasting and not checking. so do you see what the If blocks are doing?
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top