Excel VBA将右列与左列相加

时间:2017-04-08 15:57:08

标签: vba excel-vba excel

在下面查看我的数据的屏幕截图:

enter image description here

代码的屏幕截图(直到PO按照假设提交):

enter image description here

1 个答案:

答案 0 :(得分:0)

这不是最优雅的代码,但它适合您。我完全改变了你用来制作ColSumTraining3 sub的逻辑。将宏指定给按钮,选择所需的行并单击按钮。 CumWip的值将根据Area自动填充。

您可以调整此代码以便稍后执行所有行。

Sub ColSumTraining4()

Dim SawCol As Integer
Dim BakeCol As Integer
Dim CNCCol As Integer
Dim GrindCol As Integer
Dim NC2Col As Integer
Dim NC3Col As Integer
Dim NC4Col As Integer
Dim NC5Col As Integer
Dim CumWipCol As Integer
Dim AreaCol As Integer

SawCol = 2
BakeCol = 3
CNCCol = 4
GrindCol = 5
NC2Col = 6
NC3Col = 7
NC4Col = 8
NC5Col = 9
CumWipCol = 12
AreaCol = 13

Dim AreaVal As String
Dim LastCol As Integer

AreaVal = UCase(Sheets(1).Cells(Selection.Row, AreaCol).Value)

Select Case AreaVal
    Case "NC5"
        LastCol = NC5Col
    Case "NC4"
        LastCol = NC4Col
    Case "NC3"
        LastCol = NC3Col
    Case "NC2"
        LastCol = NC2Col
    Case "GRIND"
        LastCol = GrindCol
    Case "CNC"
        LastCol = CNCCol
    Case "BAKE"
        LastCol = BakeCol
    Case "SAW"
        LastCol = GrindCol
    Case Else
        MsgBox "Please select a valid row"
        Exit Sub
End Select

Dim CumWipSum As Integer
Dim TotalCumWipSum As Integer
TotalCumWipSum = 0

For i = NC5Col To LastCol Step -1
    TotalCumWipSum = TotalCumWipSum + Sheets(1).Cells(Selection.Row, i).Value
Next i

Dim CumWip As Range
Set CumWip = Sheets(1).Cells(Selection.Row, CumWipCol)

CumWip.Value = TotalCumWipSum

End Sub