Advertisement
sinancetinkaya

VBA Timer for P.Action

Sep 12th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.37 KB | None | 0 0
  1. 'Bu kod Modules/scheduler içine gidecek
  2.  
  3. 'Program sistem saatinin saat hanesini sira numarasi olarak kullanip bir sonraki siradaki saati zamanlar
  4. 'yani su an saat 18:00 ise 19:00'a ayarlar
  5. Public Const hours As String = "01:00:00,02:00:00,03:00:00,04:00:00,05:00:00,06:00:00,07:00:00,08:00:00,09:00:00,10:00:00,11:00:00,12:00:00,13:00:00,14:00:00,15:00:00,16:00:00,17:00:00,18:00:00,19:00:00,20:00:00,21:00:00,22:00:00,23:00:00,00:00:00"
  6.  
  7. Public Const DebugMode As Boolean = False 'Kendin kullandigin zaman False yap
  8.  
  9. Public Const ExportType = "xlsm"
  10. Public Const sayfa = "genel"
  11. Public Col As New Collection
  12.  
  13. Public Function getNextTimer()
  14.     If DebugMode Then
  15.         t = Now() + TimeSerial(0, 1, 0)
  16.     Else
  17.         array_hours = Split(hours, ",")
  18.         t = array_hours(Hour(Now()))
  19.     End If
  20.     getNextTimer = t
  21. End Function
  22.  
  23. Public Sub scheduledMacro()
  24.     t = getNextTimer()
  25.     Application.OnTime t, "scheduledMacro"
  26.    
  27.     Set Sh = ThisWorkbook.Sheets(sayfa)
  28.     output = "D:\" + Format(Now(), "yyyy-MM-dd_hh-mm-ss")
  29.    
  30.     If ExportType = "html" Then
  31.         Set PO = ThisWorkbook.PublishObjects.Add( _
  32.             SourceType:=xlSourceSheet, _
  33.             Filename:=output & "." & ExportType, _
  34.             Sheet:=Sh.Name, _
  35.             HtmlType:=xlHtmlStatic)
  36.         PO.Publish True
  37.         PO.Delete
  38.                
  39.     ElseIf ExportType = "pdf" Then
  40.         Sh.ExportAsFixedFormat _
  41.             Type:=xlTypePDF, _
  42.             Filename:=output & "." & ExportType, _
  43.             Quality:=xlQualityStandard, _
  44.             IncludeDocProperties:=True, _
  45.             IgnorePrintAreas:=False, _
  46.             OpenAfterPublish:=False
  47.    
  48.     ElseIf ExportType = "xlsm" Then
  49.         Dim wb As Workbook
  50.         Dim sh2 As Worksheet
  51.        
  52.         Sh.Copy
  53.         Set wb = ActiveWorkbook
  54.        
  55.         Set sh2 = wb.Sheets(Sh.Name)
  56.         sh2.Cells.Copy
  57.         sh2.Cells.PasteSpecial xlPasteValues
  58.        
  59.         wb.SaveAs Filename:=output & "." & ExportType, FileFormat:=xlOpenXMLWorkbookMacroEnabled
  60.         wb.Close True
  61.    
  62.     ElseIf ExportType = "png" Then
  63.         zoom_coef = 100 / Sh.Parent.Windows(1).Zoom
  64.         Set area = Sh.Range(Sh.PageSetup.PrintArea)
  65.         area.CopyPicture xlPrinter
  66.         Set chartobj = Sh.ChartObjects.Add(0, 0, area.Width * zoom_coef, area.Height * zoom_coef)
  67.         chartobj.Chart.Paste
  68.         chartobj.Chart.Export output, ExportType
  69.         chartobj.Delete
  70.     End If
  71.    
  72. End Sub
  73.  
  74. Function sum_counter(CELL As Range)
  75.     Dim cell_address As String
  76.     cell_address = CELL.Address
  77.     new_value = CELL.Value
  78.     counter = 0
  79.     If cHas(Col, cell_address) Then
  80.         arr = cGet(Col, cell_address)
  81.         old_value = arr(0)
  82.         counter = arr(1)
  83.         If new_value > old_value Then
  84.             counter = counter + 1
  85.         End If
  86.     End If
  87.     cSet Col, cell_address, Array(new_value, counter)
  88.     sum_counter = counter
  89. End Function
  90.  
  91. Private Function cGet(ByRef Col As Collection, Key As String) As Variant
  92.     If Not cHas(Col, Key) Then Exit Function
  93.     On Error Resume Next
  94.         Err.Clear
  95.         Set cGet = Col(Key)(1)
  96.         If Err.Number = 13 Then
  97.             Err.Clear
  98.             cGet = Col(Key)(1)
  99.         End If
  100.     On Error GoTo 0
  101.     If Err.Number <> 0 Then Call Err.Raise(Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext)
  102. End Function
  103.  
  104. Private Sub cSet(ByRef Col As Collection, Key As String, Item As Variant)
  105.     If (cHas(Col, Key)) Then Col.Remove Key
  106.     Col.Add Array(Key, Item), Key
  107. End Sub
  108.  
  109. Public Function cHas(Col As Collection, Key As String) As Boolean
  110.     cHas = True
  111.     On Error Resume Next
  112.         Err.Clear
  113.         Col (Key)
  114.         If Err.Number <> 0 Then
  115.             cHas = False
  116.             Err.Clear
  117.         End If
  118.     On Error GoTo 0
  119. End Function
  120. Private Sub cRemove(ByRef Col As Collection, Key As String)
  121.     If cHas(Col, Key) Then Col.Remove Key
  122. End Sub
  123. Private Function cKeys(ByRef Col As Collection) As String()
  124.     Dim Initialized As Boolean
  125.     Dim Keys() As String
  126.  
  127.     For Each Item In Col
  128.         If Not Initialized Then
  129.             ReDim Preserve Keys(0)
  130.             Keys(UBound(Keys)) = Item(0)
  131.             Initialized = True
  132.         Else
  133.             ReDim Preserve Keys(UBound(Keys) + 1)
  134.             Keys(UBound(Keys)) = Item(0)
  135.         End If
  136.     Next Item
  137.  
  138.     cKeys = Keys
  139. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement