vba 用于从第二个选项卡上的记录计数复制和粘贴变量数或行的宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20012642/
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 00:28:32  来源:igfitidea点击:

Macro to Copy and Paste down Variable Number or Rows from Record Count on Second Tab

excel-vbavbaexcel

提问by user2998195

I have an Excel workbook with 2 tabs: "Data", and "Report". Each tab has a header row (row 1).

我有一个带有 2 个选项卡的 Excel 工作簿:“数据”和“报告”。每个选项卡都有一个标题行(第 1 行)。

Every time I run a new report, I'll paste new data onto the "Data" tab. The "Report" tab includes some formulas that reference data on the "Data" tab. I want to be able to create a Macro to copy-down the formulas on the "Report" tab to the corresponding number of rows from the "Data" tab. (The number of rows will vary each time I run the report).

每次运行新报告时,我都会将新数据粘贴到“数据”选项卡上。“报告”选项卡包括一些引用“数据”选项卡上的数据的公式。我希望能够创建一个宏来将“报告”选项卡上的公式复制到“数据”选项卡中相应的行数。(每次运行报告时,行数都会有所不同)。

For simplicity sake, let's say on the "Report" tab, I have a header row and the some formulas in A2 & B2. I want to be able to run a Macro that will copy-down these formulas for the corresponding number of rows to the row count for the "Data" tab.

为简单起见,让我们在“报告”选项卡上说,我有一个标题行和 A2 和 B2 中的一些公式。我希望能够运行一个宏,将这些公式的相应行数复制到“数据”选项卡的行数。

I've found the following code - but am not sure how to modify it to meet my needs:

我找到了以下代码 - 但不知道如何修改它以满足我的需要:

Sub CopyValueDown() Dim lRow As Integer

Sub CopyValueDown() 将 lRow 变暗为整数

Sheets("Data").Select
lRow = Range("A" & Rows.Count).End(xlUp).Row
If lRow = 1 Then GoTo End
Range("A1:B1").AutoFill Destination:=Range("A1:B" & lRow)
End Sub

Any suggestions?

有什么建议?

采纳答案by Tim Williams

Sub CopyValueDown() 

Dim lRow As Integer

    lRow = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
    If lRow > 1 Then 
        Sheets("Report").Range("A2:B2").AutoFill _
                  Destination:=Range("A2:B" & lRow)
    end if
End Sub