Data Validation in VBA use RC notation

brenner

Board Regular
Joined
Sep 13, 2007
Messages
96
I have two procedures. First procedure (cell validation) has all the details that are passed to the second procedure (cell validation rules), which applies the data validation and conditional formatting. I'm trying to pass several and formulas (BtnNumberRange) using RC notation. I can only seem to pass them as strings. I need them to convert to relative references within the data validation across the validation range. My UsedArray are column numbers. My TopNumberRange are dates.


Thanks in advance.



Private Sub CellValidation()
For Each sht In ActiveWorkbook.Worksheets
LastRow = sht.Cells(LastRowExcel, 1).End(xlUp).row
TabName = sht.Name
sht.Activate

UsedArray = Array(iWsC_DTOCS, iWsC_DTOCE, iWsC_RFTOCS, iWsC_RFTOCE)
TopNumberRange = Array(Range("YearStart"), Range("YearEnd"))
BtnNumberRange = Array("=and(RC>=" & TopNumberRange(1) & "," & TopNumberRange(2) & ">RC)", "and(RC>RC[-1]," & TopNumberRange(2) & ">=RC)", "and(RC>RC[-4]," & TopNumberRange(2) & ">RC)", "and(RC>RC[-1]," & TopNumberRange(2) & ">=RC)")

NumberMessagePrompt = "Please enter an appropriate date based on the dates that you previously provided."
Call CellValidationRules(TabName, UsedArray, , , BtnNumberRange, TopNumberRange, NumberMessagePrompt)
Next sht
End Sub




Private Sub CellValidationRules(TabName, UsedArray, Optional DropDownArray, Optional CondFormExpression, Optional BtnNumberRange, Optional TopNumberRange, Optional NumberMessagePrompt)
For i = 1 To UBound(UsedArray)
ValidationRange = Sheets(TabName).Range(Cells(iWsStartRow, UsedArray(i)), Cells(LastRow, UsedArray(i))).Address
With Range(ValidationRange).Validation
.delete
.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=BtnNumberRange(i)
.IgnoreBlank = False
.ErrorMessage = NumberMessagePrompt
End With
Next i
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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