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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14611433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:25:02  来源:igfitidea点击:

Excel VBA open workbook and copy content

excelvbaexcel-vba

提问by sikas

I'm creating a VBA that should ask user for file, then copy the content of the first sheet (Sheet name is not the default and contains a space) .. After that I need to delete specific columns .. From the rows I need to delete the rows that contains specific text (Case intensive)

我正在创建一个应该要求用户提供文件的 VBA,然后复制第一张工作表的内容(工作表名称不是默认值,并且包含一个空格).. 之后我需要删除特定的列.. 从我需要的行删除包含特定文本的行(案例密集型)

All I can do now is load the file, but don't know how to copy the data to the active worksheet!!

我现在能做的就是加载文件,但不知道如何将数据复制到活动工作表中!!

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 ...

End If

回答by android_newbie

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.

希望这可以帮助。此代码会将数据粘贴到已经处于活动状态的工作表中。如果要将数据粘贴到特定工作表中,您可以用 Sheets("Sheetname") 替换 Activesheet。

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

我假设粘贴的数据只会从 A 列到 Z 列,请根据需要进行更改。

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