Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Function chooseFile()
  2. Dim file As FileDialog
  3.  
  4. Set file = Application.FileDialog(msoFileDialogFilePicker)
  5. With file
  6.     .Title = "Select a File"
  7.     .AllowMultiSelect = False
  8.     .InitialFileName = strPath
  9.     If .Show = True Then
  10.         chooseFile = .SelectedItems(1)
  11.     End If
  12. End With
  13.  
  14. End Function
  15.  
  16. Public Sub fileAddress()
  17. ThisWorkbook.Worksheets(4).Range("K1") = chooseFile()
  18. End Sub
  19.  
  20. Public Sub Auto_Open()
  21.  
  22. autoUpdate
  23.  
  24. End Sub
  25.  
  26. Public Sub autoUpdate()
  27. Dim StartTime As Double
  28. Dim SecondsElapsed As Double
  29.  
  30. StartTime = Timer
  31.  
  32. Dim prevMonth As Workbook
  33. Dim prevMonthPath As String
  34. Dim lastDate, repDate, firstDate As Date
  35. Dim firstIndex, lastIndex, lastDay As Integer
  36. Dim rng1, rng2 As Range
  37.  
  38. Application.ScreenUpdating = False
  39. Application.AskToUpdateLinks = False
  40. Application.DisplayAlerts = False
  41.  
  42. lastDate = Date
  43. repDate = ThisWorkbook.Worksheets(1).Range("B1")
  44.  
  45. If Year(repDate) <> Year(lastDate) Or Month(repDate) <> Month(lastDate) Then
  46.     ThisWorkbook.Worksheets(4).Range("C2:C31").ClearContents
  47.     Application.ScreenUpdating = True
  48.     Application.AskToUpdateLinks = True
  49.     Application.DisplayAlerts = True
  50.     Exit Sub
  51. End If
  52.  
  53. firstDate = Date - 29
  54. firstIndex = Day(firstDate)
  55. lastIndex = Day(lastDate)
  56. lastDay = Day(Application.WorksheetFunction.EoMonth(firstDate, 0))
  57.  
  58. If Month(firstDate) = Month(lastDate) Then
  59.     With ThisWorkbook.Worksheets(1)
  60.         Set rng1 = .Range(.Cells(4 + firstIndex, 3), .Cells(4 + lastIndex, 3))
  61.     End With
  62.    
  63.     With ThisWorkbook.Worksheets(4)
  64.         .Range("C2:C31").Value = rng1.Value
  65.     End With
  66. Else
  67.     prevMonthPath = ThisWorkbook.Worksheets(4).Range("K1")
  68.    
  69.     On Error Resume Next
  70.         Set prevMonth = Workbooks.Open(prevMonthPath)
  71.    
  72.     If prevMonth Is Nothing Then
  73.         ThisWorkbook.Worksheets(4).Range("C2:C31").ClearContents
  74.         Application.ScreenUpdating = True
  75.         Application.AskToUpdateLinks = True
  76.         Application.DisplayAlerts = True
  77.         Exit Sub
  78.     End If
  79.    
  80.     With prevMonth.Worksheets(1)
  81.         ThisWorkbook.Worksheets(4).Range("O2:O32").Value = .Range("C5:C35").Value
  82.         Set rng1 = .Range(.Cells(4 + firstIndex, 3), .Cells(4 + lastDay, 3))
  83.     End With
  84.    
  85.     With ThisWorkbook.Worksheets(1)
  86.         Set rng2 = .Range(.Cells(5, 3), .Cells(4 + lastIndex, 3))
  87.     End With
  88.    
  89.     With ThisWorkbook.Worksheets(4)
  90.         .Range(.Cells(2, 3), .Cells(lastDay - firstIndex + 2, 3)).Value = rng1.Value
  91.         .Range(.Cells(lastDay - firstIndex + 3, 3), .Cells(31, 3)).Value = rng2.Value
  92.     End With
  93.    
  94.     prevMonth.Close
  95. End If
  96.  
  97. Application.ScreenUpdating = True
  98. Application.AskToUpdateLinks = True
  99. Application.DisplayAlerts = True
  100.  
  101. SecondsElapsed = Round(Timer - StartTime, 2)
  102. MsgBox "Skoroszyt zaktualizowany w czasie: " & SecondsElapsed & "s", vbInformation
  103. End Sub
  104.  
  105. Public Sub autoCalculate()
  106.  
  107. If IsEmpty(ThisWorkbook.Worksheets(4).Range("K1")) Then
  108.     Exit Sub
  109. End If
  110.  
  111. Dim lastDate, repDate, firstDate As Date
  112. Dim firstIndex, lastIndex, lastDay As Integer
  113. Dim rng1, rng2 As Range
  114.  
  115. Application.ScreenUpdating = False
  116. Application.AskToUpdateLinks = False
  117. Application.DisplayAlerts = False
  118.  
  119. lastDate = Date
  120. repDate = ThisWorkbook.Worksheets(1).Range("B1")
  121.  
  122. If Year(repDate) <> Year(lastDate) Or Month(repDate) <> Month(lastDate) Then
  123.     ThisWorkbook.Worksheets(4).Range("C2:C31").ClearContents
  124.     Application.ScreenUpdating = True
  125.     Application.AskToUpdateLinks = True
  126.     Application.DisplayAlerts = True
  127.     Exit Sub
  128. End If
  129.  
  130. firstDate = Date - 29
  131. firstIndex = Day(firstDate)
  132. lastIndex = Day(lastDate)
  133. lastDay = Day(Application.WorksheetFunction.EoMonth(firstDate, 0))
  134.  
  135. If Month(firstDate) = Month(lastDate) Then
  136.     With ThisWorkbook.Worksheets(1)
  137.         Set rng1 = .Range(.Cells(4 + firstIndex, 3), .Cells(4 + lastIndex, 3))
  138.     End With
  139.    
  140.     With ThisWorkbook.Worksheets(4)
  141.         .Range("C2:C31").Value = rng1.Value
  142.     End With
  143. Else
  144.     With ThisWorkbook.Worksheets(1)
  145.         Set rng2 = .Range(.Cells(5, 3), .Cells(4 + lastIndex, 3))
  146.     End With
  147.    
  148.     With ThisWorkbook.Worksheets(4)
  149.         Set rng1 = .Range(.Cells(1 + firstIndex, 15), .Cells(1 + lastDay, 15))
  150.         .Range(.Cells(2, 3), .Cells(lastDay - firstIndex + 2, 3)).Value = rng1.Value
  151.         .Range(.Cells(lastDay - firstIndex + 3, 3), .Cells(31, 3)).Value = rng2.Value
  152.     End With
  153. End If
  154.  
  155. Application.ScreenUpdating = True
  156. Application.AskToUpdateLinks = True
  157. Application.DisplayAlerts = True
  158. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement