Search code examples
rangelibreoffice-calclibreoffice-basic

Subtract column position using Macros using StarBasic in LibreCalc given a range of columns eg. A1:B1 - must result 1


Subtract column position using VBA (libre StarBasic) macro in LibreCalc given a range of columns eg. A1:B1 must result 1

I have a set of ranges I would like to get the Column position difference as integer using VBA macro in Libre Calc eg.

B17:C28,E17:G28,L17:O28

It must results (C-B) = 1, G-E = 2, O - L = 3

How I can do this?


Solution

  • I had to use "Columns.Count" for ActiveSheet.getCellRangeByName("a1:a1") to get the information of subtracting column position in a Range.

    Dim sDataRng 'Where is the data
    
    
    ' Constant with ranges
    Const SCells = "B1:C1,E1:G1,L1:1"
    
    ' split constant using as separator ",".
    aCells = Split(SCells,",")
     
    
    Sub subtract_column_position 
    
    for i  = 0 to 2 step 1  
                
            ActiveSheet = ThisComponent.CurrentController.ActiveSheet
            sDataRng    = aCells(i)
            oRange      = ActiveSheet.getCellRangeByName(aCells(i))
    
            print oRange.Columns.Count - 1
    
    next i 
    
    end sub