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
你需要用'End With'關閉你的'With ...'語句 –
你關閉了你的'With'語句還是隻關閉了一個? –