Ask Your Question
1

Changing a variable in a macro with a macro

asked 2018-03-18 02:35:54 +0200

ERas gravatar image

So referencing my past question on selecting a range of cells.

Sub column_SelectVisible()
REM Selects the first <lNumberOfCells> visible cells in column <lColumnIndex> in the current Sheet.
    Const lColumnIndex   As Long = 0
    Const lNumberOfCells As Long = 200

    Dim oDoc    As Object : oDoc    = ThisComponent
    Dim oSheet  As Object : oSheet  = oDoc.CurrentController.ActiveSheet
    Dim oColumn As Object : oColumn = oSheet.Columns.getByIndex( lColumnIndex )
    Dim oRanges As Object : oRanges = oColumn.queryVisibleCells()
    Dim oRange  As Object : oRange  = oRanges.getByIndex(0)

    Dim oCursor As Object : oCursor = oSheet.createCursorByRange( oRange )
    oCursor.collapseToSize ( 1, lNumberOfCells )
    oDoc.CurrentController.select( oCursor )
End Sub

I now just need to be able to change the column select variable lColumnIndex to increment as a way to select a different column with 0 being column A, 1 = B and so on. I'm terrible with programming so I kind of do need to be hand held into this.

edit retag flag offensive close merge delete

Comments

@ERas Please note. While the volunteers here are very willing to help with problems, do not utilize the site as a free programming service. If you are terrible with programming, who is going to maintain the code?

Ratslinger gravatar imageRatslinger ( 2018-03-18 03:36:43 +0200 )edit

Understandable. It's really just that I can't grasp the language and it would genuinely take a year or so to understand. But I am reading the code given to me and trying to make sense of it.

ERas gravatar imageERas ( 2018-03-18 09:26:42 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2018-03-18 03:29:28 +0200

librebel gravatar image

updated 2018-03-18 03:31:12 +0200

Hello again @ERas,

i made a few adjustments to the code; the method column_SelectNext() is now the method to be connected to a Toolbar button / Keyboard Shortcut :

Global g_NextColumnIndex As Long

Sub column_SelectNext()
REM Select a block of visible cells in the next column.
REM To reset the column counter, set g_NextColumnIndex = 0 ( or restart LO ).
    column_SelectVisible( g_NextColumnIndex )
    g_NextColumnIndex = g_NextColumnIndex + 1        REM increment by 1.
End Sub

Sub column_SelectVisible( lColumnIndex As Long )
REM Selects the first <lNumberOfCells> visible cells in column <lColumnIndex> in the current Sheet.
    Const lNumberOfCells As Long = 200

    Dim oDoc    As Object : oDoc    = ThisComponent
    Dim oSheet  As Object : oSheet  = oDoc.CurrentController.ActiveSheet
    Dim oColumn As Object : oColumn = oSheet.Columns.getByIndex( lColumnIndex )
    Dim oRanges As Object : oRanges = oColumn.queryVisibleCells()
    Dim oRange  As Object : oRange  = oRanges.getByIndex(0)
    Dim oCursor As Object : oCursor = oSheet.createCursorByRange( oRange )

    oCursor.collapseToSize ( 1, lNumberOfCells )
    oDoc.CurrentController.select( oCursor )
End Sub
edit flag offensive delete link more

Comments

+1 for gritting teeth. :)

Ratslinger gravatar imageRatslinger ( 2018-03-18 04:01:01 +0200 )edit

Much obliged, i used to do that for real during my sleep for a couple of years... it could be stress-related. I always appreciate to see "newcomers" learn to use macros in LibreOffice :)

librebel gravatar imagelibrebel ( 2018-03-18 04:56:10 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-03-18 02:35:54 +0200

Seen: 34 times

Last updated: Mar 18 '18