Results 1 to 12 of 12

Thread: Please Help (Arrays)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Question

    I know I asked the same question about two days ago but I could really do with some help on this.

    Without changing names to the same or making the index values the same (or copy and paste) is there any way in code to make a number of command buttons into an array that could be used to return an index value.

    i.e. something like

    sub myarray

    dim mybuttonarray(3) as control/object

    mybuttonarray = (cmdone,cmdtwo,cmdthree)

    end sub

    sub mybuttonarray_click (index as integer)

    then code dependent on which button is clicked.....

    end sub

    Please help...

    cheers
    Steve



  2. #2
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    So what have you got against control arrays? Is there some reason you can't use one? Because that is exactly what you need to do I reckon...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Cool COOX I believe you are an excel expert

    Because as far as I know you can't create control arrays in Excel !! i.e. change the names to the same or change the index values to the same

    Coox in another post you stated that you could aid people with excel questions, please help me then......

    Steve

  4. #4
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Ok. Now, so far as getting all buttons to call one sub, and then that sub doing different things depending on who called it (just put three buttons on a form):
    Code:
    Private Sub CommandButton1_Click()
     Master ActiveControl.Name
    End Sub
    
    Private Sub CommandButton2_Click()
     Master ActiveControl.Name
    End Sub
    
    Private Sub CommandButton3_Click()
     Master ActiveControl.Name
    End Sub
    
    
    Sub Master(myName As String)
     MsgBox myName & " was pressed"
    End Sub
    Does that do what you want?

    [Edited by coox on 08-23-2000 at 05:45 AM]

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    You can take this even further, and pass the entire CommandButton control to the Master sub, then you'd be able to change its properties.
    Code:
    Sub Master(MyControl As CommandButton)
        MyControl.Caption = "You clicked me! OUCH! Don't do that!" :rolleyes:
    End Sub
    
    Private Sub CommandButton1_Click()
        Call Master(ActiveControl)
    End Sub
    
    Private Sub CommandButton2_Click()
        Call Master(ActiveControl)
    End Sub
    
    Private Sub CommandButton3_Click()
        Call Master(ActiveControl)
    End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Thanks

    I can see what you are saying but I was trying to avoid using

    command1_click
    command2_click etc..

    As I have 26 buttons. They are not actually on a form but are embedded into the spreadsheet and when the user presses the buttons one of the 26 spreadsheets are shown. I was just trying to think of a way that it could be done without having to type comm2_click comm3_click etc...

    What Yonatan states about passing the commandbutton itself is fine except it doesn't solve the problem of

    "Again repeating myself"

    Command1_click
    Command2_click etc...

    Is there not a way that I can just call

    MYBUTTONARRAY_click (INDEX as Integer)

    Where MYBUTTONARRAY is a collection of commandbuttons

    Or am I just completely losing it !!!

    Steve




  7. #7
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    As you said: Excel isn't sophisticated enough to allow you to use control arrays.
    So use some sub which will handle all of them.
    However, you will have to add (at least) 78 lines to call it from each CommandButton.
    Life's tough, but Microsoft is tougher.

  8. #8
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Listen, all you got to do is this -

    1. Go to your first sheet, view the Control Toolbox, go into Design mode.

    2. Locate each command button and, still in design mode, double-click on it. This will generate the basic click event (which is all you want) for each button, eg:
    Code:
    Private Sub CommandButton1_Click()
    
    End Sub
    3. In the VB editor, turn Full Module View (at the bottom their, looks a bit like the Align... buttons in Excel and Word) and you'll see all the click events for all the buttons all at once.

    4. Key (or copy and paste from this posting) Yonatan's code (the call Master(ActiveControl) bit - nice one Yonatan!) into one click event, then paste it into all the others - take you about 30 seconds. And you won't have to edit a thing...

    That's it, couldn't really be simpler. I don't think there's a way of doing what you're after, ie having a single bit of code that somehow recognizes and deals with all the click events in the whole workbook. Anyway, that should work fine...


  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    DOH !!!

    Oh well !! thanks for the responses anyway.

    Steve

  10. #10
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Blimey, what's my spelling like - just realised I put "their" instead of "there"...

    Steve, you sound like a right lazy arse... oh well, I suppose computer's are supposed to do all the work for us eh?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    Thumbs up Sorry COOX

    Sorry COOX, I must have been writing my response at the same time....

    Thanks but what you have said is pretty much what I had already done.

    Cheers Anyway
    Steve

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    178

    OH Yes !!

    Not just a lazy arse !!

    Pretty much lazy everything, elbows, arms, hands etc....

    Especially brain !

    Steve

Posting Permissions

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



Click Here to Expand Forum to Full Width