vba 从一行复制特定单元格并粘贴到另一个工作表上的不同单元格中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27882607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:47:20  来源:igfitidea点击:

Copy specific cells from one row and paste into different cells on another worksheet

excel-vbacopy-pastevbaexcel

提问by Abyrvalg

I saw many similar questions on this site but non of them has answer. Not sure why. Probably it is too simple to bother. Not for me though. I am not good at all with VBA and will not probably need to pick on it for another number of years. Hopefully someone will be kind to spend time and help.

我在这个网站上看到了很多类似的问题,但没有一个有答案。不知道为什么。可能是太简单了,打扰了。不过不适合我。我对 VBA 一点也不擅长,而且可能不需要再使用几年了。希望有人愿意花时间和帮助。

I have two sheets in a workbook. Sht1 contains data organized by rows. Rows populated daily, total number of rows will be 300 to 400 by the end of project. Sht2 represents a document form. Most of the cells on that form contain static information that does not change from one report to another. Except some dynamic cells that have to be populated from Sht1. I print the form and file the hard copy. I might come back and print some reports one more time if the hard copies gone missing or the data changed for some reason. The point is clear - I do not want to keep and manage 400 Word files. It is just painful.

我的工作簿中有两张纸。Sht1 包含按行组织的数据。每天填充行,到项目结束时,总行数将达到 300 到 400。Sht2 表示文档形式。该表单上的大多数单元格包含静态信息,这些信息不会从一份报告更改为另一份报告。除了一些必须从 Sht1 填充的动态单元格。我打印了表格并提交了硬拷贝。如果硬拷贝丢失或数据由于某种原因发生变化,我可能会再回来打印一些报告。重点很明确 - 我不想保留和管理 400 个 Word 文件。这只是痛苦。

What I wanted is assign a code to a command button which will call for an input box. I enter the row ID (I guess the rows should be numbered consequently from 1 to N). Then VBA takes data from some cells of that row, lets say C5 (when ID=4), E5 and H5 on Sht1 and copies them to cells B5, D5 and D7 on Sht2.

我想要的是为一个命令按钮分配一个代码,该按钮将调用一个输入框。我输入行 ID(我猜这些行的编号应该是从 1 到 N)。然后 VBA 从该行的某些单元格中获取数据,比如 Sht1 上的 C5(当 ID=4 时)、E5 和 H5,并将它们复制到 Sht2 上的单元格 B5、D5 和 D7。

Much appreciated for your time reading this and even more if you can help. Thank you.

非常感谢您花时间阅读本文,如果您能提供帮助,甚至更多。谢谢你。

回答by stifin

Here is some very simple code to copy data from one cell on Sheet1 to another cell on Sheet2.

这是一些非常简单的代码,用于将数据从 Sheet1 上的一个单元格复制到 Sheet2 上的另一个单元格。

Sub Macro1()

    Dim iRow As Integer

    iRow = InputBox("Which row?")

    Worksheets("Sht2").Cells(5, 2).Value = Worksheets("Sht1").Cells(iRow, 3).Value
    Worksheets("Sht2").Cells(5, 4).Value = Worksheets("Sht1").Cells(iRow, 5).Value
    Worksheets("Sht2").Cells(7, 4).Value = Worksheets("Sht1").Cells(iRow, 7).Value

End Sub

This takes values from the row specified on Sheet1, columns C, E, and H. It copies these values onto Sheet2, cells B5, D5, D7, respectively. Note the order of arguments to Cells(row, col), which is opposite to the Excel cell references that you would be used to. In other words, remember that Cells(1, 3)is actually C1.

这将从 Sheet1 上指定的行、C、E 和 H 列中获取值。它将这些值分别复制到 Sheet2、单元格 B5、D5、D7 上。请注意 的参数顺序Cells(row, col),这与您习惯的 Excel 单元格引用相反。换句话说,记住它Cells(1, 3)实际上是 C1。

This is about as simple as I can make it but it should set you in the right direction.

这与我所能做到的一样简单,但它应该让你朝着正确的方向前进。