Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Function chooseFile()
- Dim file As FileDialog
- Set file = Application.FileDialog(msoFileDialogFilePicker)
- With file
- .Title = "Select a File"
- .AllowMultiSelect = False
- .InitialFileName = strPath
- If .Show = True Then
- chooseFile = .SelectedItems(1)
- End If
- End With
- End Function
- Public Sub fileAddress()
- ThisWorkbook.Worksheets(4).Range("K1") = chooseFile()
- End Sub
- Public Sub Auto_Open()
- autoUpdate
- End Sub
- Public Sub autoUpdate()
- Dim StartTime As Double
- Dim SecondsElapsed As Double
- StartTime = Timer
- Dim prevMonth As Workbook
- Dim prevMonthPath As String
- Dim lastDate, repDate, firstDate As Date
- Dim firstIndex, lastIndex, lastDay As Integer
- Dim rng1, rng2 As Range
- Application.ScreenUpdating = False
- Application.AskToUpdateLinks = False
- Application.DisplayAlerts = False
- lastDate = Date
- repDate = ThisWorkbook.Worksheets(1).Range("B1")
- If Year(repDate) <> Year(lastDate) Or Month(repDate) <> Month(lastDate) Then
- ThisWorkbook.Worksheets(4).Range("C2:C31").ClearContents
- Application.ScreenUpdating = True
- Application.AskToUpdateLinks = True
- Application.DisplayAlerts = True
- Exit Sub
- End If
- firstDate = Date - 29
- firstIndex = Day(firstDate)
- lastIndex = Day(lastDate)
- lastDay = Day(Application.WorksheetFunction.EoMonth(firstDate, 0))
- If Month(firstDate) = Month(lastDate) Then
- With ThisWorkbook.Worksheets(1)
- Set rng1 = .Range(.Cells(4 + firstIndex, 3), .Cells(4 + lastIndex, 3))
- End With
- With ThisWorkbook.Worksheets(4)
- .Range("C2:C31").Value = rng1.Value
- End With
- Else
- prevMonthPath = ThisWorkbook.Worksheets(4).Range("K1")
- On Error Resume Next
- Set prevMonth = Workbooks.Open(prevMonthPath)
- If prevMonth Is Nothing Then
- ThisWorkbook.Worksheets(4).Range("C2:C31").ClearContents
- Application.ScreenUpdating = True
- Application.AskToUpdateLinks = True
- Application.DisplayAlerts = True
- Exit Sub
- End If
- With prevMonth.Worksheets(1)
- ThisWorkbook.Worksheets(4).Range("O2:O32").Value = .Range("C5:C35").Value
- Set rng1 = .Range(.Cells(4 + firstIndex, 3), .Cells(4 + lastDay, 3))
- End With
- With ThisWorkbook.Worksheets(1)
- Set rng2 = .Range(.Cells(5, 3), .Cells(4 + lastIndex, 3))
- End With
- With ThisWorkbook.Worksheets(4)
- .Range(.Cells(2, 3), .Cells(lastDay - firstIndex + 2, 3)).Value = rng1.Value
- .Range(.Cells(lastDay - firstIndex + 3, 3), .Cells(31, 3)).Value = rng2.Value
- End With
- prevMonth.Close
- End If
- Application.ScreenUpdating = True
- Application.AskToUpdateLinks = True
- Application.DisplayAlerts = True
- SecondsElapsed = Round(Timer - StartTime, 2)
- MsgBox "Skoroszyt zaktualizowany w czasie: " & SecondsElapsed & "s", vbInformation
- End Sub
- Public Sub autoCalculate()
- If IsEmpty(ThisWorkbook.Worksheets(4).Range("K1")) Then
- Exit Sub
- End If
- Dim lastDate, repDate, firstDate As Date
- Dim firstIndex, lastIndex, lastDay As Integer
- Dim rng1, rng2 As Range
- Application.ScreenUpdating = False
- Application.AskToUpdateLinks = False
- Application.DisplayAlerts = False
- lastDate = Date
- repDate = ThisWorkbook.Worksheets(1).Range("B1")
- If Year(repDate) <> Year(lastDate) Or Month(repDate) <> Month(lastDate) Then
- ThisWorkbook.Worksheets(4).Range("C2:C31").ClearContents
- Application.ScreenUpdating = True
- Application.AskToUpdateLinks = True
- Application.DisplayAlerts = True
- Exit Sub
- End If
- firstDate = Date - 29
- firstIndex = Day(firstDate)
- lastIndex = Day(lastDate)
- lastDay = Day(Application.WorksheetFunction.EoMonth(firstDate, 0))
- If Month(firstDate) = Month(lastDate) Then
- With ThisWorkbook.Worksheets(1)
- Set rng1 = .Range(.Cells(4 + firstIndex, 3), .Cells(4 + lastIndex, 3))
- End With
- With ThisWorkbook.Worksheets(4)
- .Range("C2:C31").Value = rng1.Value
- End With
- Else
- With ThisWorkbook.Worksheets(1)
- Set rng2 = .Range(.Cells(5, 3), .Cells(4 + lastIndex, 3))
- End With
- With ThisWorkbook.Worksheets(4)
- Set rng1 = .Range(.Cells(1 + firstIndex, 15), .Cells(1 + lastDay, 15))
- .Range(.Cells(2, 3), .Cells(lastDay - firstIndex + 2, 3)).Value = rng1.Value
- .Range(.Cells(lastDay - firstIndex + 3, 3), .Cells(31, 3)).Value = rng2.Value
- End With
- End If
- Application.ScreenUpdating = True
- Application.AskToUpdateLinks = True
- Application.DisplayAlerts = True
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement