Excel VBA中的Do-While-Loop中的“无效的过程调用或参数”错误?

为什么有一个

“无效的过程调用或参数”

Excel VBA中的Do-While-Loop错误?

我似乎无法find问题,我不相信有一个错字。 该错误是针对带星号的代码行。

请注意, Worksheets("DTR").Cells(i,3)是一个date。

 For i = 2 To Total_rows_DTR m = Application.Match(Worksheets("DTR").Cells(i, 3), Worksheets("Holidays Table").Range("A2:A1048576"), 0) If Not IsError(m) Then If Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3), Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "Regular" Then x = 1 '** Error occurs in the following line: Do While Application.WorksheetFunction.Weekday(Worksheets("DTR").Cells(i, 3) - x) = 1 Or Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3)-x, Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "Regular" Or Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3)-x, Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "SNWH" And Application.WorksheetFunction.Sum(Worksheets("DTR").Range("P" & ix & ":S" & ix)) > 0 x = x + 1 Loop If Application.WorksheetFunction.Sum(Worksheets("DTR").Range("P" & i & ":S" & i)) = 0 Then Worksheets("DTR").Cells(i, 26) = 0 End If End If End If Next i 

1.第一种解决scheme(更新条件见2)

看看你的循环…

 Do While Application.WorksheetFunction.Weekday(Worksheets("DTR").Cells(i, 3) - x) = 1 _ Or Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3), Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "Regular" _ Or Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3), Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "SNWH" _ And Application.WorksheetFunction.Sum(Worksheets("DTR").Range("P" & i & ":S" & i)) > 0 x = x + 1 Loop 

在那里你只增加x ,你的循环的唯一x依赖部分是在第一个Or之前。

 Application.WorksheetFunction.Weekday(Worksheets("DTR").Cells(i, 3) - x) = 1 'only part that depends on x 

所以所有其他的语句可以在循环之前检查,因为它们是静态的(相对于x)。

所以你的循环在每次迭代中的计算都比以前less,因为ABC只计算一次。 我们可以这样做,因为它们不会通过迭代循环来改变。

 A = Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3), Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "Regular" B = Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3), Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "SNWH" C = Application.WorksheetFunction.Sum(Worksheets("DTR").Range("P" & i & ":S" & i)) > 0 Do While Application.WorksheetFunction.Weekday(Worksheets("DTR").Cells(i, 3) - x) = 1 _ Or A _ Or B _ And C x = x + 1 Loop 

2.由于OP更新了循环代码…

 Do While Application.WorksheetFunction.Weekday(Worksheets("DTR").Cells(i, 3) - x) = 1 _ Or Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3)-x, Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "Regular" _ Or Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3)-x, Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "SNWH" _ And Application.WorksheetFunction.Sum(Worksheets("DTR").Range("P" & ix & ":S" & ix)) > 0 x = x + 1 Loop 

… 4个条件中的每一个都取决于x ,我们可以这样做:

 Dim RunLoop As Boolean Do 'we split up the conditions into AD so if an error occurs we know in which condition A = Application.WorksheetFunction.Weekday(Worksheets("DTR").Cells(i, 3) - x) = 1 B = Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3) - x, Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "Regular" C = Application.WorksheetFunction.Index(Worksheets("Holidays Table").Range("B2:B1048576"), Application.WorksheetFunction.Match(Worksheets("DTR").Cells(i, 3) - x, Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "SNWH" D = Application.WorksheetFunction.Sum(Worksheets("DTR").Range("P" & i - x & ":S" & i - x)) > 0 'now we check all coditions RunLoop = A Or B Or C And D 'and exit the loop if the condition is false If RunLoop = False Then Exit Do x = x + 1 Loop 

注意:我还build议使用Option Explicit并正确地声明每个variables。

你的Do...Loop太复杂了。 更多信息在这里或这里 。

在循环中写入极端大的条件不被视为干净的代码。 如前所述,将条件分开来分离布尔函数并分别评估它们。 喜欢这个:

 Public Sub TestMe() While isIt1 Or isItRegular 'do something Wend End Sub Public Function isIt1() As Boolean isIt1 = Application.WorksheetFunction.Weekday(Worksheets("DTR").Cells(i, 3) - x) = 1 End Function Public Function isItRegular() As Boolean With Application.WorksheetFunction isItRegular = .index(Worksheets("Holidays Table").Range("B2:B1048576"), .match(Worksheets("DTR").Cells(i, 3), Worksheets("Holidays Table").Range("A2:A1048576"), 0)) = "Regular" End With End Function