简体   繁体   English

复制列中的特定单元格并粘贴到另一个工作表中

[英]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.

相关问题 Excel-复制选定的单元格并粘贴到另一个工作表中列的末尾 - Excel - Copy selected cell and paste to the end of a column in another worksheet 将特定的列从工作表复制/粘贴到另一个 - Copy/Paste Specific Columns from a Worksheet to another 如何将工作表复制为图像并将其粘贴到特定单元格中? - How do you copy a worksheet as an Image and paste it into a specific cell? 使用vba在工作表数组中复制并粘贴特定单元格(使用.find) - copy and paste specific cell(using .find) in worksheet array using vba 复制一个静态的命名范围并将其粘贴到另一个工作表中的变量单元格地址 - Copy a static named range and paste it to a variable cell adress in another worksheet 如何将单元格值复制并粘贴到同一工作簿中的另一个工作表 - How to copy and paste a cell value to another worksheet in the same workbook 如何通过匹配单元格值将范围复制并粘贴到另一个工作表 - How to copy and paste a range to another worksheet by matching a cell value 复制单元格范围并根据日期粘贴到另一个工作表中? - Copy cell range and paste in another worksheet based on the date? 将一个单元格从一个工作表复制并粘贴到另一个工作表,然后乘以一个值 - copy and paste a cell from one worksheet to another and multiply it by a value VBA 将单个单元格复制并粘贴到另一个工作表中定义的次数 - VBA Copy and paste a single cell a defined number of times in another worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM