打开Excel文件后,从其他Excel文件中读取数据

时间:2017-04-06 06:11:26

标签: excel-vba vba excel

我正在尝试打开Excel文件并将其数据复制到我的其他工作簿。我能够通过文件对话框打开文件。我怎么想把数据复制到Excel表格上?

Sub selectfile()
    Dim strFileToOpen As String
    Dim i As Integer
    Dim count As Integer

    strFileToOpen = Application.GetOpenFilename _
                    (Title:="Please choose a file to open", _
                    FileFilter:="Excel Files *.xls* (*.xls*),")
    If strFileToOpen = False Then
        MsgBox "No file selected.", vbExclamation, "Sorry!"
        Exit Sub
    Else
        Workbooks.Open Filename:=strFileToOpen
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

 Option Explicit

 Sub selectfile()
 Dim strFileToOpen As String
 Dim i As Integer
 Dim count As Integer

 strFileToOpen = Application.GetOpenFilename _
 (Title:="Please choose a file to open", _
 FileFilter:="Excel Files *.xls* (*.xls*),")   
 If strFileToOpen = False Then
 MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Sub
Else
Workbooks.Open Filename:=strFileToOpen
'HERE
activesheet.name = "Copy Sheet"
Sheets("Copy Sheet").Range("A1:" & Sheets("Copy Sheet").SpecialCells(xlCellTypeLastCell).Address).Copy Destination:=Sheets("ENTER YOUR SHEET NAME HERE").Range("A1")
End If
End Sub