自從我處理VBA以來,我已經有一段時間了,而且我寫了一些相當不雅的東西。長碼需要數組
有人可以提出一種方法來縮短以下代碼嗎?我想陣列可以幫助,但我不知道如何實現它。
該代碼的目的是建立螺紋管道系統,它必須以相當精確的長度進行組合。程序總是使用最大長度的管道,以適應所需管道長度的剩餘量。
爲了保持代碼儘可能短,我省略了維度變量和顯示結果的部分。
在此先感謝您的幫助。
Sub ThreadedPipeCalc()
Dim desLength As Single, end1 As String, end2 As String
Dim none As Single, CS_Con As Single, CS_Un As Single
Dim CS_90deg As Double, CS_Tee As Single, CS_Flange As Single
Dim CS_Con_ct As Integer, CS_Un_ct As Integer, CS_Flange_ct As Integer
Dim CS_90deg_ct As Integer, CS_Tee_ct As Integer
Dim CS_Con_ct_tot As Integer, CS_Un_ct_tot As Integer
Dim CS_90deg_ct_tot As Integer, CS_Tee_ct_tot As Integer
Dim A_pipe As Single, B_pipe As Single, C_pipe As Single
Dim D_pipe As Single, E_pipe As Single, F_pipe As Single
Dim H_pipe As Single, I_pipe As Single, J_pipe As Single
Dim K_pipe As Single, L_pipe As Single, M_pipe As Single
Dim N_pipe As Single, O_pipe As Single, P_pipe As Single
Dim Q_pipe As Single, R_pipe As Single, S_pipe As Single
Dim T_pipe As Single, U_pipe As Single, V_pipe As Single
Dim W_pipe As Single, X_pipe As Single, Y_pipe As Single
Dim Z_pipe As Single, Threadin As Single, FULLY_pipe As Single
Dim A_ct As Integer, B_ct As Integer, C_ct As Integer
Dim D_ct As Integer, E_ct As Integer, F_ct As Integer
Dim H_ct As Integer, I_ct As Integer, J_ct As Integer
Dim K_ct As Integer, L_ct As Integer, M_ct As Integer
Dim N_ct As Integer, O_ct As Integer, P_ct As Integer
Dim Q_ct As Integer, R_ct As Integer, S_ct As Integer
Dim T_ct As Integer, U_ct As Integer, V_ct As Integer
Dim W_ct As Integer, X_ct As Integer, Y_ct As Integer
Dim Z_ct As Integer, FULLY_ct As Integer
Dim A_ct_tot As Integer, B_ct_tot As Integer, C_ct_tot As Integer
Dim D_ct_tot As Integer, E_ct_tot As Integer, F_ct_tot As Integer
Dim H_ct_tot As Integer, I_ct_tot As Integer, J_ct_tot As Integer
Dim K_ct_tot As Integer, L_ct_tot As Integer, M_ct_tot As Integer
Dim N_ct_tot As Integer, O_ct_tot As Integer, P_ct_tot As Integer
Dim Q_ct_tot As Integer, R_ct_tot As Integer, S_ct_tot As Integer
Dim T_ct_tot As Integer, U_ct_tot As Integer, V_ct_tot As Integer
Dim W_ct_tot As Integer, X_ct_tot As Integer, Y_ct_tot As Integer
Dim Z_ct_tot As Integer, FULLY_ct_tot As Integer
Dim segCount As Integer
Dim CountRedux As Boolean, continue As Integer
continue = 6
none = 0
CS_Con = 2.53
SS_Con = 2.5
CS_Un = 3
SS_Un = 2.85
CS_90deg = 2.25
SS_90deg = 2.28
CS_Tee = 2.25
SS_Tee = 2.26
CS_Flange = 1
SS_Flange = 1
SS_Flang_red = 1.1875
SS_Cap = 1.77
Threadin = 0.563
A_pipe = 126
B_pipe = 72
C_pipe = 60
D_pipe = 48
E_pipe = 36
F_pipe = 24
G_pipe = 22
H_pipe = 20
I_pipe = 18
J_pipe = 16
K_pipe = 14
L_pipe = 12
M_pipe = 11
N_pipe = 10
O_pipe = 9
P_pipe = 8
Q_pipe = 7
R_pipe = 6.5
S_pipe = 6
T_pipe = 5.5
U_pipe = 5
V_pipe = 4.5
W_pipe = 4
X_pipe = 3.5
Y_pipe = 3
Z_pipe = 2.5
FULLY_pipe = 2
While continue = 6
segCount = 0
Range("C3:C32").Value = 0
CS_Con_ct = 0
CS_Un_ct = 0
CS_90deg_ct = 0
CS_Tee_ct = 0
CS_Flange_ct = 0
A_ct = 0
B_ct = 0
C_ct = 0
D_ct = 0
E_ct = 0
F_ct = 0
G_ct = 0
H_ct = 0
I_ct = 0
J_ct = 0
K_ct = 0
L_ct = 0
M_ct = 0
N_ct = 0
O_ct = 0
P_ct = 0
Q_ct = 0
R_ct = 0
S_ct = 0
T_ct = 0
U_ct = 0
V_ct = 0
W_ct = 0
X_ct = 0
Y_ct = 0
Z_ct = 0
FULLY_ct = 0
CS_Con_ct_tot = Range("D3")
CS_Un_ct_tot = Range("D4")
CS_90deg_ct_tot = Range("D5")
CS_Tee_ct_tot = Range("D6")
A_ct_tot = Range("D7")
B_ct_tot = Range("D8")
C_ct_tot = Range("D9")
D_ct_tot = Range("D10")
E_ct_tot = Range("D11")
F_ct_tot = Range("D12")
G_ct_tot = Range("D13")
H_ct_tot = Range("D14")
I_ct_tot = Range("D15")
J_ct_tot = Range("D16")
K_ct_tot = Range("D17")
L_ct_tot = Range("D18")
M_ct_tot = Range("D19")
N_ct_tot = Range("D20")
O_ct_tot = Range("D21")
P_ct_tot = Range("D22")
Q_ct_tot = Range("D23")
R_ct_tot = Range("D24")
S_ct_tot = Range("D25")
T_ct_tot = Range("D26")
U_ct_tot = Range("D27")
V_ct_tot = Range("D28")
W_ct_tot = Range("D29")
X_ct_tot = Range("D30")
Y_ct_tot = Range("D31")
Z_ct_tot = Range("D32")
FULLY_ct_tot = Range("D33")
desLength = Application.InputBox("Enter the desired end to center or center to center length", Type:=1)
end1 = Application.InputBox("Enter End1 Connection (none, Connector, Union, 90deg, or Tee)", Type:=2)
If end1 = Range("A1") Then
CountRedux = True
Else
CountRedux = False
End If
end2 = Application.InputBox("Enter End2 Connection (none, Connector, Union, 90deg, or Tee)", Type:=2)
Range("A1") = end2
Range("B2") = desLength
If end1 = "Connector" Then
CS_Con_ct = CS_Con_ct + 1
If CountRedux = False Then CS_Con_ct_tot = CS_Con_ct_tot + 1
desLength = desLength - CS_Con + Threadin
End If
If end1 = "Union" Then
CS_Un_ct = CS_Un_ct + 1
If CountRedux = False Then CS_Un_ct_tot = CS_Un_ct_tot + 1
desLength = desLength - CS_Un + Threadin
End If
If end1 = "90deg" Then
CS_90deg_ct = CS_90deg_ct + 1
If CountRedux = False Then CS_90deg_ct_tot = CS_90deg_ct_tot + 1
desLength = desLength - CS_90deg + Threadin
End If
If end1 = "Tee" Then
CS_Tee_ct = CS_Tee_ct + 1
If CountRedux = False Then CS_Tee_ct_tot = CS_Tee_ct_tot + 1
desLength = desLength - CS_Tee + Threadin
End If
If end2 = "Connector" Then
CS_Con_ct = CS_Con_ct + 1
CS_Con_ct_tot = CS_Con_ct_tot + 1
desLength = desLength - CS_Con + Threadin
End If
If end2 = "Union" Then
CS_Un_ct = CS_Un_ct + 1
CS_Un_ct_tot = CS_Un_ct_tot + 1
desLength = desLength - CS_Un + Threadin
End If
If end2 = "90deg" Then
CS_90deg_ct = CS_90deg_ct + 1
CS_90deg_ct_tot = CS_90deg_ct_tot + 1
desLength = desLength - CS_90deg + Threadin
End If
If end2 = "Tee" Then
CS_Tee_ct = CS_Tee_ct + 1
CS_Tee_ct_tot = CS_Tee_ct_tot + 1
desLength = desLength - CS_Tee + Threadin
End If
'While desLength >= A_pipe
' A_ct = A_ct + 1
' segCount = segCount + 1
' desLength = desLength - A_pipe
' If segCount > 2 Then
' desLength = desLength + CS_Con - Threadin - Threadin
' End If
'Wend
While desLength >= B_pipe
B_ct = B_ct + 1
segCount = segCount + 1
desLength = desLength - B_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= C_pipe
C_ct = C_ct + 1
segCount = segCount + 1
desLength = desLength - C_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= D_pipe
D_ct = D_ct + 1
segCount = segCount + 1
desLength = desLength - D_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= E_pipe
E_ct = E_ct + 1
segCount = segCount + 1
desLength = desLength - E_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= F_pipe
F_ct = F_ct + 1
segCount = segCount + 1
desLength = desLength - F_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= G_pipe
G_ct = G_ct + 1
segCount = segCount + 1
desLength = desLength - G_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= H_pipe
H_ct = H_ct + 1
segCount = segCount + 1
desLength = desLength - H_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= I_pipe
I_ct = I_ct + 1
segCount = segCount + 1
desLength = desLength - I_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= J_pipe
J_ct = J_ct + 1
segCount = segCount + 1
desLength = desLength - J_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= K_pipe
K_ct = K_ct + 1
segCount = segCount + 1
desLength = desLength - K_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= L_pipe
L_ct = L_ct + 1
segCount = segCount + 1
desLength = desLength - L_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= M_pipe
M_ct = M_ct + 1
segCount = segCount + 1
desLength = desLength - M_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= N_pipe
N_ct = N_ct + 1
segCount = segCount + 1
desLength = desLength - N_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= O_pipe
O_ct = O_ct + 1
segCount = segCount + 1
desLength = desLength - O_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= P_pipe
P_ct = P_ct + 1
segCount = segCount + 1
desLength = desLength - P_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= Q_pipe
Q_ct = Q_ct + 1
segCount = segCount + 1
desLength = desLength - Q_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= R_pipe
R_ct = R_ct + 1
segCount = segCount + 1
desLength = desLength - R_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= S_pipe
S_ct = S_ct + 1
segCount = segCount + 1
desLength = desLength - S_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= T_pipe
T_ct = T_ct + 1
segCount = segCount + 1
desLength = desLength - T_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= U_pipe
U_ct = U_ct + 1
segCount = segCount + 1
desLength = desLength - U_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= V_pipe
V_ct = V_ct + 1
segCount = segCount + 1
desLength = desLength - V_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= W_pipe
W_ct = W_ct + 1
segCount = segCount + 1
desLength = desLength - W_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= X_pipe
X_ct = X_ct + 1
segCount = segCount + 1
desLength = desLength - X_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= Y_pipe
Y_ct = Y_ct + 1
segCount = segCount + 1
desLength = desLength - Y_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength >= Z_pipe
Z_ct = Z_ct + 1
segCount = segCount + 1
desLength = desLength - Z_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
While desLength > 0
FULLY_ct = FULLY_ct + 1
segCount = segCount + 1
desLength = desLength - FULLY_pipe
If segCount >= 2 Then
desLength = desLength - CS_Con + Threadin + Threadin
End If
Wend
CS_Con_ct_p = segCount - 1
CS_Con_ct_tot = CS_Con_ct_tot + CS_Con_ct_p
A_ct_tot = A_ct + A_ct_tot
B_ct_tot = B_ct + B_ct_tot
C_ct_tot = C_ct + C_ct_tot
D_ct_tot = D_ct + D_ct_tot
E_ct_tot = E_ct + E_ct_tot
F_ct_tot = F_ct + F_ct_tot
G_ct_tot = G_ct + G_ct_tot
H_ct_tot = H_ct + H_ct_tot
I_ct_tot = I_ct + I_ct_tot
J_ct_tot = J_ct + J_ct_tot
K_ct_tot = K_ct + K_ct_tot
L_ct_tot = L_ct + L_ct_tot
M_ct_tot = M_ct + M_ct_tot
N_ct_tot = N_ct + N_ct_tot
O_ct_tot = O_ct + O_ct_tot
P_ct_tot = P_ct + P_ct_tot
Q_ct_tot = Q_ct + Q_ct_tot
R_ct_tot = R_ct + R_ct_tot
S_ct_tot = S_ct + S_ct_tot
T_ct_tot = T_ct + T_ct_tot
U_ct_tot = U_ct + U_ct_tot
V_ct_tot = V_ct + V_ct_tot
W_ct_tot = W_ct + W_ct_tot
X_ct_tot = X_ct + X_ct_tot
Y_ct_tot = Y_ct + Y_ct_tot
Z_ct_tot = Z_ct + Z_ct_tot
FULLY_ct_tot = FULLY_ct + FULLY_ct_tot
Range("C3") = CS_Con_ct
Range("C4") = CS_Un_ct
Range("C5") = CS_90deg_ct
Range("C6") = CS_Tee_ct
Range("C7") = A_ct
Range("C8") = B_ct
Range("C9") = C_ct
Range("C10") = D_ct
Range("C11") = E_ct
Range("C12") = F_ct
Range("C13") = G_ct
Range("C14") = H_ct
Range("C15") = I_ct
Range("C16") = J_ct
Range("C17") = K_ct
Range("C18") = L_ct
Range("C19") = M_ct
Range("C20") = N_ct
Range("C21") = O_ct
Range("C22") = P_ct
Range("C23") = Q_ct
Range("C24") = R_ct
Range("C25") = S_ct
Range("C26") = T_ct
Range("C27") = U_ct
Range("C28") = V_ct
Range("C29") = W_ct
Range("C30") = X_ct
Range("C31") = Y_ct
Range("C32") = Z_ct
Range("C33") = FULLY_ct
Range("D3") = CS_Con_ct_tot
Range("D4") = CS_Un_ct_tot
Range("D5") = CS_90deg_ct_tot
Range("D6") = CS_Tee_ct_tot
Range("D7") = A_ct_tot
Range("D8") = B_ct_tot
Range("D9") = C_ct_tot
Range("D10") = D_ct_tot
Range("D11") = E_ct_tot
Range("D12") = F_ct_tot
Range("D13") = G_ct_tot
Range("D14") = H_ct_tot
Range("D15") = I_ct_tot
Range("D16") = J_ct_tot
Range("D17") = K_ct_tot
Range("D18") = L_ct_tot
Range("D19") = M_ct_tot
Range("D20") = N_ct_tot
Range("D21") = O_ct_tot
Range("D22") = P_ct_tot
Range("D23") = Q_ct_tot
Range("D24") = R_ct_tot
Range("D25") = S_ct_tot
Range("D26") = T_ct_tot
Range("D27") = U_ct_tot
Range("D28") = V_ct_tot
Range("D29") = W_ct_tot
Range("D30") = X_ct_tot
Range("D31") = Y_ct_tot
Range("D32") = Z_ct_tot
Range("D33") = FULLY_ct_tot
continue = MsgBox("Do you have another segment?", vbQuestion + vbYesNo)
Wend
Call PresentThreadedCalc
End Sub
代碼總是使用最長的管段可能並循環向下,看看哪些是適合最長的部分。
如果沒有使用整個管道,但仍然有長度,它使用「完全螺紋」部分,完成了長度。
你能解釋一下關於它如何工作?最終產出是什麼,以及每個階段的結果如何? – Graham
請參閱編輯。程序填寫此表格,告訴用戶哪些管道長度和部件將滿足所需的測量。 – tannman357
程序如何處理同一長度的多個解決方案?例如:如果我需要80「管道,那麼它可能是1×72」+ 1×8「管道或4×20」管道或1×60「+ 1×20」管道...... –