我知道我的VBA代码可以改进。我很确定我的代码可以看起来更好。我想标准化并减少重复相同的过程。这也会提高效率吗?我可以为此提供一些指导方针吗?从哪儿开始?我很高兴收到您的一些例子!我想学习更有效地编写代码。
' Kanban_Color
'
Dim white, red, green, orange, peach, yellow, pink, blue, maroon, violet, black As Long
white = RGB(255, 255, 255) 'White
red = RGB(255, 0, 0) 'Red
green = RGB(215, 228, 188) 'Green
orange = RGB(228, 109, 10) 'Orange
peach = RGB(242, 174, 92) 'Peach
yellow = RGB(255, 255, 113) 'Yellow
pink = RGB(255, 182, 193) 'Pink
blue = RGB(147, 205, 221) 'Blue
maroon = RGB(149, 55, 53) 'Maroon
violet = RGB(204, 192, 218) 'Violet
black = RGB(0, 0, 0) 'Black
ws_kanban.Select
Set shelf_card = ActiveSheet.Range("A1:E10")
Set trav_card = ActiveSheet.Range("A12:G21")
Set int_supp = ActiveSheet.Range("C17:E17")
Set int_supp_loc = ActiveSheet.Range("C20:E20")
Set section = ws_kanban_data.Range("G2")
Set section2 = ActiveSheet.Range("G2:H2")
Set warning = ActiveSheet.Range("G6")
Set warning2 = ActiveSheet.Range("G7")
Set issue_type = ActiveSheet.Range("F1:F10")
Set issue_type_trav = ActiveSheet.Range("H12:H21")
Set types = ws_kanban_data.Range("F2")
section2.Select
If section = "MECH" Then
shelf_card.Interior.Color = green
trav_card.Interior.Color = green
ElseIf section = "LAB" Then
shelf_card.Interior.Color = orange
trav_card.Interior.Color = orange
ElseIf section = "HOCC" Then
shelf_card.Interior.Color = peach
trav_card.Interior.Color = peach
ElseIf section = "SASW" Then
shelf_card.Interior.Color = yellow
trav_card.Interior.Color = yellow
ElseIf section = "SAMO" Then
shelf_card.Interior.Color = pink
trav_card.Interior.Color = pink
ElseIf section = "RRC" Then
shelf_card.Interior.Color = blue
trav_card.Interior.Color = blue
ElseIf section = "CUST" Then
shelf_card.Interior.Color = maroon
trav_card.Interior.Color = maroon
ElseIf section = "PE" Then
shelf_card.Interior.Color = red
trav_card.Interior.Color = red
ElseIf section = "PACK" Then
shelf_card.Interior.Color = violet
trav_card.Interior.Color = violet
ElseIf section = "LC" Then
shelf_card.Interior.Color = white
trav_card.Interior.Color = white
Else
section2.Font.Color = white
warning.Value = "Check Section!!"
For i = 1 To 2
shelf_card.Interior.Color = red
trav_card.Interior.Color = red
warning.Interior.Color = red
warning.Font.Color = white
Application.Wait (Now + TimeValue("0:00:01"))
shelf_card.Interior.Color = white
trav_card.Interior.Color = white
warning.Interior.Color = white
warning.Font.Color = red
Application.Wait (Now + TimeValue("0:00:01"))
Next i
warning.Font.Color = white
warning.Interior.Color = red
End If
If types = "M" Then
issue_type.Value = "Manual Issue "
issue_type_trav.Value = "Manual Issue "
issue_type.Interior.Color = black
issue_type_trav.Interior.Color = black
issue_type.Font.Color = white
issue_type_trav.Font.Color = white
ElseIf types = "B" Then
issue_type.Interior.ColorIndex = 0
issue_type_trav.Interior.ColorIndex = 0
issue_type.Font.Color = white
issue_type_trav.Interior.ColorIndex = 0
Else
issue_type.Interior.ColorIndex = 0
issue_type_trav.Interior.ColorIndex = 0
issue_type.Font.Color = white
issue_type_trav.Interior.ColorIndex = 0
warning2.Value = "Check Type!!"
For i = 1 To 2
shelf_card.Interior.Color = red
trav_card.Interior.Color = red
warning2.Interior.Color = red
warning2.Font.Color = white
Application.Wait (Now + TimeValue("0:00:01"))
shelf_card.Interior.Color = white
trav_card.Interior.Color = white
warning2.Interior.Color = white
warning2.Font.Color = red
Application.Wait (Now + TimeValue("0:00:01"))
Next i
warning2.Font.Color = white
warning2.Interior.Color = red
End If