[英]Do-Until Loop Stalling
I am trying to use a 'Do Until' loop to take a value from list (Drop-down fields worksheet, starting at cell B19) and update a cell value another Excel Sheet (specifically, Data Collection worksheet, cell C1).我正在尝试使用“直到”循环从列表中获取一个值(下拉字段工作表,从单元格 B19 开始)并更新另一个 Excel 表(特别是数据收集工作表,单元格 C1)的单元格值。 Once I can get this to work, I will add already functioning code to save the file based on the value in C1 in the Data Collection worksheet.一旦我可以让它工作,我将添加已经运行的代码以根据数据收集工作表中 C1 中的值保存文件。
I am testing the code but it is constantly getting stuck after the pulling that first value.我正在测试代码,但在提取第一个值后它不断卡住。 Basically, it doesn't actually loop through the list until it ends.基本上,它实际上不会循环遍历列表,直到它结束。
I believe it has to do with what is classified as the active cell.我相信这与归类为活动细胞有关。 I think when I paste the value that changes the active cell.我想当我粘贴更改活动单元格的值时。 I tried to correct this by re-iterating the active cell again.我试图通过再次重新迭代活动单元格来纠正这个问题。 This might be creating an infinite loop though.不过,这可能会造成无限循环。
Is there something I can do to adjust this?我可以做些什么来调整它吗? Thank you in advance for looking at this and any replies you might have!提前感谢您查看此内容以及您可能收到的任何回复! I based structure on the documentation found at https://docs.microsoft.com/en-us/office/troubleshoot/excel/loop-through-data-using-macro我基于在https://docs.microsoft.com/en-us/office/troubleshoot/excel/loop-through-data-using-macro找到的文档的结构
Code below:下面的代码:
Sub Test2()
' Select cell to start loop, *first line of data*.
Worksheets("Drop-down fields").Activate
Range("B19").Select
Worksheets("Drop-down fields").Range("B19").Copy
' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
Worksheets("Data Collection").Range("C1").PasteSpecial Paste:=xlPasteValues
Worksheets("Drop-down fields").Activate
Range("B19").Select
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Samuel,塞缪尔,
Here's your code refactored to remove all the activates and selects and incrementing through you list in col B and copying to col C.这是您的代码重构以删除所有激活和选择,并通过您在列 B 中的列表递增并复制到列 C。
Sub Test2()
Dim wksSrc as Worksheet
Dim wksDst as Worksheet
Dim lSrcRow as Long
Dim lDstRow as Long
Set wksSrc = Worksheets("Drop-down fields")
Set wksDst = Worksheets("Data Collection")
lSrcRow = 19
lDstRow = 1
Do
wksSrc.cells(lSrcRow,2).Copy
wksDst.Cells(lDstRow,3).PasteSpecial Paste:=xlPasteValues
lSrcRow = lSrcRow + 1
lDstRow = lDstRow + 1
Loop Until wksSrc.cells(lSrcRow,lSrcCol) = ""
End Sub 'Test2
Note: code is untested but I think I got all the references right.注意:代码未经测试,但我认为我得到了所有参考。 HTH HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.