我正在尝试优化我的代码并找到代码的任何和所有复制,以最大限度地减少代码并使其易于阅读...我仍然对Case Switch
使用VBA和我很难想象这个。以下代码是否可以转换为Case Switch
方案?
If (optDelete.value) Then
If (chkbxValid.value) Then
Range("H4:I1000").ClearContents
End If
If (chkbxValidDuplicate.value) Then
Range("K4:L1000").ClearContents
End If
If (chkbxInvalid.value) Then
Range("N4:O1000").ClearContents
End If
If (chkbxInvalidDuplicate.value) Then
Range("Q4:R1000").ClearContents
End If
Else
If (chkbxValid.value) Then
Call SortData(Range("I4:I1000"), Range("H4:I1000"))
End If
If (chkbxValidDuplicate.value) Then
Call SortData(Range("L4:L1000"), Range("K4:L1000"))
End If
If (chkbxInvalid.value) Then
Call SortData(Range("O4:O1000"), Range("N4:O1000"))
End If
If (chkbxInvalidDuplicate.value) Then
Call SortData(Range("R4:R1000"), Range("Q4:R1000"))
End If
End If
答案 0 :(得分:1)
不确定
Select Case True
Case optDelete.Value And chkbxValid.Value
Range("H4:I1000").ClearContents
Case optDelete.Value And chkbxValidDuplicate.Value
Range("K4:L1000").ClearContents
Case optDelete.Value And chkbxInvalid.Value
Range("N4:O1000").ClearContents
Case optDelete.Value And chkbxInvalidDuplicate.Value
Range("Q4:R1000").ClearContents
Case chkbxValid.Value
Call SortData(Range("I4:I1000"), Range("H4:I1000"))
Case chkbxValidDuplicate.Value
Call SortData(Range("L4:L1000"), Range("K4:L1000"))
Case chkbxInvalid.Value
Call SortData(Range("O4:O1000"), Range("N4:O1000"))
Case chkbxInvalidDuplicate.Value
Call SortData(Range("R4:R1000"), Range("Q4:R1000"))
End Select
但根据您的需要,请考虑以下方面的清洁度:
在主程序中:
Call DeleteOrSortRoutine(optDelete.Value, chkbxValid.Value, Range("H4:I1000"), Range("I4:I1000"))
Call DeleteOrSortRoutine(optDelete.Value, chkbxValidDuplicate.Value, Range("K4:L1000"), Range("L4:L1000"))
Call DeleteOrSortRoutine(optDelete.Value, chkbxInvalid.Value, Range("N4:O1000"), Range("O4:O1000"))
Call DeleteOrSortRoutine(optDelete.Value, chkbxInvalidDuplicate.Value, Range("Q4:R1000"), Range("R4:R1000"))
然后,在模块的后面定义:
Private Sub DeleteOrSortRoutine(ByVal deleteCheck As Boolean, ByVal secondaryCheck As Boolean, ByVal range1 As Range, ByVal range2 As Range)
If deleteCheck And secondaryCheck Then
range1.ClearContents
ElseIf secondaryCheck Then
Call SortRange(range2, range1)
End If
End Sub
答案 1 :(得分:0)
你能试试这段代码吗?
Select Case optDelete.Value
Case chkbxValid.Value: Range("H4:I1000").ClearContents
Case chkbxValidDuplicate.Value: Range("K4:L1000").ClearContents
Case chkbxInvalid.Value: Range("N4:O1000").ClearContents
Case chkbxInvalidDuplicate.Value: Range("Q4:R1000").ClearContents
End Select
Select Case optSort.Value
Case chkbxValid.Value: Call SortData(Range("I4:I1000"), Range("H4:I1000"))
Case chkbxValidDuplicate.Value: Call SortData(Range("L4:L1000"), Range("K4:L1000"))
Case chkbxInvalid.Value: Call SortData(Range("O4:O1000"), Range("N4:O1000"))
Case chkbxInvalidDuplicate.Value: Call SortData(Range("R4:R1000"), Range("Q4:R1000"))
End Select