+ Reply to Thread
Results 1 to 7 of 7

Conditional Worksheet Events

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Conditional Worksheet Events

    Hello,

    I would like to have a nested if or select case statement to handle a worksheet event.
    The conditions it will check are:
    1. Make sure target is w/in range, otherwise EXIT
    2. Make sure that target offset value is not empty, otherwise display message
    3. All is good, open form

    I’ve tried various formulations and positionings of the statements ,but not all conditions are met with equal success.
    What happens is I get the the first two conditions, but the third doesn’t work.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        Dim Rng1 As Range
        Set Rng1 = Range("DBlClick")
        
    If Intersect(Target, Rng1) Is Nothing Then Exit Sub
        	 Cancel = True
    
     	 'If Intersect(Target.Cells.Offset(0, -6), Rng1) Is Nothing Then
       	  '  MsgBox "You must first fill in the necessary data "
      ‘Exit Sub   
    
            	  frm_EditPricing.Show
    
    End Sub

    I’ve attached a workbook for clarification

    Thanks
    Mark
    Attached Files Attached Files
    Last edited by max57; 01-31-2010 at 07:42 PM.

  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

    Re: Conditional Worksheet Events

    Hello Mark,

    Change this statement...
    If Intersect(Target, Rng1) Is Nothing Then Exit Sub
        	 Cancel = True
    To this....
    If Intersect(Target, Rng1) Is Nothing Then 
             Exit Sub
        	 Cancel = True
    End If
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Conditional Worksheet Events

    Hi Leith,
    I fixed the code as suggested, but alas, no go.
    I tried various permutations of the If statement but I think I'm missing something regarding the syntax.

    Here is the code as it stands now:

       
       Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
        Dim Rng1 As Range
        Set Rng1 = Range("DBlClick")
        
        If Intersect(Target, Rng1) Is Nothing Then
            Exit Sub
            Cancel = True
            
        ElseIf Intersect(Target.Cells.Offset(0, -6), Rng1) Is Nothing Then
            MsgBox "You must fill in the row before seeing this form"
            Exit Sub
            Cancel = True
           End If
           
          frm_EditPricing.Show

    I've attached the latest version of the worksheet to illustrate what's going on.

    Thanks!

    Mark
    Attached Files Attached Files

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

    Re: Conditional Worksheet Events

    Hello Max57,

    Here is the working code for the DoubleClick macro...
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
        Dim Rng1 As Range
        Set Rng1 = Range("DBlClick")
        
        If Intersect(Target, Rng1) Is Nothing Then
            Cancel = True
            Exit Sub
        End If
       
      'Cells that have formulas are not considered empty. This counts the cells filled in by the user 
        If WorksheetFunction.CountA(Cells(Target.Row, "B").Resize(1, 4)) = 0 Then
            MsgBox "You must fill in the row before seeing this form"
            Cancel = True
            Exit Sub
        End If
           
        frm_EditPricing.Show
          
    End Sub

  5. #5
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Conditional Worksheet Events

    Hello Leith,

    Exactly what I needed.

        If WorksheetFunction.CountA(Cells(Target.Row, "B").Resize(1, 4)) = 0 Then
    I had tried "Is Empty "to no avail. I was thinking about looking for another way to check the cell, but this bit of code wasn't even on my radar.

    One question: What purpose does Resize(1,4) serve in this example?

    Much obliged!

    Mark

  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

    Re: Conditional Worksheet Events

    Hello Mark,

    The Resize property allows you to change the number of rows and columns for a cell or range. For example, if the cell is "B10" then the range is expanded to 1 row by 4 columns so the working range is now "B10:E10".

  7. #7
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Conditional Worksheet Events

    Leith,

    Thanks again!
    Always a learning experience.

    Mark

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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