1

我知道代码可能不是很好,但我仍在学习 VBA。

我创建了一个工作表来组织我的大学课程列表,但我试图让它在我更改前两列中的值时自动执行代码。

第一列中的“x”将其选择为已完成的课程,从所需的总小时数中减去该课程的学分。第二列中的“S”和一些数字表示一个学期,并总结了每学期要修读的学分,例如,如果我在“工程学习和职业”旁边输入“S1”,那么它将占用该课程的 1 个学分,并将其添加到整个学期的总学时中。

这两个代码完全相互独立。

工作表截图:http: //i.imgur.com/G850X.png

代码:

Private Sub Calculations()

    Dim creditsLeft As Integer creditsLeft = 130

    For i = 3 To 43
        If Cells(i, 1).Value = "x" Then creditsLeft = (creditsLeft - Cells(i, 8)) Next i

    Range("J3").Value = creditsLeft


    Dim S1creds, S2creds, S3creds, S4creds, S5creds, S6creds As Integer

    For i = 3 To 43
        If Cells(i, 2).Value = "S1" Then S1creds = (S1creds + Cells(i, 8))
        If Cells(i, 2).Value = "S2" Then S2creds = (S2creds + Cells(i, 8))
        If Cells(i, 2).Value = "S3" Then S3creds = (S3creds + Cells(i, 8))
        If Cells(i, 2).Value = "S4" Then S4creds = (S4creds + Cells(i, 8))
        If Cells(i, 2).Value = "S5" Then S5creds = (S5creds + Cells(i, 8))
        If Cells(i, 2).Value = "S6" Then S6creds = (S6creds + Cells(i, 8)) 
    Next i

    Range("J9").Value = "Sem 1 Hrs: " 
    Range("J10").Value = "Sem 2 Hrs: "
    Range("J11").Value = "Sem 3 Hrs: " 
    Range("J12").Value = "Sem 4 Hrs: "
    Range("J13").Value = "Sem 5 Hrs: " 
    Range("J14").Value = "Sem 6 Hrs: "

    Range("K9").Value = S1creds 
    Range("K10").Value = S2creds
    Range("K11").Value = S3creds 
    Range("K12").Value = S4creds
    Range("K13").Value = S5creds 
    Range("K14").Value = S6creds

    End Sub
4

1 回答 1

3

你甚至不需要 VBA。

I3:=130-SUMIF(A3:A43,"x",H3:H43)
K9:=SUMIF(B3:B43,"S1",H3:H43)
K10:=SUMIF(B3:B43,"S2",H3:H43)
K11:=SUMIF(B3:B43,"S3",H3:H43)
K12:=SUMIF(B3:B43,"S4",H3:H43)
K13:=SUMIF(B3:B43,"S5",H3:H43)
K14:=SUMIF(B3:B43,"S6",H3:H43)

于 2012-11-29T14:41:52.990 回答