• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Looping Issue

Marsdon

New Member
Hi Guys

I have an problem in looping the code. I have two ranges (G and H) which contains From and To dates. And my requirement here is To Date should not be less than From date. IF SO it there should be msg Box Saying TO Date is lesser than from date. And it should clear the data in that particular cell. In the same way I have to do it for Range G9 to G 26. I have created a code for a single cells . But I want the same to be repeated the below rows. I feel there should be looping done but I dont know how to loop the same.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
  If Range("G9") < Range("F9") Then
  MsgBox "Please Change the Date"
  Range("G9") = Range("F9")
  Range("F9").Select
  End If
End Sub
 
Last edited by a moderator:
This will do it as you enter data
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Left(Target.Address, 2) = "$F" Or Left(Target.Address, 2) = "$G" Then
  If Cells(Target.Row, 6) > Cells(Target.Row, 7) Then
  MsgBox ("Please Check the Date in cell " + Target.Address)
  End If
End If

End Sub
 
Hi Marsdon

In my opinion this is not a vba solution. Data validation solves this one. Try and put a value in G9:G26 which is less than F9:F26. Error message pops up.

See file attached.


Take care

Smallman
 

Attachments

  • Alert1.xlsx
    9.1 KB · Views: 3
Thank You Mr Hui.

And Small Man

I already have a data validation updated in the same cell's. Thank You as well
 
Back
Top