将variables添加到另一个variablesvba

Dim x As Long Dim y As Long Dim CDTotal As Double Dim CSTotal As Double Dim ETotal As Double Dim FTotal As Double Dim HTotal As Double Dim ITotal As Double Dim ITTotal As Double Dim MTotal As Double Dim TTotal As Double Dim UTotal As Double Dim TotalValue As Double For y = 3 To 3 For x = 600 To 1 Step -1 If Cells(x, y).Value = "CD Sector Average" Then CDTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "CS Sector Average" Then CSTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "E Sector Average" Then ETotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "F Sector Average" Then FTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "H Sector Average" Then HTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "I Sector Average" Then ITotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "IT Sector Average" Then ITTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "M Sector Average" Then MTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "T Sector Average" Then TTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "U Sector Average" Then UTotal = Cells(x, y + 5).Value End If If Cells(x, y).Value = "Total Portfolio" Then TotalValue = UTotal + TTotal + MTotal + ITTotal + ITotal + HTotal + FTotal + ETotal + CSTotal + CDTotal Cells(x, y + 5) = TotalValue End If Next x Next y 

这些名称右侧的单元格5列中有值。 而且我需要把它们加起来,而不在行之间添加任何数字。

我写的最后一部分是错的? 它出来为0,但在参考的单元格中有清楚的数据。

只要你的文本在列C中 ,并且值在列H中 ,我看不出代码有什么问题

我也冒着重写代码的自由,使之更加清晰:

 Sub test() Dim x As Long Dim y As Long Dim TotalValue As Double TotalValue = 0 y = 3 For x = 600 To 1 Step -1 Select Case Cells(x, y).Value Case "CD Sector Average", "CS Sector Average", _ "E Sector Average", "F Sector Average", _ "H Sector Average", "I Sector Average", _ "IT Sector Average", "M Sector Average", _ "T Sector Average", "U Sector Average" TotalValue = TotalValue + Cells(x, y + 5).Value Case "Total Portfolio" Cells(x, y + 5).Value = TotalValue TotalValue = 0 End Select Next x End Sub