2016-08-15 72 views
0

我正在Excel中编写我的第一个VBA代码。 基本上,我正在创建一个工具,以便在工作中轻松地为我编制报告。目前,我一直在发现在某些日期之间发生的变化,因为我不断收到“未发生错误”。下一个没有错误。不知道为什么

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 

'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 

    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 

任何帮助将非常感谢,因为我目前失去了! 任何其他帮助,代码格式化或一般原理也可以理解,因为这是我第一次用VBA

+11

你需要用'End With'关闭你的'With ...'语句 –

+0

你关闭了你的'With'语句还是只关闭了一个? –

回答

0

你得到这个错误的原因是,你需要有一个End With关闭With,就像所有For必须有一个NextIf必须有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 
相关问题