How do I use a Variable to define a range in a macro

If I record a Macro to import and paste data and get something like:
rem ----------------------------------------------------------------------
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = “ToPoint”
args4(0).Value = “$B$11:$B$1000”
args4(0).Value = args4(0).Value & A1 & “)”

How do I make args4(0).Value = “$B$11:$B$1000” include a variable for the number of rows?

Something like args4(0).Value = “$B$11:$B$” & A1, where cell A1 contains the number of rows being imported.

Have not been able to find any guidance on this.

See https://wiki.documentfoundation.org/Macros/Basic/Calc/Ranges#Get_cell_by_name

ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(“A1”).string

Hi that is not what I am after.

I am looking for say “A10 : Bxxx” where xxx is a value held in cell “A1”

The value in “A1” is the count of the number of rows being imported, could be 10 could be 100000

Have tried the ```
range = sheet.getCellRangeByPosition(start_column, start_row, end_column, end_row)

Okay, what happens if you substitute in this line instead of A1 the line that @fpy showed in his answer? I mean

args4(0).Value = “$B$11:$B$” & ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(“A1”).string
1 Like

This isn’t what you presented in your OriginalQuestion.
Changing the question after having got an answer means wasting the time of those who try to help.

This is what the original said.

How do I make args4(0).Value = “$B$11:$B$1000” include a variable for the number of rows?

Something like args4(0).Value = “$B$11:$B$” & A1, where cell A1 contains the number of rows being imported.

Thanks that works just fine.

Recorded macros are very limited and use so-called UNO commands which are chosen without trying to get consistency with API calls.
Reworking recorded macros by deleting or changing parts or by insertion of hand-written Basic lines is not helpfulk with learning to code Basic macros.
If you consider to use hand-written code (instead of recorded macros) you may start with:

REM  *****  BASIC  *****

Option Explicit

Sub written()
Const startCopy         = "B11"
Const startPasteValues  = "K1"
Dim   numRows           As Long
Dim   theSheet          As Object
Dim   sourceCell1       As Object
Dim   targetCell1       As Object
Dim   sourceRangeCursor As Object
Dim   targetRangeCursor As Object
theSheet          = ThisComponent.CurrentController.ActiveSheet
numRows           = InputBox("Number of rows:   ", "Prompt for ...", 1)
sourceCell1       = theSheet.getCellRangeByName(startCopy)
sourceRangeCursor = theSheet.createCursorByRange(sourceCell1)
sourceRangeCursor.collapseToSize(1, numRows)
targetCell1       = theSheet.getCellRangeByName(startPasteValues)
targetRangeCursor = theSheet.createCursorByRange(targetCell1)
targetRangeCursor.collapseToSize(1, numRows)
targetRangeCursor.setDataArray(sourceRangeCursor.getDataArray())
End Sub

REM Advantages:
REM  You can use "speaking names".
REM  You get compact code.
REM  The code is well readable.
REM  You have an opportunity to learn something interesting.

I wrote the code based on your original question (as I undertsood it).

@Lupp No, my friend, it’s too early for him, he won’t understand - since he’s recording the code with a macro recorder, it means he just switched from Excel to Calc. It’ll be about a month before he downloads Pitonyak’s book and starts using the correct code.

You may read my (edited) answer again. The essence of the words inserted at the beginning:

  • Recorded macros are acceptable and useful if they simply do what’s tried to achieve.
  • When learning how to use Basic and the API, post-processing recorded macros is more of a hindrance than a help.

Nevertheless I had started considering an answer with reworking a recorded macro:
disask_126348_AutomatedCopyPaste.ods (15.9 KB)

Thanks for the help my problem is sorted.
I realize that for the purist the fact that I just want to have a Macro generator that works without any need to ever have recourse to dealing with writing code may be hard to take. But that is how it is for me I just want the stuff to be a tool which works. If I have to start learning how it all works then the system that has been programmed by those who like to do so. In this case Libra Office, have failed to meet the needs of their customer base. That said I think Libra Office is great, just do not want to have to fiddle with the Macros.

nah …
just some always want to show off :wink:
and you’re lucky no one suggested you should go for Python :innocent:


anywayz, for future readers, please #Mark_an_answer_as_Solution
Ask/Guide - How to use the Ask site - The Document Foundation Wiki

Done that, JohnSUN fixed it for me.

:+1: