+ Reply to Thread
Results 1 to 5 of 5

Using Variables Within Button Actions

Hybrid View

  1. #1
    Registered User
    Join Date
    09-30-2011
    Location
    A Place
    MS-Off Ver
    Excel 2003
    Posts
    3

    Using Variables Within Button Actions

    I am using a large number (21 presently, growing later) of buttons on one particular sheet,
    and rather than specifying B1.Visible = False .. B21.Visible = False, can I use a For statement somehow?
    For Butt = 1 To 21
        ("B" & Butt).Visible = False
    End For
    Also, all these buttons will be assigned variables based on their unique functions before referincing a generic function. i.e.

    'Each Button pressed disables the previous button
    ("B" & (Butt - 1)).Enabled = False
    Is there anyway to do this, or am I going to have to do it the long way, of copy and paste 21 times and renaming?

    Any assistance available?

    Thanks!
    Last edited by romperstomper; 09-30-2011 at 02:51 AM. Reason: tags

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

    Re: Using Variables Within Button Actions

    Are you talking about Forms buttons or ActiveX ones? The answers will be quite different for each.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    09-30-2011
    Location
    A Place
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Using Variables Within Button Actions

    ActiveX. I know VBA is limited in its capacity, wondering if its even possible to take shortcuts like those?

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

    Re: Using Variables Within Button Actions

    For the visibility, it's pretty straightforward:
    For Butt = 1 To 21
        Me.Shapes("B" & Butt).Visible = False
    Next Butt
    assuming the code is in the worksheet code module. (If not, replace Me with the relevant worksheet)

    For the click coding, you would basically need to have a routine that accepts an index number and then each button click would be:
    Private Sub B2_Click()
       SomeMacro 1
    End Sub
    for example.
    With Forms buttons it would be easier as you could include the number argument in the OnAction string, or have the macro check the shape name using Application.Caller.

    For the record, VBA is not that limited.

  5. #5
    Registered User
    Join Date
    09-30-2011
    Location
    A Place
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Using Variables Within Button Actions

    The Me.Shapes worked fine for Visible! Thanks for that.

    But what code can you use for enabled? It says Object doesn't support this proerty or method.

    I figure Me.Shapes is the incorrect part? What should I change that to?

    How about refering to variables? i.e. I want at least 21 variables of each type.

    Could I do something like

    For Vary 1 To 21
    Dim Sales(Vary) As Integer
    Dim String(Vary) As String
    Next Vary
    Last edited by PersonMan; 10-01-2011 at 04:54 AM.

+ 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