Hi there!
I am having some problems with data validation through VBA.
The "Age" column should have several data validations, depending on which method was selected by the user
should look something like this (refer to image)
here is my code
With this code I have a run-time error "Type mismatch" on if else statements.
It would be also great, if you could also help me with the "Format" of the "Cell" depending on which data validation is. (if "date of birth" then cell format smth like "dd/mm/yyyy")
I am also very new to VBA, so if anyone has any suggestions, if you could show the entire code (from SUB to END SUB) instead of just the code lines, as I am still learning what goes where.
Thank you!
I am having some problems with data validation through VBA.
The "Age" column should have several data validations, depending on which method was selected by the user
should look something like this (refer to image)
here is my code
VBA Code:
Sub DateOfBirth()
Dim FORM As Worksheet
Dim COLUMN_RANGE As Range
Set FORM = Worksheets("FORM")
Set COLUMN_RANGE = FORM.ListObjects("Database").ListColumns("Age").DataBodyRange
With COLUMN_RANGE.Validation
.Delete
.Add Type:=xlValidateDate, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="01-01-1900", Formula2:="=Now()"
.IgnoreBlank = True
.InCellDropdown = False
End With
End Sub
Sub YearsOld()
Dim FORM As Worksheet
Dim COLUMN_RANGE As Range
Set FORM = Worksheets("FORM")
Set COLUMN_RANGE = FORM.ListObjects("Database").ListColumns("Age").DataBodyRange
With COLUMN_RANGE.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="1", Formula2:="110"
.IgnoreBlank = True
.InCellDropdown = False
End With
End Sub
Sub AgeCategory()
Dim FORM As Worksheet
Dim COLUMN_RANGE As Range
Set FORM = Worksheets("FORM")
Set COLUMN_RANGE = FORM.ListObjects("Database").ListColumns("Age").DataBodyRange
With COLUMN_RANGE.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=INDIRECT(""AgeCategoryList[Age Category]"")"
.IgnoreBlank = True
.InCellDropdown = True
End With
End Sub
Sub AgeMethodDropDownList()
Dim FORM As Worksheet
Dim COLUMN_RANGE As Range
Set FORM = Worksheets("FORM")
Set COLUMN_RANGE = FORM.ListObjects("Database").ListColumns("Age Method").DataBodyRange
With COLUMN_RANGE.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Formula1:="=INDIRECT(""AgeMethodList[Age Method]"")"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = "Age Method"
.InputMessage = "Pick Age Method from List"
.ShowInput = True
End With
If COLUMN_RANGE.Value = "Date of Birth" Then
Call DateOfBirth
ElseIf COLUMN_RANGE.Value = "Years Old" Then
Call YearsOld
ElseIf COLUMN_RANGE.Value = "Age Category" Then
Call AgeCategory
Else
End If
End Sub
With this code I have a run-time error "Type mismatch" on if else statements.
It would be also great, if you could also help me with the "Format" of the "Cell" depending on which data validation is. (if "date of birth" then cell format smth like "dd/mm/yyyy")
I am also very new to VBA, so if anyone has any suggestions, if you could show the entire code (from SUB to END SUB) instead of just the code lines, as I am still learning what goes where.
Thank you!