Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function ColumnNameToNumber(oSheet As Object, columnName As String) As Integer
  2.     ColumnNameToNumber = oSheet.getCellRangeByName(columnName & 1).getCellAddress().Column
  3. End Function
  4.  
  5. Function CountMoney(oSheet As Object, dateColumn As Integer, moneyColumn As Integer, startDate As Date, endDate As Date) As Double
  6.     Dim oCells As Object : oCells = oSheet.getColumns().getByIndex(dateColumn)
  7.     Dim oNotEmpty As Object : oNotEmpty = oCells.queryContentCells(com.sun.star.sheet.CellFlags.DATETIME)
  8.     Dim nrows As Integer : nrows = oNotEmpty.computeFunction(com.sun.star.sheet.GeneralFunction.COUNT)
  9.  
  10.     For i = 0 To nrows - 1
  11.         Dim rowDate As Date : rowDate = CDate(oSheet.getCellByPosition(dateColumn, i).Value)
  12.        
  13.         If startDate <= rowDate And rowDate <= endDate Then
  14.             answer = answer + oSheet.getCellByPosition(moneyColumn, i).Value
  15.         End If
  16.     Next
  17.    
  18.     CountMoney = answer
  19. End Function
  20.  
  21. Sub Main
  22.     Dim oSheet As Object : oSheet = ThisComponent.Sheets(0)
  23.     Dim inputString As String
  24.     Dim components() As String
  25.    
  26.     inputString = InputBox("Enter 'dateColumn,moneyColumn,startDate,endDate': dates are in format MM/DD/YYYY, columns are column letters")
  27.     components = Split(inputString, ",")
  28.    
  29.     Print CountMoney(oSheet, ColumnNameToNumber(oSheet, components(0)), ColumnNameToNumber(oSheet, components(1)), CDate(components(2)), CDate(components(3)))
  30. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement