如何将结果(在另一个工作簿中完成)返回到原始工作簿?

时间:2019-02-14 06:17:33

标签: excel vba

我在工作簿1中有一个“更新”按钮,然后它要求用户找到excel文件,打开的工作簿将运行RunCode(),汇总数据并将其结果保存在范围A200:D256

我知道我想念打开的workbook(=workbook2)的代码,例如昏暗的XXXXXX

请帮助我不知道如何调暗?

以下是代码:

Sub Button1_Click()

  Dim sourceSheet As Worksheet
  Set sourceSheet = ActiveSheet

' Create and set the file dialog object.
    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd
        .Filters.Clear      ' Clear all the filters

        ' Give the dialog box a title, word for doc or Excel for excel files
        .Title = "Select an Excel File"

        ' Apply filter to show only a particular type of files
        .Filters.Add "Excel Files", "*.xls?", 1

        ' Do not allow users to select more than one file
        ' Set the value as "True" to select multiple files
        .AllowMultiSelect = False

        ' Show the file.
        If .Show = True Then
           Workbooks.Open Filename:=.SelectedItems(1)
           Debug.Print .SelectedItems(1)

           Call RunCode
        End If
    End With

    Call sourceSheet.Activate

    Dim lasRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).row

End Sub

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

如果使用类似以下的内容,则可以参考打开的工作簿:

Dim wbk as workbook
Set wbk = Workbooks.Open Filename:=.SelectedItems(1)

您可以通过以下方式获得汇总数据:

wbk.worksheets("sheetname").range("A200:D256").copy
thisworkbook.worksheets("sheetname").range("first cell to copy to").pastespecial (xlpasteall)

也可以使用工作表(工作表编号)或Activesheet代替工作表(工作表名称)。代替pastespecial(XlPasteAll),您可以使用pastespecial(XlPasteValues),它最适合您。