第d =工作表(A(i))处的错误1004。单元格(B(j),l)+ d

时间:2014-03-11 00:05:47

标签: excel vba excel-vba

我在这个命令中遇到错误

d = Worksheets(A(i)).Cells(B(j), l) + d

我在数组A中存储了工作表的名称(i)数组B(j)具有整数值。我已经将两个数组一起声明为d整数。

提前致谢,

请找到以下代码

Sub checksum()
    Dim A(50) As String
    Dim B(5) As Integer
    Dim i As Integer, j As Integer, d As Integer, k As Integer, p As Integer, l As Integer, s As Integer
    s = 1
    A(1) = "TREND M S&G"
    A(2) = "TREND M RAZORS"
    A(3) = "TREND M RZ ACC"
    A(4) = "TREND M GROOM"
    A(5) = "TREND BODY GROOM"
    A(6) = "TREND Multi"
    A(7) = "TREND BRDM"
    A(8) = "TREND PRCSN"
    A(9) = "TREND M H CLIP"
    A(10) = "TREND PTB&A"
    A(11) = "TREND rch"
    A(12) = "TREND batt"
    A(13) = "TREND refills"
    A(14) = "TREND BABY"
    A(15) = "TREND BREAST FEED"
    A(16) = "TREND breast pad"
    A(17) = "TREND breast pumps"
    A(18) = "TREND REUSABLE"
    A(19) = "TREND DISPOSABLE"
    A(20) = "TREND TODDLER"
    A(21) = "TREND TODDLER C&P"
    A(22) = "TREND FEED ACCESS"
    A(23) = "TREND SOOTHING"
    A(24) = "TREND P&H"
    A(25) = "TREND TEETHERS"
    B(1) = 3
    B(2) = 6
    B(3) = 9
    B(4) = 12

    ThisWorkbook.Sheets.Add After:=Sheets(Worksheets.Count), Count:=1, Type:=xlWorksheet

    For i = 1 To 25
         Worksheets(Worksheets.Count).Cells(i, 1) = A(i)

         For j = 1 To 4
             d = 0

             If i > 10 Then k = 54 And p = 70
             If i < 11 Then k = 56 And p = 66

             For l = k To p
                 d = Worksheets(A(i)).Cells(B(j), l) + d
             Next l

             If d = 100 Then Worksheets(Worksheets.Count).Cells(i, j + 1) = "Fine"
             If d <> 100 Then Worksheets(Worksheets.Count).Cells(i, j + 1) = "Error"

         Next j
    Next i
End Sub

2 个答案:

答案 0 :(得分:2)

这不起作用; And Operator 不能以这种方式使用:

If i > 10 Then k = 54 And p = 70
If i < 11 Then k = 56 And p = 66

将其更改为:

If i > 10 Then
    k = 54
    p = 70
Else
    k = 56
    p = 66
End If

答案 1 :(得分:0)

我不知道你引用的单元格中有什么,但根据我在这里看到的内容,我猜它包含一个整数。如果是这样,那么您需要访问单元格的值:

d = Worksheets(A(i)).Cells(B(j), l).Value2 + d

你需要对你的最后几行做同样的事情

If d = 100 Then Worksheets(Worksheets.Count).Cells(i, j + 1).Value2 = "Fine"
If d <> 100 Then Worksheets(Worksheets.Count).Cells(i, j + 1).Value2 = "Error"