Results 1 to 3 of 3

Thread: Variable as button name

  1. #1
    New Member
    Join Date
    Apr 12
    Posts
    2

    Variable as button name

    Hello,

    I am using macros in excel, and I want to use a variable to define the name of a button to change its backcolor. What Ive done is as follows:

    lote = Label2.Caption


    If OptionButton1.Value = True Then lote.BackColor = vbYellow

    However it does not recognize lote as a button name to change the backcolor. Can anyone help me please?

    Thanks.

  2. #2
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 05
    Location
    Poland, Lodz
    Posts
    189

    Re: Variable as button name

    Hi,

    You can access ActiveX controls on sheet using OLEObjects collection:
    Code:
        Dim strButtonName As String
        strButtonName = "cmdButton2"
        Sheets(1).OLEObjects(strButtonName).Object.BackColor = vbRed
    If you command button is placed on form you can use Controls collection:

    Code:
        Dim strButtonName As String
        strButtonName = "cmdButton1"
        Me.Controls(strButtonName).BackColor = vbBlue
    Hope this helps.

    Regards,
    Sebastian
    using VB 2010 .NET Framework 4.0; MS Office 2010; SQL Server 2008 R2 Express Edition | Remember to mark resolved threads and rate useful posts.

  3. #3
    New Member
    Join Date
    Apr 12
    Posts
    2

    Re: Variable as button name

    Thanks you very much Sebastian.

    It works well.

Posting Permissions

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