Using the Excel.Validation Variable

medberg

New Member
Joined
Jul 29, 2011
Messages
30
Hello,

I have a cell that I want to get the Validation information for. Basicailly, I declare a variable like this:
Code:
Dim validation As Excel.validation
Now, I want to set validation equal to something. I was thinking something along the lines of this:
Code:
validation = Sheet1.Cells(row, column).validation
This does not work at all. Does anyone know the correct syntax to get this working? Thanks!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The macro recorder can be really helpful for cases like this where you know how to do something manually, but you don't know the VBA syntax.

Start by recording a macro like this...
Rich (BB code):
Sub Macro2()
'
' Macro2 Macro
'

'    
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:="1", Formula2:="10"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = "Guess"
        .ErrorTitle = "Error"
        .InputMessage = "Pick a number between 1 and 10"
        .ErrorMessage = "Your number is not between 1 and 10"
        .ShowInput = True
        .ShowError = True
    End With
End Sub

You can them make the code more flexible for your purpose....

Rich (BB code):
Sub Add_Validation()
    Dim lMax As Long, lMin As Long
    lMin = 10
    lMax = 100
    With Sheets("Sheet1").Range("A1:E1").Validation
        .Delete
        .Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:=lMin, Formula2:=lMax
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = "Guess"
        .ErrorTitle = "Error"
        .InputMessage = "Pick a number between " & lMin & " and " & lMax
        .ErrorMessage = "Your number is not between " & lMin & " and " & lMax
        .ShowInput = True
        .ShowError = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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