Excel设置活动工作簿

时间:2016-08-24 05:29:06

标签: excel vba excel-vba

我搜索了论坛,找不到适合我问题的答案。我是Excel VBA的新手,无法激活我刚刚打开的工作簿。正下方是导致我麻烦的部分。

所以我按下一个按钮,它将我带到文件路径,然后选择要打开的文件。每周这个文件都有一个不同的名称,我不确定它是什么,直到我打开文件路径并查看它。

一旦打开它,我希望我的宏操作文件中的数据并复制并粘贴到运行代码的工作簿中。但是,当我运行此宏并打开文件时,它将不会激活新打开的工作簿并运行其余的宏来尝试操作原始文件中的数据。

我认为我需要以不同方式打开文件,以便我刚打开的工作簿是活动的,或者在不知道文件名的情况下弄清楚如何激活新打开的工作簿。谢谢你的帮助。

Dim filepath As String
filepath = Environ("USERPROFILE") & "\Dropbox\On the go ordering"
Call Shell("explorer.exe" & " " & filepath, vbNormalFocus)


Range("A6:E500").Select
Sub on_the_go_button()

    Dim RANKER As Workbook

    Set RANKER = ThisWorkbook

    Dim filepath As String
    filepath = Environ("USERPROFILE") & "\Dropbox\On the go ordering"
    Call Shell("explorer.exe" & " " & filepath, vbNormalFocus)


    Range("A6:E500").Select
    Selection.Copy
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$E$495").AutoFilter Field:=1, Criteria1:=RGB(213, _
        223, 248), Operator:=xlFilterCellColor
    Range("G1").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RIGHT(RC[-6],8)=""Subtotal"",VALUE(LEFT(RC[-6],6)),"""")"
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-4])"
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-4])"
    Range("G1:I1").Select
        Selection.Copy
    Range("G1:I500").Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    Selection.Copy
    RANKER.Activate
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Range("P1:Q74").Select
    Selection.Copy
    Sheets("Contest").Select
    Range("A3").Select

    ActiveCell.End(xlToRight).Select


    ActiveCell.Offset(0, 1).Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub

2 个答案:

答案 0 :(得分:2)

尝试以下...

 filepath = application.getopenfilename()
 Dim Wb as workbook
 Set Wb = workbooks.open(filepath)

使用WB作为工作簿对象

答案 1 :(得分:2)

因为OP指定了默认路径;我建议使用Application.FileDialog(msoFileDialogFilePicker)

enter image description here

USAGE:

Dim WeeklyWorkbook

Set WeeklyWorkbook = getWeeklyWorkbook

If WeeklyWorkbook Is Nothing Then
    MsgBox "No file selected", vbInformation, "Action Cancelled"
    Exit Sub
End If
Function getWeeklyWorkbook() As Workbook

    Dim fDialog As FileDialog, result As Integer
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

    'Optional: FileDialog properties
    fDialog.AllowMultiSelect = False
    fDialog.Title = "Select a file"
    fDialog.InitialFileName = Environ("USERPROFILE") & "\Dropbox\On the go ordering"
    'Optional: Add filters
    fDialog.Filters.Clear
    fDialog.Filters.Add "Excel files", "*.xls, *.xlsx, *.xlsm"
    fDialog.Filters.Add "All files", "*.*"

    'Show the dialog. -1 means success!
    If fDialog.Show = -1 Then
        Set getWeeklyWorkbook = Workbooks.Open(fDialog.SelectedItems(1))
    End If

End Function

参考:VBA FILEDIALOG – OPENING, SELECTING AND SAVING FILES AND FOLDERS