Excel VBA:带工作表选择器的打开工作簿书

时间:2018-06-26 17:37:25

标签: excel-vba vba excel

我正在尝试创建一个宏来打开工作簿,当它打开时可以选择在工作簿中选择某个工作表以复制并粘贴到宏最初运行的位置。到目前为止,我已经有了打开工作簿对话框的代码,并且打开了组合框以选择工作表,但是我得到了运行时error

Sub import()

'Removes subtotal and preps sheet for new data

Columns("A:D").Select
Selection.RemoveSubtotal
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.ClearContents
Range("A2").Select
Range("A1") = "Code"
Range("B1") = "Description"
Range("C1") = "Cases"
Range("D1") = "Pounds"


Dim filedialog As filedialog, strPathFile As String, wbSource As Workbook, cmdBar As CommandBar, cmdBarBtn As CommandBarButton, sht As Worksheet


'file picker for the mix file

Set filedialog = Application.filedialog(msoFileDialogFilePicker)
With filedialog
    .InitialFileName = "P:\Chambers"
    .AllowMultiSelect = False
    .Filters.Clear
    .Title = "Open Mix"

    If .Show = False Then
        MsgBox "No file selected. Please select file"
        Exit Sub
    End If
    strPathFile = .SelectedItems(1)
End With

Set wbSource = Workbooks.Open(Filename:=strPathFile)

'Sheet picker to choose appropriate sheet
Set cmdBar = Application.CommandBars.Add("register", msoBarPopup)
For Each sht In wbSource.Worksheets
    Set cmdBarBtn = cmdBar.Controls.Add
    cmdBarBtn.Caption = sht.Name
    cmdBarBtn.Style = msoButtonCaption
    cmdBarBtn.OnAction = "SelectThatSheet"
Next sht
cmdBar.ShowPopup

Set mySht = Worksheets(Application.Caller(1))


End Sub

0 个答案:

没有答案