[英]Excel VBA: Copy & Paste - Cells are being copied but not pasted
I am brand new to VBA and am working on writing some code that copies data from a column in one workbook and pastes the data into another. 我是VBA的新手,正在编写一些代码,这些代码可以从一个工作簿中的列复制数据,然后将数据粘贴到另一个工作簿中。
See my code below: 请参阅下面的代码:
Set src = Workbooks.Open("C:\Users\RP\Desktop\CopyFrom.xlsm", True, True)
Dim startRow As Integer, endRow as Integer
With src.Sheets("Sheet1")
startRow = .Range("B:B").Find(what:="*", after:=.Range("B1")).Row
endRow = .Range("B:B").Find(what:="", after:=.Range("B1"), searchdirection:=xlPrevious).Row
End With
src.Worksheets("Sheet1").Range("B" & startRow & ":B" & endRow).Copy
Sheets("PasteHere").Range("B1").PasteSpecial xlPasteValues
I have startRow
and startRow
since the data is in Column B
but does not start in cell B1
. 我有
startRow
和startRow
因为数据在B
列中,但没有在单元格B1
开始。
When I run the macro, I see that the data from the src
file is copied (it has the dotted "ants" around it and I can paste it wherever I please). 当我运行宏时,我看到
src
文件中的数据被复制了(它周围带有虚线的“蚂蚁”,可以将其粘贴到任何需要的位置)。 However, in my current workbook, there is no data pasted. 但是,在我当前的工作簿中,没有粘贴数据。
Can you help me figure this out? 你能帮我解决这个问题吗? Thank you!
谢谢!
As was commented you don't need to copy and paste if you just need the values; 如前所述,如果只需要这些值,则无需复制和粘贴; in fact copy and paste is much slower than working with the values directly!
实际上,复制和粘贴要比直接使用这些值慢得多!
Set src = Workbooks.Open("C:\Users\RP\Desktop\CopyFrom.xlsm", True, True)
Set dest = Workbooks.Open("C:\Users\RP\Desktop\CopyTo.xlsm", True, True)
Dim startRow As Integer, endRow as Integer
With src.Sheets("Sheet1")
startRow = .Range("B:B").Find(what:="*", after:=.Range("B1")).Row
endRow = .Range("B:B").Find(what:="", after:=.Range("B1"), searchdirection:=xlPrevious).Row
End With
dest.Worksheets("PasteHere").Range("B1").Value = _
src.Worksheets("Sheet1").Range("B1").Value
You can learn more about the Range object here: 您可以在此处了解有关Range对象的更多信息:
https://docs.microsoft.com/en-us/office/vba/api/excel.range(object) https://docs.microsoft.com/zh-cn/office/vba/api/excel.range(object)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.