I’m trying to cut a master file. Columns P and R have data in some rows, and some do not. I want to tell VBA, if there’s data in that row, do not create a drop down list, but if there is no data, then create a drop down option for the manager.
How would I alter my data validation module to say if there is data already in there, ignore creating a drop down, but create a drop down option for any blank cells in those two columns
Here is my original module:
Sub DataValidation()
lastrow = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
With Range("P2:P" & lastrow).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, _
Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
.IgnoreBlank = True
.InCellDropdown = True
End With
With Range("R2:R" & lastrow).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
.IgnoreBlank = True
.InCellDropdown = True
End With
End Sub
Try code such as the following to target only those blank cells:
Sub DataValidation()
lastrow = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
With Range("P2:P" & lastrow).SpecialCells(xlCellTypeBlanks).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
.IgnoreBlank = True
.InCellDropdown = True
End With
With Range("R2:R" & lastrow).SpecialCells(xlCellTypeBlanks).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
.IgnoreBlank = True
.InCellDropdown = True
End With
End Sub
EDIT: There is one error to trap for. If the entire Range("P2:P" & lastRow)
is blank, then the code will return an error. This is because the .SpecialCells
method call returns a Range object, excepting at least 1 cell.
One work around is to use error handling, and store the range into a variable. If all of the cells are blank, then they all need validation. Otherwise, only validate those cells that are blank (if you truly never foresee this happening, then you can skip this). Code below:
Sub DataValidation()
Dim rng As Range
lastrow = ActiveWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
On Error Resume Next
Set rng = Range("P2:P" & lastrow).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If rng Is Nothing Then
With Range("P2:P" & lastrow).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
.IgnoreBlank = True
.InCellDropdown = True
End With
Else
With rng.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="Yes - Regularly Works Eligible Shift,No - Does Not Regularly Work Eligible Shift"
.IgnoreBlank = True
.InCellDropdown = True
End With
End If
'Reset to reuse with the R column
Set rng = Nothing
On Error Resume Next
Set rng = Range("R2:R" & lastrow).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If rng Is Nothing Then
With Range("R2:R" & lastrow).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
.IgnoreBlank = True
.InCellDropdown = True
End With
Else
With rng.Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
.IgnoreBlank = True
.InCellDropdown = True
End With
End If
End Sub
Answer:
You could do something like this
With Range("I1:I" & lastrow).SpecialCells(xlCellTypeBlanks).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="8%,10%,12%,15%"
.IgnoreBlank = True
.InCellDropdown = True
End With
Tags: excelexcel