得到1004错误

时间:2013-11-16 22:05:10

标签: excel excel-vba vba

我正在尝试为一个水平逻辑模型编写一个宏,这个模型的价值相当于我的等级,并且我不断收到1004错误。它集中在第一个If语句的第一行。

这是我正在使用的代码:

Sub pheonix()
'
' pheonix Macro
'
' Keyboard Shortcut: Ctrl+u
'
    Dim WS As Worksheet
    Set WS = Sheets.Add
    Sheets.Add.Name = "RESULTS"
    Sheets("Case 2").Select

    Range("C1:R1").Select
    Selection.Copy
    Sheets("RESULTS").Select

    ActiveSheet.Paste


    Dim Row As Integer
    Dim Day As Integer
    Dim SurfaceInflow As Integer
    Dim GroundwaterOutflow As Integer
    Dim SurfaceOutflow As Integer
    Dim Stage As Integer
    Dim Evap As Integer
    Dim Precip As Integer
    Dim AreaL As Integer
    Dim ChangeStorage As Integer
    Dim Storage As Integer

    Dim InitialStorage As Integer
    Dim InitialStage As Integer
    Dim InitialArea As Integer



    Row = 2

    'calulation for initial day
    'InitialStage = "4"
    'InitialArea = 11 * InitialStage ^ 0.5
    'InitialStorage = (22 / 3) * (InitialStage ^ (3 / 2))

    Set wksSource = ActiveWorkbook.Sheets("Case 2")
    Set wksDest = ActiveWorkbook.Sheets("RESULTS")

     For Day = 5 To 734
        Sheets("RESULTS").Cells(Row, "A") = Sheets("Case 2").Cells(Row, "C")


        For i = 0 To 288
             Sheets("Case 2").Select

                SurfaceInflow = ((0.2 * Cells(Day, "G")) * ((1 - 0.4) * (557 - Cells(Day, "J")))) + ((0.95 * Cells(Day, "G")) * (0.4 * (557 - Cells(Day, "J"))))

                If Cells(Stage, "H") >= 1.348 Then
                    GroundwaterOutflow = (0.379 * Cells(Stage, "H") - 0.511)
                    Else: GroundwaterOutflow = 0
                End If

                If Cells(Stage, "H") > 2.9 Then
                    SurfaceInflow = (33 * (Cells(Stage, "H") - 2.9) ^ (3 / 2))
                    Else: SurfaceInflow = 0
                End If

                Precip = Cells(Precip, "S")

                Evap = Cells(Evap, "T")

                Area = (11 * Cells(Stage, "H") ^ 0.5)

                ChangeStorage = (Area * (Precip - Evap)) - GroundwaterOutflow + SurfaceInflow - SurfaceOutflow

                Storage = Storage + ChangeStorage


        Next i
            Sheets("RESULTS").Cells(Row, "B") = "X"
            Row = Row + 1

     Next Day

End Sub

帮助?我对任何编程都不是很熟悉,这是一个相当不幸的项目,由教授在我们身上出现。

1 个答案:

答案 0 :(得分:2)

这是因为Stage的值为0

If Cells(Stage, "H") >= 1.348 Then

Excel行以1开头,xl2003及之前最高为65536,xl2007 +为1048576

同样建议将行变量声明为Long而不是Integer

您可能还希望看到THIS以使您的代码更加健壮。