+ Reply to Thread
Results 1 to 6 of 6

run code when a cell is changed

Hybrid View

  1. #1
    Registered User
    Join Date
    05-25-2011
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    52

    run code when a cell is changed

    I have a drop down menu for the user to select the week number they want to view. Once they select the week I want code to run that will hide all but the data from that week. I wrote the code and it works when I run it as a macro, but I don't know how to make it automatically run once the week number cell is changed. I don't want to have to tell people to select their choice from the drop down menu, and then run the macro manually. I'm sure there has to be a way to insert this code into the worksheet itself, but I am unsure as to how to write it, and where to put it. Many thanks for the help.

    Here's my macro code if you're interested:

    Sub hideall()
    '
    ' hideall Macro
    '
    
    
        
        Columns("G:HA").Select
       
       Selection.EntireColumn.Hidden = True
       
          If Range("B1").Value = "Week 26" Then
       Columns("T:AI").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 27" Then
        Columns("AJ:AY").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 28" Then
        Columns("AZ:BO").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 29" Then
        Columns("BP:CE").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 30" Then
        Columns("CF:CU").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 31" Then
        Columns("CV:DK").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 32" Then
        Columns("DL:EA").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 33" Then
        Columns("EB:EQ").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 34" Then
        Columns("ER:FE").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 35" Then
        Columns("FH:FW").Select
       Selection.EntireColumn.Hidden = False
       
       ElseIf Range("B1").Value = "Week 36" Then
        Columns("FX:GM").Select
       Selection.EntireColumn.Hidden = False
       
          ElseIf Range("B1").Value = "Week 37" Then
        Columns("GN:HC").Select
       Selection.EntireColumn.Hidden = False
       
       
       End If
       
    End Sub
    btw, if it matters, the cell with the drop down menu for week number is cell B1

  2. #2
    Forum Contributor Dave H9's Avatar
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    418

    Re: run code when a cell is changed

    Put the following into the relevant worksheet module and change the WS_RANGE referemce to suit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "J:J"      '<<<< change to suit'
    
        On Error GoTo ws_exit
    
    
        If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
             Call hideall
        End If
    
    ws_exit:
    
    End Sub
    Dave H

  3. #3
    Registered User
    Join Date
    05-25-2011
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    52

    Re: run code when a cell is changed

    I tried the code, but maybe I don't understand what you mean by change to suit. I am unfamiliar with what WS_RANGE is, so it's hard for me to figure it out. I tried putting in B1 as the range, but it pulled up a box that looked like it wanted me to save something and I could neither save, nor close out, and ended up having to force close excel.

  4. #4
    Registered User
    Join Date
    07-13-2011
    Location
    Chennai, India
    MS-Off Ver
    Excel 2003
    Posts
    7

    Thumbs up Re: run code when a cell is changed

    Hi all,
    I have a
    Last edited by mrsrinivas1988; 07-14-2011 at 09:32 AM.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: run code when a cell is changed

    Again:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Forum Contributor Dave H9's Avatar
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2007
    Posts
    418

    Re: run code when a cell is changed

    gryffin13,

    WS_RANGE is specifying the column the change event will trigger on. If your week number is in column A then change "J:J" to "A:A" or if in column L change to "L:L" etc.

    Hope that helps

    Dave H

+ 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