也许你可以帮助我,我坚持这个问题。 ckuhn203建议我将所有重复if-statement
分解为function
。我正在努力,但我无法弄清楚我做错了什么。
我有这个子:
Sub st_verdelen()
Worksheets("totaallijst").Activate
lastRtot = Sheets("totaallijst").Cells.SpecialCells(xlCellTypeLastCell).row
counter_all = 0
For a = 1 To lastRtot
slbkoppeling = Sheets("totaallijst").Cells(a, 8).Value
slbAanwezig = Sheets("totaallijst").Cells(a, 9).Value
If Sheets("totaallijst").Cells(a, 7).Value = "2A2" And SLBers = "Mvs" And slbstdperklas = Sheets("StdntKlas").Cells(15, 5).Value And slbkoppeling = "" And slbAanwezig = "" Then
If counter_all <= slbstdperklas And UUU = 0 Then
Sheets("totaallijst").Cells(a, 9).Value = SLBers
counter_all = counter_all + 1
If counter_all = slbstdperklas Then
UUU = 1
End If
End If
End If
If Sheets("totaallijst").Cells(a, 7).Value = "1B4C" And SLBers = "htp" And slbstdperklas = Sheets("StdntKlas").Cells(16, 5).Value And slbkoppeling = "" And slbAanwezig = "" Then
If counter_all <= slbstdperklas And VVV = 0 Then
Sheets("totaallijst").Cells(a, 9).Value = SLBers
counter_all = counter_all + 1
If counter_all = slbstdperklas Then
VVV = 1
End If
End If
End If
If Sheets("tot_list").Cells(a, 7).Value = "2G4" And blsers = "htp" And blsstdperklas = Sheets("StdntKlas").Cells(16, 6).Value And blskoppeling = "" And blsAanwezig = "" Then
If counterW <= blsstdperklas And WWW = 0 Then
Sheets("tot_list").Cells(a, 9).Value = blsers
counter_all = counter_all + 1
If counterW = blsstdperklas Then
WWW = 1
End If
End If
End If
next a
end sub
我试图将它们切成碎片但不幸的是没有成功。我做过类似的事情:
Sub st_verdelen()
Worksheets("totaallijst").Activate
lastRtot = Sheets("totaallijst").Cells.SpecialCells(xlCellTypeLastCell).row
counter_all = 0
For a = 1 To lastRtot
If scan_slb("VCIT2A2", "Mvs", 15, 5, 0) Then
End If
If scan_slb("VCIT2A2", "htp", 16, 5, 0) Then
End If
If scan_slb("VCIT2A2", "htp", 16, 5, 0) Then
End If
next a
end sub
Function scan_slb(value1, value2, row, col, noneactief) As Boolean
scan_slb = (Sheets("totaallijst").Cells(a, 7).Value = value1 And SLBers = value2 And slbstdperklas = Sheets("StdntKlas").Cells(row, col).Value And slbkoppeling = "" And slbAanwezig = "")
counter_all = counter_all + 1
If counter_all = slbstdperklas Then
noneactief = 1
End If
End Function
我无法弄清楚我做错了什么。有什么建议吗?
需要counter_all
,以便不再重复此部分。如果我将其遗漏,那么我就无法激活noneactief
,以便if语句在被激活之后不会被激活。
答案 0 :(得分:1)
函数scan_slb(value1,value2,row,col,noneactief)似乎有一些问题,或者上下文不够。使用&#34; row&#34;作为参数名称不是好的做法,因为它是属性名称。您有时会修改传递给函数的noneactief的值。这很好,因为excel-vba默认是传递引用;但调用代码无论如何都不使用非实现,因此您需要编写代码来执行某些操作。我会做一些假设并对此有所了解:
counter_all = 0
For a = 1 To lastRtot
slbkoppeling = Sheets("totaallijst").Cells(a, 8).Value
slbAanwezig = Sheets("totaallijst").Cells(a, 9).Value
If scan_slb(sheets("totaallijst").Cells(a, 7).Value, "2A2", _
SLBers, "Mvs", slbstdperklas, Sheets("StdntKlas").Cells(15, 5).Value, _
slbkoppeling, "", slbAanwezig, "") Then
If counter_all <= slbstdperklas And UUU = 0 Then
Sheets("totaallijst").Cells(a, 9).Value = SLBers
counter_all = counter_all + 1
If counter_all = slbstdperklas Then
UUU = 1
End If
End If
End If
If scan_slb(Sheets("totaallijst").Cells(a, 7).Value, "1B4C", _
SLBers, "htp", slbstdperklas, Sheets("StdntKlas").Cells(16, 5).Value, _
slbkoppeling, "", slbAanwezig, "") Then
If counter_all <= slbstdperklas And VVV = 0 Then
Sheets("totaallijst").Cells(a, 9).Value = SLBers
counter_all = counter_all + 1
If counter_all = slbstdperklas Then
VVV = 1
End If
End If
End If
If scan_slb(Sheets("tot_list").Cells(a, 7).Value, "2G4", _
blsers, "htp", blsstdperklas, Sheets("StdntKlas").Cells(16, 6).Value, _
blskoppeling, "", blsAanwezig, "") Then
If counterW <= blsstdperklas And WWW = 0 Then
Sheets("tot_list").Cells(a, 9).Value = blsers
counter_all = counter_all + 1
If counterW = blsstdperklas Then
WWW = 1
End If
End If
End If
next a
End Sub
function scan_slb(a1,a2,b1,b2,c1,c2,d1,d2)
scan_slb = (a1 = a2 and b1 = b2 and c1 = c2 and d1 = d2)
End Function
假设除了counter_all之外你还有全局变量,并且你正在测试&#34;&#34;是字符串类型。使用函数可以考虑更多这方面的问题,特别是如果您愿意改变通过ByRef传递的变量。就个人而言,我已经把#34; ByVal&#34;在scan_slb中的所有变量之前。