[英]VBA - Copy-Paste to another workbook
我試圖在表的末尾將特定單元格從一個工作簿復制到另一個工作簿。 我認為問題在於使用'ActiveWorkbook'很多。 我得到錯誤“對象不支持此屬性或方法”,似乎宏從CurrentBook而不是上傳器復制單元格。
我該如何修復我的代碼?
Dim uploadfile As Variant
Dim uploader As Workbook
Dim CurrentBook As Workbook
Dim lastRow As Integer
Set CurrentBook = ActiveWorkbook
uploadfile = Application.GetOpenFilename()
If uploadfile = "False" Then
Exit Sub
End If
Workbooks.Open uploadfile
Set uploader = ActiveWorkbook
With uploader
Application.CutCopyMode = False
Range("A1:J100").Copy
End With
CurrentBook.Activate
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & lastRow & ":J" & lastRow + 100).Select
Selection.Paste
uploader.Close
End Sub
在這里,只是因為我在工作中感到無聊,成千上萬的帖子是這樣的:
Option Explicit
Sub CopyPaste()
Dim uploadfile As String 'not Variant, the filename will be a string
Dim wbCopy As Workbook 'Better than uploader As Workbook you need to declare variables easy to read
Dim wbPaste As Workbook 'same as above
Dim LastRow As Lon 'integer doesn't work (it will be a long delimited to integer) either Byte(0-255) or Long
Set wbPaste = ThisWorkbook 'the workbook containing the code
uploadfile = Application.GetOpenFilename()
If uploadfile = "False" Then
Exit Sub
End If
Set wbCopy = Workbooks.Open(uploadfile) 'declare your paste workbook like this
' Set uploader = ActiveWorkbook this way of referencing a workbook might give you errors, use the above
With wbCopy.Sheets("MySheetName") 'reference always the worksheets, change MySheetName for the actual sheet name
'Application.CutCopyMode = False this is useless here it's emptying the clipboard but below you copy something new
.Range("A1:J100").Copy 'you missed the . on the beginning (when using With you need to use the dot to reference the with)
End With
With wbPaste.Sheets("MySheetName") 'reference always the worksheets, change MySheetName for the actual sheet name
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Range("A" & LastRow).PasteSpecial xlPasteValues 'when you copy/paste you just need to find the first cell on the paste range, Excel will paste the whole range
End With
Application.CutCopyMode = False 'now you can use this to avoid warning message on closing the copy workbook
wbCopy.Close Savechanges:=False
End Sub
在VBA中,您不能使用Ative
。
所以替換所有Active
,你必須使用PasteSpecial
,它比Paste
更好
Sub test()
Dim uploadfile As Variant
Dim uploader As Workbook
Dim CurrentBook As Workbook
Dim lastRow As Integer
Set CurrentBook = ThisWorkbook
uploadfile = Application.GetOpenFilename()
If uploadfile = "False" Then
Exit Sub
End If
Set uploader = Workbooks.Open(uploadfile)
Application.CutCopyMode = False
uploader.Worksheets(1).Range("A1:J100").Copy
lastRow = CurrentBook.Worksheets(1).Cells(CurrentBook.Worksheets(1).Rows.Count, "A").End(xlUp).Row + 1
Range("A" & lastRow & ":J" & (lastRow + 100)).PasteSpecial xlPasteValues
uploader.Close
End Sub
這段代碼對我有用,
別客氣 :)
您可以在下面找到有關如何從一個工作簿復制到另一個工作簿的示例代碼:
Option Explicit
Sub test()
Dim wbSource As Workbook, wbDestination As Workbook
'Set both workbooks by name to avoid conflicts
Set wbSource = Workbooks("Book1")
Set wbDestination = Workbooks("Book2")
'Copy paste only values
wbSource.Worksheets("Sheet1").Range("A1").Copy
wbDestination.Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteValues
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.