我正在使用大量的IF语句,但我100%确定这可以做得更好更短(更干净)。
我一直在尝试使用谷歌找到解决方案,但我找不到任何适合我的问题。
如何使以下代码更清晰,更短?
Dim wsTab1 As Worksheet
Dim wsTab2 As Worksheet
Dim wsTab3 As Worksheet
Dim Array1 As Variant
Dim Array2 As Variant
Set wsTab1 = Sheets("Tab 1 - Prijslijst")
Set wsTab2 = Sheets("Tab 2 - Nieuwe prijzen")
Set wsTab3 = Sheets("Tab 3 - Prijslijst aangepast")
Array1 = Array("DK", "DL", "DM", "DN", "DO", "DP", "DQ", "DR", "DS", "DT", "DU", "DV", "DW", "DX", "DY", "DZ", "EA", "EB", "EC", "ED", "CQ", "CR", "CS", "CT", "CU", "CV", "CW", "CX", "CY", "CZ", "DA", "DB", "DC", "DD", "DE", "DF", "DG", "DH", "DI", "DJ")
Array2 = Array("W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V")
For i = 0 To UBound(Array1) 'loop through array
If wsTab1.Range(Array1(i) & xlCell2.Row).Value <> wsTab2.Range(Array2(i) & xlCell2.Row).Value Then 'do your comparison based on array values
wsTab3.Range(Array1(i) & xlCell2.Row).Value = wsTab2.Range(Array2(i) & xlCell2.Row).Value
wsTab3.Range(Array1(i) & xlCell2.Row).Interior.ColorIndex = 4
wsTab2.Range(Array2(i) & xlCell2.Row).Interior.ColorIndex = 4
End If
Next i
编辑:设法得到这个^
答案 0 :(得分:0)
如何将范围用作值数组以进行比较,如下所示:
请记住,我注意到IF语句之后的第二行有时会引用xlCell2.Row,有时它引用xlCell3.Row,为简单起见,我只使用了xlCell2.Row,你的代码可能是个错误吗?
Dim wsTab1 As Worksheet
Dim wsTab2 As Worksheet
Dim wsTab3 As Worksheet
Dim Array1 As Variant
Dim Array2 As Variant
Set wsTab1 = Sheets("Tab 1 - Prijslijst")
Set wsTab2 = Sheets("Tab 2 - Nieuwe prijzen")
Set wsTab3 = Sheets("Tab 3 - Prijslijst aangepast")
Array1 = Array("DK", "DL", "DM", "DN", "DO", "DP", "DQ", "DR", "DS", "DT", "DU", "DV", "DW", "DX", "DY", "DZ", "EA", "EB", "EC", "ED", "CQ", "CR", "CS", "CT", "CU", "CV", "CW", "CX", "CY", "CZ", "DA", "DB", "DC", "DD", "DE", "DF", "DG", "DH", "DI", "DJ")
Array2 = Array("W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V")
For i = 0 To UBound(Array1) 'loop through array
If wsTab1.Range(Array1(i) & xlCell2.Row).Value <> wsTab2.Range(Array2(i) & xlCell2.Row).Value Then 'do your comparison based on array values
wsTab3.Range(Array1(i) & xlCell2.Row).Value = wsTab2.Range(Array2(i) & xlCell2.Row).Value
wsTab3.Range(Array1(i) & xlCell2.Row).Interior.ColorIndex = 4
wsTab2.Range(Array2(i) & xlCell2.Row).Interior.ColorIndex = 4
End If
Next i
<强>更新强> 或者您可以通过执行以下操作来完成相同的操作而不使用数组:
Dim wsTab1 As Worksheet
Dim wsTab2 As Worksheet
Dim wsTab3 As Worksheet
Set wsTab1 = Sheets("Tab 1 - Prijslijst")
Set wsTab2 = Sheets("Tab 2 - Nieuwe prijzen")
Set wsTab3 = Sheets("Tab 3 - Prijslijst aangepast")
For i = 95 To 134
If wsTab1.Cells(i, xlCell2.Row).Value <> wsTab2.Cells(i - 92, xlCell2.Row).Value Then 'do your comparison based on array values
wsTab3.Cells(i, xlCell2.Row).Value = wsTab2.Cells(i - 92, xlCell2.Row).Value
wsTab3.Cells(i, xlCell3.Row).Interior.ColorIndex = 4
wsTab2.Cells(i - 92, xlCell2.Row).Interior.ColorIndex = 4
End If
Next i
End Sub
答案 1 :(得分:0)
我会让你开始 - 这是第一个(在你的代码中标记为'Inkoopprijs) - 其他'Inkoopstaffels等将以同样的方式简化
Sub demo2()
Dim wsTab1 As Worksheet
Set wsTab1 = Sheets("Tab 1 - Prijslijst")
Dim wsTab2 As Worksheet
Set wsTab2 = Sheets("Tab 2 - Nieuwe prijzen")
Dim wsTab3 As Worksheet
Set wsTab3 = Sheets("Tab 3 - Prijslijst aangepast")
Dim c2Row As Long
Dim c3row As Long
c2Row = xlCell2.Row
c3row = xlCell3.Row
Dim x As Long
Dim y As Long
For x = 23 To 42 ' Cols W to AP
y = x + 92
'Inkoopprijs
If wsTab1.Cells(c2Row, y).Value <> wsTab2.Cells(c2Row, x).Value Then
wsTab3.Cells(c2Row, y).Value = wsTab2.Cells(c2Row, x).Value
wsTab3.Cells(c3row, y).Interior.ColorIndex = 4
wsTab2.Cells(c2Row, x).Interior.ColorIndex = 4
End If
Next x