Consulting

Results 1 to 8 of 8

Thread: Sheet names

  1. #1

    Sheet names

    I have a checkbox and a button that needs to be named the same name as the sheet it is on. Is there a way to link its name to the name of the sheet. If the sheet name changes, so does the name for the checkbox and button.
    Any ideas?

    Thanks,
    John.

  2. #2
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location
    What toolbar did the button and the checkbox come from?

    Control Toolbox or Forms?
    Justin Labenne

  3. #3
    Forms.

    Someone asked why do I need that. FYI:

    I have 18 sheets that I have to change between. I can't see all 18 when all are visible. I have a master sheet. I click on a checkbox to unhide/goto the sheet I'm referring to. When I return I hit a return button. Instead of having 17 macros for the checkboxes and 17 for the return buttons. If they are labled as the sheetnames they come from I can use application.caller to see which sheet needs to be unhidden and same for the return, which one being closed. I've manually changed the names to match. I want to automate it so it can be kept somewhat self maintained.

    Thanks.
    JohnnyAppleSeed.

  4. #4
    VBAX Mentor Justinlabenne's Avatar
    Joined
    Jul 2004
    Location
    Clyde, Ohio
    Posts
    408
    Location
    I am not sure just how your setup is working, but something like this should get you pointed in the direction you want:

    Option Explicit
    
    Sub ChangeToSheetNames()
     Dim s As Shape
    For Each s In ActiveSheet.Shapes
            If s.Type = msoFormControl Then
                s.Select
                On Error Resume Next
                Selection.Characters.Text = ActiveSheet.Name
            End If
        Next s
    End Sub
    Justin Labenne

  5. #5
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    If you're aiming for self-maintaining code, then try this slightly different approach:
    Put a listbox on the Master sheet and add the code below to the sheet's code.
    This will hide and show worksheets whatever they are called.

    Private Sub Worksheet_Activate()
    Dim ws As Worksheet
    ListBox1.Clear
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> "Master" Then
                ListBox1.AddItem ws.Name
                If ws.Visible = True Then
                    ListBox1.Selected(ListBox1.ListCount - 1) = True
                End If
            End If
        Next
    End Sub
    
    Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim i As Long
    For i = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) = True Then
                Worksheets(ListBox1.List(i, 0)).Visible = True
            Else
                Worksheets(ListBox1.List(i, 0)).Visible = False
            End If
        Next I
    End Sub
    K :-)

  6. #6

    Solved.

    Thanks guys that was most helpful.John.

  7. #7
    VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by Killian

    Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim i As Long
    For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
    Worksheets(ListBox1.List(i, 0)).Visible = True
    Else
    Worksheets(ListBox1.List(i, 0)).Visible = False
    End If
    Next i
    End Sub
    Wonderful code here. Glad for your help.

  8. #8
    VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Another approach to this problem is to create a floating toolbar listing all sheet names - have a look at http://www.vbaexpress.com/kb/getarticle.php?kb_id=152

Posting Permissions

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