Please understand that I am specifically looking for BASIC as that’s what all the macros in my spreadsheet are in.
In the macro below the line aAllRangesAddress
has a number of fields that are referenced by cell numbers. I’d like it to reference named ranges instead of the cell numbers.
REM ***** BASIC *****
Sub sumAndClearAllRanges3 'make sum from two ranges and put it to the second range
Dim oDoc As Object, oSheet As Object, data1(), data2(), oRange1 As Object, oRange2 As Object, i&, j&
Dim aAllRangesAddress As Variant
aAllRangesAddress = Array("A63:C72","I63:I72", "A74:C78","I74:I78", "A80:C90","I80:I90", "A92:C98","I92:I98", "A100:C118","I100:I118", "A120:C126","I120:I126", "N63:P74","V63:V74", "N76:P83","V76:V83", "N85:P88","V85:V88", "N90:P99","V90:V99", "N101:P109","V101:V109", "N112:P126","V112:V126" ) ' and all others
oDoc=ThisComponent
oSheet=oDoc.CurrentController.ActiveSheet
For j = LBound(aAllRangesAddress) To UBound(aAllRangesAddress) Step 2
oRange1=oSheet.getCellRangeByName(aAllRangesAddress(j))
oRange2=oSheet.getCellRangeByName(aAllRangesAddress(j+1))
data1=oRange1.getData
data2=oRange2.getData
for i=lbound(data1) to ubound(data1) 'make sum
data2(i)(0)=Fix(data1(i)(0)+data1(i)(1)+data1(i)(2)+data2(i)(0))
next i
oRange1.clearContents(1) 'clear all contents in the range
oRange2.setData(data2) 'set new data
Next j
end Sub
So instead of ("A63:C72","I63:I72", "A74:C78","I74:I78")
I’d prefer something like ("sGroup","sTotal", "bGroup","bTotal"
Part of the reason for this is I move the fields around every so often to make it easier for me to use and to add more stuff, but every time I do this I have to correct the macro above with the proper cell names. A named range works so much better because the named range moves with the cells in that range.
I first just tried changing the ranges to named ranges but that didn’t work. I tried fiddling with it using different methods in the few macros I have in this sheet but I can’t get any of them to work.