我试图运行将复制文本的代码" ready"或者"取消"在L列中,基于在指定行中是否分别存在列S或T中的值。
我不断收到错误编译错误:无效的下一个控件变量引用。
我的代码如下,
Dim TimeToRun
Sub auto_open()
Call ScheduleCopyPriceOver
End Sub
Sub ScheduleCopyPriceOver()
TimeToRun = Now + TimeValue("00:00:10")
Application.OnTime TimeToRun, "CopyPriceOver"
End Sub
Sub CopyPriceOver()
Dim lRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Orders")
Application.ScreenUpdating = False
For SRow = 1 To 5000
For TRow = 1 To 5000
If ws.Cells(SRow, 19) = SRow Then
ws.Cells(SRow, 12).Select
ActiveCell.FormulaR1C1 = "ready"
ElseIf ws.Cells(TRow, 20) = TRow Then
ws.Cells(TRow, 12).Select
ActiveCell.FormulaR1C1 = "Cancel"
Call ScheduleCopyPriceOver
End If
Next SRow
Next TRow
End Sub
Sub auto_close()
On Error Resume Next
Application.OnTime TimeToRun, "CopyPriceOver", , False
End Sub
答案 0 :(得分:0)
这就是Variatus的意思...... 此外,没有必要按照您的代码选择单元格,因为在循环中选择单元格会使代码变慢。
Sub CopyPriceOver()
Dim lRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Orders")
Application.ScreenUpdating = False
For SRow = 1 To 5000
For TRow = 1 To 5000
If ws.Cells(SRow, 19) = SRow Then
ws.Cells(SRow, 12).Select
ws.Cells(SRow, 12).Value = "ready"
ElseIf ws.Cells(TRow, 20) = TRow Then
ws.Cells(TRow, 12).Select
ws.Cells(TRow, 12).Value = "Cancel"
Call ScheduleCopyPriceOver
End If
Next TRow
Next SRow
End Sub
答案 1 :(得分:0)
不完全确定您的数据是什么样的,以便能够检查IF
和ELSEIF
语句的有效性,也无法确定是否需要运行5,000次循环5000次... 。但是整理你的循环以防止编译错误:
For SRow = 1 To 5000
For TRow = 1 To 5000
If ws.Cells(SRow, 19) = SRow Then
ws.Cells(SRow, 12).FormulaR1C1 = "ready"
ElseIf ws.Cells(TRow, 20) = TRow Then
ws.Cells(TRow, 12).FormulaR1C1 = "Cancel"
Call ScheduleCopyPriceOver
End If
Next TRow
Next SRow
(注意:我也删除了不必要的小区选择)
答案 2 :(得分:0)
不要理解为什么你不能这样做
Sub CopyPriceOver()
Dim sRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Orders")
Application.ScreenUpdating = False
For sRow = 1 To 5000
If ws.Cells(sRow, 19) = sRow Then
ws.Cells(sRow, 12).Value = "Ready"
ElseIf ws.Cells(sRow, 20) = sRow Then
ws.Cells(sRow, 12).Value = "Cancel"
Call ScheduleCopyPriceOver
End If
Next sRow
Application.ScreenUpdating = True
End Sub