我有一个VBA脚本,允许我选择一个文件,然后从该文件复制一个范围并将其粘贴到目标工作表中。但是,每次执行此操作都会打开源文件然后关闭,如果我想将信息保存在剪贴板上,则会提示我。
我不知道在不打开源excel的情况下复制数据会更好。
Option Explicit
Sub DCDatabaseCopy()
Dim vFile As Variant
Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim wbCopyFrom As Workbook
Dim wsCopyFrom As Worksheet
Dim DCRowCount As Integer
Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = ActiveSheet
'-------------------------------------------------------------
'Open file with data to be copied
vFile = Application.GetOpenFilename("Excel Files (*.*)," & "*.*", 1, "Select Excel File", "Open", False)
'If Cancel then Exit
If TypeName(vFile) = "Boolean" Then
Exit Sub
Else
Set wbCopyFrom = Workbooks.Open(vFile)
Set wsCopyFrom = wbCopyFrom.Worksheets(1)
End If
'--------------------------------------------------------------
'Copy Range
DCRowCount = wsCopyFrom.Range("A1", wsCopyFrom.Range("A1").End(xlDown)).Rows.Count
wsCopyFrom.Range("A1:G" & DCRowCount).Copy
wsCopyTo.Range("A2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Close file that was opened
wbCopyFrom.Close SaveChanges:=False
End Sub
答案 0 :(得分:2)
关闭工作簿之前:
wbCopyFrom.Application.CutCopyMode = False
这将清除剪贴板。
答案 1 :(得分:-1)
使用前后两行代码,它将绕过剪贴板提示
with_
或者可能是你需要使用它:
Application.DiplayAlerts = FALSE
Application.DiplayAlerts = TRUE
显然,如果文件不在ActiveWindow中,那么用wherver替换那部分代码