Macro to choose an open workbook and Worksheet

DChambers

Active Member
Joined
May 19, 2006
Messages
257
Anyone have a macro that will display all the open workbooks and allow me to choose 1?
Then using the chosen workbook display all the worksheets and choose 1?

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Was hoping to add this to some existing VBA code wbChoice as workbook, wsChoice as worksheet - so I thought maybe an inputbox or msgbox - something that would ask the question then disapear when answered. Is that possible?
 
Upvote 0
Hmm. A MsgBox is just going to ...well.....give you a message and an InputBox is just going to ask for some input. I think what you will want is a UserForm with two listboxes in it.

Try creating a userform with two listBoxes and add this code. It is not complete yet but I am working on activating the selected sheet

Code:
Private Sub ListBox1_Click()
Dim actvWb, wsName As String
Dim ws As Object
    For i = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(i) Then
            actvWb = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
        End If
    Next i
    
Application.Workbooks(actvWb).Activate



End Sub

Private Sub UserForm_Initialize()
Dim wbName As String
Dim wb As Object
Dim listQty As Integer
Dim rStart As Range
Set rStart = Sheet1.Range("A:A")

With Me.ListBox1
    For Each wb In Application.Workbooks
        wbName = wb.Name
        .AddItem wbName
    Next
End With
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
Private Sub ListBox1_Click()
Dim actvWb, wsName As String
Dim ws As Worksheet
    For i = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(i) Then
            actvWb = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)
        End If
    Next i
    
Application.Workbooks(actvWb).Activate

Me.ListBox2.Clear
With Me.ListBox2
    For Each ws In Application.Workbooks(actvWb).Worksheets
        wsName = ws.Name
        .AddItem wsName
    Next
End With

End Sub
'***********************************
Private Sub ListBox2_Click()
Dim actvWs As String
Dim ws As Worksheet
    For i = 0 To Me.ListBox2.ListCount - 1
        If Me.ListBox2.Selected(i) Then
            actvWs = Me.ListBox2.List(Me.ListBox2.ListIndex, 0)
        End If
    Next i
    
Sheets(actvWs).Activate
Unload Me
End Sub
'***********************************
Private Sub UserForm_Initialize()
Dim wbName As String
Dim wb As Object
Dim listQty As Integer
Dim rStart As Range
Set rStart = Sheet1.Range("A:A")

With Me.ListBox1
    For Each wb In Application.Workbooks
        wbName = wb.Name
        .AddItem wbName
    Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,364
Messages
6,184,536
Members
453,239
Latest member
dbenthu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top