[英]Copy specific cell in column and paste in another worksheet
I am trying to write an excel macro that copies specific cells from one worksheet and pastes them in another. 我正在尝试编写一个Excel宏,该宏可以从一个工作表中复制特定的单元格并将其粘贴到另一个工作表中。 Unfortunately the code is not working and I can't see where I went wrong. 不幸的是,代码无法正常工作,我看不到哪里出错了。 Here is what I have: 这是我所拥有的:
Sub sbCopyRangeToAnotherSheet()
Range("A1").Select
Selection.Copy
Sheets("Sheet1").Select
lMaxRows = Cells(Rows.Count, "B").End(xlUp).Row
Range("B" & lMaxRows + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End
It is attempting to follow this pseudocode: 它正在尝试遵循以下伪代码:
If a row in worksheet one has a number its first cell and is blank for the rest, copy that number and paste it into cell A1 of sheet two. 如果工作表中的一行的第一个单元格有一个编号,其余的行为空白,则复制该编号并将其粘贴到第二页的单元格A1中。
else move down one cell and check again 否则向下移动一个单元格并再次检查
I assigned it to a button and runs when I click it. 我将其分配给一个按钮,并在单击它时运行。 I get a debugging error on the "Selection.PasteSpecial...." line. 我在“ Selection.PasteSpecial ....”行上收到调试错误。
I have no experience programming and this is my first attempt.Sorry for any mistakes. 我没有编程经验,这是我的第一次尝试。对于任何错误,深表歉意。 Google searches on this topic have led me to sites that my work computer has blocked for some reason. Google在该主题上的搜索使我进入了我的工作计算机由于某种原因被阻止的站点。
Thank you! 谢谢!
You are getting that error because your clipboard is getting empty by the time you are pasting. 您收到该错误消息是因为粘贴时剪贴板已空。
My suggestion. 我的建议。 Avoid the use of .Select . 避免使用.Select 。 Define your objects and work with them. 定义您的对象并使用它们。 You may want to see this link 您可能希望看到此链接
How to avoid using Select in Excel VBA macros 如何避免在Excel VBA宏中使用选择
Once you have declared your objects, move the copy line just before you are pasting. 声明对象后,请在粘贴之前移动复制行。
Or write something like this 或这样写
lMaxRows = Sheets("Sheet1").Range("B" & _
Sheets("Sheet1").Rows.Count).End(xlUp).Row + 1
Sheets("Blah Blah").Range("A1").Copy
Sheets("Sheet1").Range("B" & lMaxRows).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.