We all use VBA macro to automate reports and for that we are very much dependent on Google. Google provides lot of information but at times it’s difficult to locate the right piece of information. This blog is created to share some useful VBA codes which can be used by professionals in their work. You will find ready to use codes and formulas to make your job easy.
Tuesday, May 12, 2015
Sunday, May 3, 2015
Multiple Selection in data validation
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RngDV As Range
Dim OldVal As String
Dim NewVal As String
If Target.Count > 1 Then GoTo exitHandler
Select Case Target.Column
Case 5 'this Case line works for column B only
'Case 2, 5, 6 'this Case line works for multiple columns
If Cells(Target.Row, 4) = "In" Or Cells(Target.Row, 3) = "Not In" Then
On Error Resume Next
'check the cell for data validation
Set RngDV = Target.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If RngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, RngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
NewVal = Target.Value
Application.Undo
OldVal = Target.Value
Target.Value = NewVal
If OldVal <> "" Then
If NewVal <> "" Then Target.Value = OldVal & ", " & NewVal
End If
End If
End If
End Select
exitHandler:
Application.EnableEvents = True
End Sub