Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88030

Subscript out of range when using Userform input workbook as reference

$
0
0

I have put together a userform for the user to select an open workbook - ideally would like to have this integrated with a script so that the script runs in reference to the workbook chosen. However an out of range error presents when running the script:


Private Sub Go_Click()

 If ComboBox1.ListIndex = -1 Then
        MsgBox "Please select a workbook name and try again"
        Exit Sub
    End If

    Dim wb As Workbook, copytest As Range, pastetest As Range

    Set wb = Workbooks(ComboBox1.List(ComboBox1.ListIndex))


   Set copytest = wb.Worksheets(2).Columns(6)
   Set pastetest = Workbooks("VBA Workbook.xlsx").Worksheets(1).Columns(1)
   copytest.Copy Destination:=pastetest

        '~~> Do what you want

Debug.Print

End Sub

Combo box:

Private Sub UserForm_Initialize()


    Dim wkb As Workbook
    Me.Label1.Caption = "Please select the relevant workbook"
    With Me.ComboBox1
        '~~> Loop thorugh all open workbooks and add
        '~~> their name to the Combobox
        For Each wkb In Application.Workbooks
            .AddItem wkb.Name
        Next wkb
    End With
End Sub

Viewing all articles
Browse latest Browse all 88030

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>