1

I'm trying to write a macro for a hydro-logic model worth a decent % of my grade, and I keep getting the 1004 error. It's centered on the first line of the first If statement.

This is the code that I'm using:

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

Help? I'm not super familiar with any programming, this was a rather unfortunate project sprung on us by the prof.

Community
  • 1
  • 1

1 Answers1

2

That is because the value of Stage is 0 in

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

Excel rows start with 1 and can go up to a max of 65536 in xl2003 and previous or to 1048576 in xl2007+

Also it is advisable to declare the row variable(s) as Long instead of Integer

You might also want to see THIS to make your code more robust.

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • But why would it be 0 there? Am I not selecting columns to work with? – user3000426 Nov 16 '13 at 22:22
  • You have to set a value of `stage` as it is as variable. If you step through the code using F8, you will notice that the value of `stage` is `0`. Or you can simply type this before that `IF` line. `MSGBOX Stage` – Siddharth Rout Nov 16 '13 at 22:24
  • ahhhh, now I see. I somehow missed an equation in there. Thanks!! – user3000426 Nov 16 '13 at 22:28
  • @user3000426, since this answered your question, feel free to accept it by clicking the checkmark next to it. That way future readers will know it's the answer (and Siddharth will have even more points :) http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Doug Glancy Nov 16 '13 at 23:49