且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel VBA打开工作簿并复制内容

更新时间:2023-12-05 16:49:28

希望这有帮助。此代码将粘贴已被激活的工作表中的数据。如果要在特定工作表中粘贴数据,则可以使用工作表替换活动表(Sheetname)。

Hope this helps. This code will paste data in the sheet that is already active. you can replace Activesheet with Sheets("Sheetname") if you want to paste data in a specific sheet.

我认为粘贴的数据只能从列A到列Z,请根据需要进行更改。

I have assumed that the data being pasted will only be from column A to column Z, please change it as required.

FileToOpen = Application.GetOpenFilename _
(Title:="Please Choose the RTCM File", _
FileFilter:="Excel Files *.xls (*.xls),")

If FileToOpen = False Then
    MsgBox "No file specified.", vbExclamation, "Duh!!!" ' Notification that nothing is chosen
    Exit Sub
Else ' Load the file, copy the first sheet and paste it in active sheet ...
    ThisWorkbook.Activate
    ThisWorkbook.ActiveSheet.Range("A1:Z65536").ClearContents
    Workbooks(FileToOpen).Activate
    lrow=Workbooks(FileToOpen).Sheets("Sheet1").Cells(65536,1).End(xlUp).Row
    Workbooks(FileToOpen).Sheets("Sheet1").Range("A1:Z" & lrow).Copy
    ThisWorkbook.Activate
    ThisWorkbook.ActiveSheet.Range("A1").PasteSpecial xlPasteValues
End If