VBA

9 replies [Last post]
wek
Offline
Last seen: 3 weeks 3 days ago
Title:
Joined: 5 May 2016
Posts: 3
Hello,

I’m new here and I want to go with LibreOffice Calc in stead of Excel.
I’m using some macro’s in VBA that I would not miss in Calc.

Example :
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error Resume Next If Target = Range(“A1”) Then If Mid(Target, 1, 2) = “• “ Then NaamTabblad = Mid(Target, 3, Len(Target) – 2) ‘Wanneer de kop begint met • dan moet het • eraf Else NaamTabblad = Target ‘Bij het ontbreken van het • dan de complete inhoud End If ActiveSheet.Name = NaamTabblad End If End Sub

When I fill A1 then the Name of the tab should change to the input of A1.
I would learn basis, but I dont the steps.
Can anybody learn the steps to go from VBA to Basis/Python/Java…

mark_t
Offline
Last seen: 8 hours 17 min ago
Title: ★★★★
Joined: 26 Apr 2016
Posts: 90
Check the section for Macros and UNO API
Check the section for Macros and UNO API, oweng has sticky post with links to programming documentation.

If you know VBA then it should be easy to start with Basic in LibreOffice. Although you will find all the objects are different structures, basic language is almost identical.

Example below based on your VBA.

From Calc, Tools, Macros, Organise Macros, LibreOffice Basic.

To put your macro in your workbook, select the workbook in the left menu, select standard, then use New to create a module.

To set up the event, right click on the tab of your sheet, select “Sheet Events”, choose “Content changed”, Assign Macro, select the macro name and click OK.


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

Option Explicit

'	Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Private Sub Worksheet_Change(Target As Object)

    Dim sText As String
    Dim NaamTabblad As String

'  	On Error Resume Next 
'  	If Target = Range(“A1”) Then 	
    If Target.ImplementationName = "ScCellObj" Then		'	Check single cell
        If Target.CellAddress.Row = 0 And Target.CellAddress.Column = 0 Then	'	First Row and column

'    		If Mid(Target, 1, 2) = “• “ Then 
            sText = Target.String
            If Mid(sText, 1, 2) = ". " Then 
'   	   		NaamTabblad = Mid(Target, 3, Len(Target) – 2) ‘Wanneer de kop begint met • dan moet het • eraf 
                NaamTabblad = Mid(sText, 3, Len(sText) - 2) 	'	Wanneer de kop begint met • dan moet het • eraf 
            Else
'    			NaamTabblad = Target ‘Bij het ontbreken van het • dan de complete inhoud
                NaamTabblad = sText 	'	Bij het ontbreken van het • dan de complete inhoud 
            End If 

'   		ActiveSheet.Name = NaamTabblad 
            Target.Spreadsheet.Name = NaamTabblad
        End If
    End If 
End Sub
LibreOffice 5.1.3.2, Windows 8.1
Lupp
Lupp's picture
Offline
Last seen: 1 day 16 hours ago
Title: ★★★★★
Joined: 1 Aug 2012
Posts: 1515
There is no "translation"...
…because the ways LibO gives access to its objects usable by programs in BASIC or another programming language is substantially different from the way Excel and VBA do that.

LibreOffice is providing this access mainly via uno. In addition there are only a few specific predefined variables and functions. As far as I understand with VBA everything seems to be integrated directly. Please note: I am not an expert concerning programming for LibO, and I know next to nothing about VBA, to put it in clear words.

There is partial support for VBA constructs in LibO BASIC (option VBAsupport 1), however, and it is geting improved, but still very limited.

I can see three ways for you.

1. Abandon the usage of old subs and find new ways to do things. To change a sheet’s name e.g. entering something into a cell is a way to achieve that goal, very many users never miss.

2. Try if the present level of VBA support is already sufficient.

3. Develop your “some macros” anew for LibO and take this as an opportunity to learn. There are some documents you may start with linked in this sticky thread.

