I want to be able to retain data validation input messages when dragging the value of one dv cell to another with a different input message. To do this I'm trying to establish a collection of input messages before any edits to the sheet are made so that when I drag a dv cell's value to another a sub is called that replaces the copied over input messages with the originals. In my sample cells A1:A3 have data validation with input messages message, message2, and message3 respectively.
In the following sub which I run before editing the sheet I establish the collection of input messages and pass it to a sub called test to be called upon a sheet change:
the test sub is as follows:
and the ThisWorkbook sheet change sub that calls test when an edit is made is as follows:
However when I edit the sheet in the target range a 424 object required error occurs on the 'test (dataValidationMessages)' line in the private sub saying dataValidationMessages = Empty. Also, when I run Sub StoreDataValidationInputMessages() before editing the sheet it seems to run the test sub automatically because the currently selected cell needs to have an input message. Any help would be greatly appreciated. I hate that there is no easy way to preserve data validation settings/contents when dragging one dv cell to another.
In the following sub which I run before editing the sheet I establish the collection of input messages and pass it to a sub called test to be called upon a sheet change:
VBA Code:
Sub StoreDataValidationInputMessages()
Dim ws As Worksheet
Dim cell As Range
Dim dataValidationMessages As New Collection
Set ws = ThisWorkbook.Worksheets("Sheet1")
For Each cell In ws.Range("A1:A3")
If Not cell.Validation Is Nothing Then
dataValidationMessages.Add cell.Validation.InputMessage, cell.Address
End If
Next cell
test dataValidationMessages
End Sub
the test sub is as follows:
VBA Code:
Sub test(ByRef dataValidationMessages As Collection)
Dim ac As Range
Set ac = ActiveCell
Dim cell As Range
For Each cell In Range(ac, ac.End(xlUp).Offset(1, 0))
cell.Validation.InputMessage = dataValidationMessages(cell.Address)
Next cell
End Sub
and the ThisWorkbook sheet change sub that calls test when an edit is made is as follows:
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim rng As Range
Set rng = Range("A1:A50")
If Not Intersect(Target, rng) Is Nothing Then
test (dataValidationMessages)
End If
End Sub
However when I edit the sheet in the target range a 424 object required error occurs on the 'test (dataValidationMessages)' line in the private sub saying dataValidationMessages = Empty. Also, when I run Sub StoreDataValidationInputMessages() before editing the sheet it seems to run the test sub automatically because the currently selected cell needs to have an input message. Any help would be greatly appreciated. I hate that there is no easy way to preserve data validation settings/contents when dragging one dv cell to another.