+ Reply to Thread
Results 1 to 4 of 4

Call method based on cell change/value

Hybrid View

  1. #1
    Registered User
    Join Date
    07-16-2012
    Location
    U.S
    MS-Off Ver
    Excel 2007
    Posts
    34

    Call method based on cell change/value

    Hello,

    I am attempting to have my macro run anytime the value of B1 changes. If B1 = 1, I want to run (just for example) Sub x, if B1 = 2, run Sub y, if B1 = 3, run Sub z. Here's my code so far, which does not include the B1 value at all, just if B1 changes, run macro. I'm a noob at this, but I think I'm looking for an "And" clause to add on to each of my If statements.
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address = "$B$1" Then Run "Sheet1.x"
    If Target.Address = "$B$1" Then Run "Sheet1.y"
    If Target.Address = "$B$1" Then Run "Sheet1.z"
    Any help would be appreciated
    Last edited by Leith Ross; 07-17-2012 at 12:19 PM. Reason: Added Code Tags

  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: Call method based on cell change/value

    Hello j.lancaster1,

    The Select Case statement makes doing this much easier.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
      ' Check that only one cell was selected and it is in column "B".
        If Target.Cells.Count > 1 Then Exit Sub
        If Intersect(Target, Columns("B:B")) Is Nothing Then Exit Sub
    
      ' Cell must be a number, if not, exit.
        If Not IsNumeric(Target) Then Exit Sub
    
            Select Case Target
                Case 1: Call Sub x
                Case 2: Call Sub y
                Case 3: Call Sub z
            End Select
    
    End Sub
    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
    Registered User
    Join Date
    07-16-2012
    Location
    U.S
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Call method based on cell change/value

    Wow, that makes much more sense. Thank you!

  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: Call method based on cell change/value

    Hello j.lancaster1,

    You're welcome.

+ 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