To give an example for the different ways of LibO, I attach a Calc document containing a BASIC sub doing the paradox renaming of sheets. It is also demonstrating a second solution based on a side effect of a user function. If wanting at all to rename a sheet by editing a cell I would surely prefer the second way.

AttachmentSize
lof13357StrangeSheetRenaming001.ods 30.32 KB

Currently installed: LibO V5.1.2 on Windows 10
Maybe we might! (Create a powerful UFO: United Free Office!) Lupp from München
wek
Offline
Last seen: 3 weeks 3 days ago
Title:
Joined: 5 May 2016
Posts: 3
Thank you very much !!
Thank you very much !! Mark_t and Lupp, the first step is put on my way on LibO – BASIC !!
info@logicos.co.uk
Offline
Last seen: 1 week 5 days ago
Title:
Joined: 28 Apr 2016
Posts: 3
LO Calc Macros - Recording
Hi. I am new to LibreOffice Calc having moved from Excel. In Excel I had recorded macros as I know nothing about writing them. I have a list of figures that I have to download from the net each week and open as a csv. These then need to be moved around to line up with the spreadsheets on our system (which can’t be altered as they are in a specific format for other reasons). It’s quite a long-winded process which is why I used a macro rather than having to go through step by step every week. I found the macro record button in Calc and went through the steps to record my changes which was fine, but when I ran the macro it seemed to stop part way through. I have tried breaking the task down and recording separate macros for the different sections but then it only runs so far on each macro. Can anyone help? By the way I am using LibreOffice version 5.1.2.2 running on Windows 7 Professional.
mark_t
Offline
Last seen: 8 hours 17 min ago
Title: ★★★★
Joined: 26 Apr 2016
Posts: 90
Better to post under a new
Better to post under a new topic, as it might not get noticed here.

Recording macros is normally not recommended. Even with Excel it’s not a good way to create macros, but if it’s simple enough you probably can get away with it in Excel.

Would you be able to always save your new csv file as a .ods file to a known location on your PC? If you do this then you an use the INDIRECT function to refer to values from your csv file. This would avoid using macros.

Example put this file name in cell A1 of the spreadsheet where you want the data to be copied. Then in a cell where you want to copy the data use


=INDIRECT("'file:///" & $A$1 & "'#$Sheet1.A4" ). 

Or you could include the reference filename in the INDIRECT function,


=INDIRECT("'file:///D:/UserData/Mark/Desktop/Untitled 1.ods'#$Sheet1.A4" ). 

Note the single quotes ‘ inside the above strings are required.

After changing the reference file you might need to use ctrl-shift-F9 to force recalculation of the values in your template. The reference file also needs to be open in calc, and it needs to be saved to a local file.

LibreOffice 5.1.3.2, Windows 8.1
mark_t
Offline
Last seen: 8 hours 17 min ago
Title: ★★★★
Joined: 26 Apr 2016
Posts: 90
Just to add that you might
Just to add that you might want to change the reference sheet file name each week, in case the latest version of that file changes your spreadsheets from previous weeks.
LibreOffice 5.1.3.2, Windows 8.1
info@logicos.co.uk
Offline
Last seen: 1 week 5 days ago
Title:
Joined: 28 Apr 2016
Posts: 3
Better to post under a new
Thank you very much Mark. I will see if I can accomplish what you advised.
mark_t
Offline
Last seen: 8 hours 17 min ago
Title: ★★★★
Joined: 26 Apr 2016
Posts: 90
I saw a recent post that
I saw a recent post that Excel macros can work if you save from excel as an Excel 2003 .xls file. This might work for you if your macros were recorded by Excel.
LibreOffice 5.1.3.2, Windows 8.1
info@logicos.co.uk
Offline
Last seen: 1 week 5 days ago
Title:
Joined: 28 Apr 2016
Posts: 3
Ooh thank you. I’ll try it.
Ooh thank you. I’ll try it. Smile

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.