[英]Excel - Macro to copy a dynamic row number from one workbook to another
I have tried searching for an answer to this, so far I have had no luck. 我试图寻找答案,到目前为止,我还没有运气。
The solution I am looking for is regarding two workbooks and being able to copy one row from the source workbook (it has 1000 rows) to another book (to only contain one row at any one time, not including column headers). 我正在寻找的解决方案是关于两个工作簿,并且能够将源工作簿(有1000行)中的一行复制到另一本书(在任何时候仅包含一行,不包括列标题)。
The other issue is that the next time the macro is run I need to look at the next row down in the source workbook eg 另一个问题是,下次运行宏时,我需要查看源工作簿中的下一行,例如
First run copies row 2 (as column headers are in row 1) into row 2 of the other book Second run copies row 3 into row 2 of the other book (as the program reading this book only looks in row 2). 第一次运行将第2行(因为列标题在第1行中)复制到另一本书的第2行中,第二次将第3行复制到另一本书的第2行中(因为阅读本书的程序仅在第2行中查找)。
So I assume a counter is needed in the code? 所以我假设代码中需要一个计数器?
If there are any suggestions it would be very much appreciated. 如果有任何建议,将不胜感激。
You will need a macro to copy the data over and since you are going across macro runs (one row each time the macro is run), you will need to store the value of the row you last copied somewhere outside the macro. 您将需要一个宏来复制数据,并且由于要遍历宏运行(每次运行宏时都要一行),因此需要将上次复制的行的值存储在宏之外的某个位置。 Let's call this LastRowCopiedOver 我们称之为LastRowCopiedOver
You can store this in one of a few places: 您可以将其存储在以下几个位置之一:
Every time the macro is run successfully, you will update the value for LastRowCopiedOver so that you know what row to pick the next time around. 每次宏成功运行时,您将更新LastRowCopiedOver的值,以便您知道下次选择哪一行。
When you reach the end of the book, you can display a messagebox with a Yes/No question to reset back to row 2. 到达书末时,您可以显示一个带有是/否问题的消息框,以重置为第二行。
A Static Function
will do the trick. Static Function
可以解决问题。
Static Function RowCounter() As Long
Dim i ' Value of locally declared variable is preserved between calls.
i = i + 1
RowCounter = i
End Function
Each time you call it from your main program, i
will increment by 1. Since RowCounter
is static, it will remember the value of i
is between calls. 每次从主程序调用它时, i
都会增加1。由于RowCounter
是静态的,因此它会记住i
的值介于两次调用之间。
Run the following sub several times and watch the MsgBox
increment. 运行以下子项几次,并观察MsgBox
增量。 The current row will be copied to "Sheet2". 当前行将被复制到“ Sheet2”。 Replace that with whatever your destination sheet is. 将其替换为您的目标表。
Sub main()
Dim iRow As Long
Do While iRow < 2 ' To skip headers row 1...
' Get next row number.
iRow = RowCounter
Loop
MsgBox iRow ' To show that each time main is called, iRow will be incremented by 1.
' Copy and paste that row...
Rows(iRow).Copy Destination:=Worksheets("Sheet2").Rows(2)
End Sub
You can reset the counter to zero by going in the VBA editor and pressing the reset button (the square) on the Standard toolbar or in the Run menu. 您可以通过进入VBA编辑器并按“标准”工具栏或“运行”菜单上的“重置”按钮(正方形)来将计数器重置为零。
If you save the counter in some protected/hidden cell or sheet, resetting it will be a PITA. 如果将计数器保存在某些受保护/隐藏的单元格或工作表中,则将其重置为PITA。
Thanks for the input, I spent a bit of time on a solution for this after reading the solutions above. 感谢您的输入,阅读上面的解决方案后,我花了一些时间来解决此问题。 I ended up creating a separate excel file, I then used some VBA script to create a counter and then copy one line from the large excel source file to the new file so it only contains one row of data. 我最终创建了一个单独的excel文件,然后使用一些VBA脚本创建了一个计数器,然后将一个行从大型excel源文件复制到新文件中,因此它仅包含一行数据。
I have now amended my QAWP script to run this excel macro and it now sets the data in the script using the new single row spreadsheet. 我现在修改了QAWP脚本以运行此excel宏,现在它使用新的单行电子表格在脚本中设置数据。
This seems to be working so far, and it means that I can create new test data separately from the datasheet being used by QAWP. 到目前为止,这似乎一直有效,这意味着我可以与QAWP使用的数据表分开创建新的测试数据。
I have posted the excel VBA script below for reference: 我已经在下面发布了excel VBA脚本以供参考:
Sub Copy_Next_Record()
Application.Workbooks.Open _
"C:\Documents and Settings\user\My Documents\Personal_Data.xls"
Dim RowCount As Range
Set RowCount = _
Workbooks("Full_Personal_Data.xls").Worksheets("Count").Range("A1")
Workbooks("Full_Personal_Data.xls").Worksheets("Data").Activate
Rows(RowCount).Copy _
Destination:=Workbooks("Personal_Data.xls").Worksheets("Sheet1").Rows(2)
RowCount.Value = RowCount.Value + 1
Workbooks("Personal_Data.xls").Close True
Workbooks("Full_Personal_Data.xls").Close True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.