+ Reply to Thread
Results 1 to 6 of 6

Trouble with If Then Else statement

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-25-2007
    Posts
    166

    Trouble with If Then Else statement

    Here's the code:

    Private Sub Worksheet_Change(ByVal IncurredTC100 As Range)
        If Application.ActiveCell = "Milestone" Then
            Application.ActiveCell.Offset(1, 0).Select
            Application.ActiveCell.Offset(0, 1).Select
                With Selection.Validation
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                    xlBetween, Formula1:="=MilestoneDrop1"
                    .IgnoreBlank = True
                    .InCellDropdown = True
                    .InputTitle = ""
                    .ErrorTitle = "Invalid Entry"
                    .InputMessage = ""
                    .ErrorMessage = _
                    "You must select a year from the list. These numbers are generated from the data you entered on the Study Specifications sheet under ""d. Key Project Dates."" Push cancel and try again."
                    .ShowInput = False
                    .ShowError = True
                End With
                Application.ActiveCell.Value = 1
                Application.ActiveCell.Offset(1, 0).Select
                With Selection.Validation
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                    xlBetween, Formula1:="=MilestoneDrop2"
                    .IgnoreBlank = True
                    .InCellDropdown = True
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = False
                    .ShowError = False
                End With
                Application.ActiveCell.Value = 1
                Application.ActiveCell.Offset(-1, 0).Select
        Else
            Application.ActiveCell.Offset(1, 0).Select
            Application.ActiveCell.Offset(0, 1).Select
                Application.ActiveCell.Validation.Delete
                Application.ActiveCell.FormulaR1C1 = False
        End If
    End Sub
    In case you're wondering, the reason I've used two Offset commands is because of merged cells. It might make more sense if you saw the xls.

    When it runs, the word "False" cascades diagonally down to the right for hundreds of cells, but the error is underneath the Else section. Everything under If/Then works fine.

    The other strange thing is that if I type anything in any cell in the worksheet (even outside the range defined in the macro), the word "False" cascades from that cell diagonally down to the right.

    Any suggestions?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello JChandler22,

    This is a common mistake when using the Worksheet_Change event. At the beginning of your code use Application.EnableEvents = False
    and at the end add Application.EnableEvents = True. This prevents the cascade event you are seeing. When you change a cell it triggers the Worksheet_ChangeEvent. This tell Excel to ignore events while the code is running.

    Sincerely,
    Leith Ross

  3. #3
    Forum Contributor
    Join Date
    06-25-2007
    Posts
    166
    Ah. That makes perfect sense.

    Thanks for the clarification. I'll try it out - I'm sure it will work!

    Many thanks.

  4. #4
    Forum Contributor
    Join Date
    06-25-2007
    Posts
    166
    Another question... if you take a look at the code from the original post, I think I have something else wrong. I'm not too familiar with VBA, so I'm sort of fumbling along...

    I'm trying to write the macro so that it only fires when certain cells are changed. Those cells are within a range that I've named IncurredTC100. Could someone tell me how I can achieve this?

    Thanks!

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Try

    If Not Intersect(Target, Range("IncurredTC100")) Is Nothing Then
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello JChandler22,

    Here is how. Add this line where appropriate in your Worksheet_Change code...
    If Intersect(Target, IncurredTC100) Is Nothing Then
      ' Exit the macro if selected cell is not part of the range
       Exit Sub
    End If
    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1