将Excel工作表导入活动工作表

时间:2017-08-31 10:18:32

标签: excel vba excel-vba import

我有很多数据的发票。我想导出和导入数据。我创建了导出特定工作表的导出VBA(" Invoice Data")。我把它保存在某个地方。现在我需要将同一个文件导入活动工作表。

我有这段代码

Dim WB As Workbook
Dim SourceWB As Workbook
Dim WS As Worksheet
Dim ASheet As Worksheet

'Turns off screenupdating and events:
Application.ScreenUpdating = False
Application.EnableEvents = False

'Sets the variables:
Set WB = ActiveWorkbook
Set ASheet = ActiveSheet
Set SourceWB = Workbooks.Open(WB.Path & "\1.xlsx")  'Modify to match

'Copies each sheet of the SourceWB to the end of original wb:
For Each WS In SourceWB.Worksheets
    WS.Copy after:=WB.Sheets(WB.Sheets.Count)
Next WS

    SourceWB.Close savechanges:=False
    Set WS = Nothing
    Set SourceWB = Nothing

WB.Activate
ASheet.Select
    Set ASheet = Nothing
    Set WB = Nothing

Application.EnableEvents = True

这段代码效果很好。但我想选择带文件打开对话框的文件

任何人都可以帮助我

2 个答案:

答案 0 :(得分:1)

终于找到了代码......

Dim wbk1 As Workbook, wbk2 As Workbook

fileStr = Application.GetOpenFilename()

Set wbk1 = ActiveWorkbook
Set wbk2 = Workbooks.Add(fileStr)

wbk2.Sheets("invoice data").Copy After:=wbk1.Sheets(1)
谢谢你们

答案 1 :(得分:0)

如果您的问题是关于..如何使用文件打开对话框..您可以使用此代码

    NewWorkbook = Application.GetOpenFilename( _  
            FileFilter:="Excel 2003 (*.xls),*.xls,Excel 2007 (*.xlsx),*.xlsx,Excel 2007 (*.xlsm),*.xlsm", _  
            Title:="Select an Excel File", _  
            MultiSelect:=File)  
    If NewWorkbook = False Then  
        Exit Sub  
    Else  
        Workbooks.Open Filename:=NewWorkbook  
    End If  

如果要选择任何类型的文件,可以删除过滤器