如果没有阻止面临结束 - 如果 - vba

时间:2016-02-25 21:49:23

标签: excel-vba vba excel

Function calculateIO(ByVal reachName As String, ByVal natFlow As Double, ByVal IOTableWorksheet As Worksheet, ByVal weeklyDate As Date) As Double

    Dim rowNoReach, rowToNextTable, columnNo, rowNo, startColumn, columnCounter, rowCounter, rowCounter1, dateCounter As Integer
    Dim vlookupRange As Range
    Dim vlookupResult As Double
    Dim currentDay, currentMonth As Integer
    Dim differenceCal As Double
    Dim ansStorage 'where to store the natural flow value from the IO table that is used to obtain the corresponding IO
    Dim IOvalue As Double

    differenceCal = 1000000
    currentDay = day(weeklyDate)
    currentMonth = month(weeklyDate)

    'Format the reach name if it is a mainstem reach name.
    If (InStr(reachName, "Mainstem") > 0) Then reachName = Trim(Split(reachName, "-")(1))

    'Initializes the row pointers
    rowNoReach = 0
    rowToNextTable = 1
    startColumn = 1

    'It is assumed that there is no IO until one is found
    calculateIO = -1


        'Loop through each IO table until there an IO table is not found
            Do While (rowToNextTable <> 0)
                rowNoReach = rowNoReach + rowToNextTable
                rowToNextTable = IOTableWorksheet.Cells(rowNoReach, 14).value

                'This will compare the reach name with the IO table name. if they are a match then an IO will be calculated using this IO table.
                If (InStr(IOTableWorksheet.Cells(rowNoReach, 2).value, reachName) > 0) Then
                    If ((currentMonth <= 3) Or (currentMonth >= 11)) Then
                        columnCounter = 1
                        For columnCounter = 1 To 21
                            If ((month(IOTableWorksheet.Cells(rowNoReach + 2, columnCounter)) = currentMonth) And (day(IOTableWorksheet.Cells(rowNoReach + 2, i)) = currentDay)) Then
                                calculateIO = IOTableWorksheet.Cells(rowNoReach + 3, columnCounter).value
                                Exit Function
                            End If
                        Next columnCounter

                    'looking through the table
                    ElseIf ((currentMonth >= 4) Or (currentMonth <= 10)) Then
                        columnCounter = 1
                        Do While IsDate(IOTableWorksheet.Cells(rowNoReach + 5, columnCounter))
                            If ((day(weeklyDate) = day(IOTableWorksheet.Cells(rowNoReach + 5, columnCounter))) And (month(weeklyDate) = month(IOTableWorksheet.Cells(rowNoReach + 5, columnCounter)))) Then
                                startColumn = columnCounter
                            End If
                            columnCounter = columnCounter + 1
                        Loop

                        If (natFlow < IOTableWorksheet.Cells(rowNoReach + 6, startColumn)) Then
                            calculateIO = natFlow
                            Exit Function
                        ElseIf (natFlow > IOTableWorksheet.Cells(rowNoReach + 6, startColumn)) Then
                            rowCounter1 = 0
                            For rowCounter1 = 0 To IOTableWorksheet.Range(IOTableWorksheet.Cells(rowNoReach + 6, startColumn), IOTableWorksheet.Range(IOTableWorksheet.Cells(rowNoReach + 6, startColumn).End(xlDown))).Rows.Count - 1
                                If (difference > (natFlow - IOTableWorksheet.Cells(rowNoReach + rowCounter1, startColumn))) Then
                                    If (natFlow - IOTableWorksheet.Cells(rowNoReach + rowCounter1, startColumn)) < 0 Then
                                        calculateIO = IOvalue
                                        Exit Function
                                    End If
                                    difference = natFlow - IOTableWorksheet.Cells(rowNoReach + rowCounter1, startColumn)
                                    IOvalue = IOTableWorksheet.Cells(rowNoReach + rowCounter1, 32)
                                End If
                        calculateIO = IOvalue
                        Exit Function
                        End If

                    End If
                ElseIf (IOTableWorksheet.Cells(rowNoReach, 2).value = "Minimum Or Established IO") Then
                    'Initialize row and column pointers
                    rowNo = rowNoReach + 3
                    columnNo = 2

                    'Calculate the row and column number
                    Do While (InStr(reachName, IOTableWorksheet.Cells(rowNoReach + 2, columnNo).value) = 0 And Not IsEmpty(IOTableWorksheet.Cells(rowNoReach + 2, columnNo).value)): columnNo = columnNo + 1: Loop
                    Do While (month(IOTableWorksheet.Cells(rowNo, 1).value) <> month(weeklyDate) Or day(IOTableWorksheet.Cells(rowNo, 1).value) <> day(weeklyDate)): rowNo = rowNo + 1: Loop

                    'Get the IO value from the table if the reach was in the table
                    If Not IsEmpty(IOTableWorksheet.Cells(rowNo, columnNo).value) Then calculateIO = IOTableWorksheet.Cells(rowNo, columnNo): Exit Function

                ElseIf (IOTableWorksheet.Cells(rowNoReach, 2).value = "Single IO Streams") Then
                    'Initialize row and column pointers
                    rowNo = rowNoReach + 3
                    columnNo = 2

                    'Calculate the column number
                    Do While InStr(reachName, IOTableWorksheet.Cells(rowNoReach + 2, columnNo).value) = 0 And Not IsEmpty(IOTableWorksheet.Cells(rowNoReach + 2, columnNo).value): columnNo = columnNo + 1: Loop

                    'Get the IO value from the table if the reach was in the table
                    If Not IsEmpty(IOTableWorksheet.Cells(rowNo, columnNo).value) Then calculateIO = IOTableWorksheet.Cells(rowNo, columnNo): Exit Function
                End If

            Loop 'looping through the first do while loop

End Function

不知道为什么代码继续有这个编译错误,我基本上通过使用相应的If-ElseIF-Else语句识别每个End If语句而没有额外的End If应该在这里。我也正确地缩进了代码。

0 个答案:

没有答案