接下来没有For错误。不知道为什么

时间:2016-08-15 09:42:48

标签: excel vba

我正在Excel中编写我的第一个VBA代码。 基本上我正在创建一个工具,可以在工作中轻松编译报告。目前,我一直在寻找某些日期之间发生的变化,因为我不断获得#34;下一步没有错误"。

main.lua:30 function or expression too complex near <eof>

任何帮助都会非常感激,因为我现在迷路了! 任何其他有关代码格式或一般原则的帮助也会受到赞赏,因为这是我第一次使用VBA

1 个答案:

答案 0 :(得分:0)

您收到该错误的原因是您需要使用With关闭End With,就像每个For必须拥有Next且每If一样必须有End If

Sub RetrieveVenues()

Dim rosterFilePath As String
rosterFilePath = "F:/VBA/on&off prem.xlsx"

Dim shiftDone As Integer
shiftDone = 2

Dim masterFile As Workbook, rosterFile As Workbook
Set masterFile = ActiveWorkbook

Dim lowDate As Date, highDate As Date


'RETRIEVE DATE RANGE FROM REPORTS

'set initial high and low
lowDate = Cells(2, 4)
highDate = Cells(2, 4)

'get row amount
With ActiveSheet
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

'get high and low date
For i = 2 To lastRow
    If Cells(i, 4) < lowDate Then
        lowDate = Cells(i, 4)
    ElseIf Cells(i, 4) > highDate Then
        highDate = Cells(i, 4)
    End If
Next i

'SORT TO RELEVANT SHIFTS

'open workbook
Workbooks.Open rosterFilePath
Set rosterFile = ActiveWorkbook

For j = 1 To 5

    Sheets(i).Activate

    With ActiveSheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    For Row = 2 To lastRow

        'Sort Lines
        'Sort on/off premesis
        If rosterFile.Cells(Row, 1).Value = "On Prem" Then

            'retrieve date
            Dim shiftDate As Date

            shiftDate = rosterFile.Cells(Row, 12).Value

            'check shift date
            If shiftDate <= highDate And shiftDate >= lowDate Then

                'CORRECT SHIFT - GET DATA FOR REPORT
                'display - name,state,date,time

                masterFile.Sheets(3).Cells(shiftDone, 1) = rosterFile.Cells(Row, 5).Value
                masterFile.Sheets(3).Cells(shiftDone, 2) = rosterFile.Cells(Row, 10).Value
                masterFile.Sheets(3).Cells(shiftDone, 3) = rosterFile.Cells(Row, 12).Value
                masterFile.Sheets(3).Cells(shiftDone, 4) = rosterFile.Cells(Row, 13).Value

                shiftDone = shiftDone + 1

            End If


        End If


    Next Row


Next j


End Sub