+ Reply to Thread
Results 1 to 2 of 2

VBA buttons with caption name and visible

  1. #1
    Registered User
    Join Date
    04-29-2015
    Location
    Lima, Peru
    MS-Off Ver
    Windows 7
    Posts
    1

    VBA buttons with caption name and visible

    Hi!!

    I have 20 buttons one next to each other in a userform. I want to insert names in cells Range a1:a10, but if there`s a name in a1, then commandbutton1 is visible, if theres a name in a2, then commandbutton2 is visible, and so on. the caption of the button have to match to the name in the cell.

    I already create the buttons in order so they match the cells numbers and put this code:

    Private Sub UserForm_Activate()


    If ActiveWorkbook.Worksheets("hoja1").Range("a1").Value <> "" Then
    CommandButton1.Caption = Range("d1")
    Else
    CommandButton1.Visible = False
    End If

    End Sub

    It works, but with 20 buttons in a frame and more than one frame with another 20 buttons, i dont want to repeat the code.

    Please help!

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA buttons with caption name and visible

    Hi rodarce and welcome to ExcelForum,

    Try the following code (included in the attached file) which works for 4 CommandButtons. You should be able to expand it by changing the value in the constant.
    Please Login or Register  to view this content.
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here


    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Changing multiple command buttons caption on userform using VBA
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-02-2022, 05:17 PM
  2. [SOLVED] Looking for help tidying up Macro vba code / visible/non visible buttons
    By darrenkaye in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-03-2012, 11:50 AM
  3. Add Caption to Options Buttons in Order
    By djblois1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-01-2011, 11:18 AM
  4. Changing caption of 50 buttons at once
    By bambino_32 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-06-2010, 06:03 AM
  5. How to change the Caption of Radio/Option Buttons Dynamically?
    By e4excel in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-29-2009, 05:40 PM

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