Consulting

Results 1 to 9 of 9

Thread: Solved: Form to change sheets

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: Form to change sheets

    I want to create a macro that will read all the tabs in the active workbook and populate a form with the names to make it easier to switch between them (for books with too many tabs). I know how to create the form but I need help at least starting the code. I don't know how to populate the names onto the form.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    uses a listbox on a userform:
    [VBA]
    Private Sub ListBox1_Click()
    Dim i As Integer, sht As String
    For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
    sht = ListBox1.List(i)
    End If
    Next i
    Sheets(sht).Activate

    End Sub
    Private Sub UserForm_Initialize()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
    ListBox1.AddItem (ws.Name)
    Next ws
    End Sub
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Lucas,

    Sorry to bother you but I just tried this code and there are 2 problems
    1) It doesn't list the sheets (by name)
    2) It crashes when I click on the list box

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Crash is brash....what does it mean in this instance....
    works for me....what are you doing with the code Daniel?
    you have to create a userform and put a listbox on it...listbox1
    put the code in the userform code module
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I created a userform and put the code in the userform module. Sorry, let me try a few things

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I just inserted a userform...added the listbox and pasted the code from the previous post.....see attached. Let me know what you think the problem might have been.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Lucas,

    It is weird I have the same exact code as you but it doesn't work. The only thing I was missing is the option explicit which I added and it still doesn't work.

    Daniel

  8. #8
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I figured it out:

    I had to change ThisWorkBook to ActiveWorkBook

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You must be using it in an addin.....that would make that specific difference....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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