ban
05-29-2008, 11:30 PM
Hi
When we do validations using data->validation , we get Retry/cancel MsgBox if we enter invalid data. And it wont allow the user to move to the next cell until he/She enters valid data into that cell.
The same I want to do using VBa Code using Ok/Cancel MsgBox.
The way I am trying to do this looks like ...
In WorkSheet_change Function
If Target.Address = "$D$10" Then
no = Sheets.Item(1).Range("D" & 10).Value
If no = 1 Then
x = no
Else
resp = MsgBox("Pls enter valid data", vbOKCancel, "validation")
If resp = 1 Then
Sheets.Item(1).Range("D" & 10).Select
ActiveCell.Interior.ColorIndex = 5
ElseIf resp = 2 Then
Call1
End If
End If
End If
And the Call1 function is :
Private Sub Call1()
Sheets.Item(1).Range("D" & 10).Value = x
End Sub
Private Sub Worksheet_Activate()
x = Sheets.Item(1).Range("D" & 10).Value
End Sub
It is working fine for Cancel button. Means it gets the previous valid data when we click the Cancel button. BUt when we click the Ok button and moved to the cell it is not displaying any error message. Actually there is no code written for this Message to display..
Anyone please help me .......
Thanks in advance.....
When we do validations using data->validation , we get Retry/cancel MsgBox if we enter invalid data. And it wont allow the user to move to the next cell until he/She enters valid data into that cell.
The same I want to do using VBa Code using Ok/Cancel MsgBox.
The way I am trying to do this looks like ...
In WorkSheet_change Function
If Target.Address = "$D$10" Then
no = Sheets.Item(1).Range("D" & 10).Value
If no = 1 Then
x = no
Else
resp = MsgBox("Pls enter valid data", vbOKCancel, "validation")
If resp = 1 Then
Sheets.Item(1).Range("D" & 10).Select
ActiveCell.Interior.ColorIndex = 5
ElseIf resp = 2 Then
Call1
End If
End If
End If
And the Call1 function is :
Private Sub Call1()
Sheets.Item(1).Range("D" & 10).Value = x
End Sub
Private Sub Worksheet_Activate()
x = Sheets.Item(1).Range("D" & 10).Value
End Sub
It is working fine for Cancel button. Means it gets the previous valid data when we click the Cancel button. BUt when we click the Ok button and moved to the cell it is not displaying any error message. Actually there is no code written for this Message to display..
Anyone please help me .......
Thanks in advance.....