Macro & ChatGpt

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
charlie.it
Volunteer
Posts: 417
Joined: Wed Aug 21, 2013 2:12 pm
Location: Italy

Macro & ChatGpt

Post by charlie.it »

Hello everyone, intrigued by the news on Chatgpt, I wanted to try it.
After asking trivial questions, I had him write a macro:
question:
Write a Basic Calc macro that calculates the sum of two cells
answer:
Here is a macro that adds the contents of cells A1 and A2 and writes the result into cell A3

SubSumCells()
Dim value1 As Double
Dim value2 As Double

value1 = Range("A1").Value
value2 = Range("A2").Value

Range("A3").Value = value1 + value2
End Sub

In the macro I have defined two variables, value1 and value2, of type double to contain the values of cells A1 and A2. This makes the code more readable and less prone to errors.

To run the macro, select the cell where you want to write the result, then press "Alt + F8" to open the macro window. Select the "SumCells" macro and click "Run". The result of the sum will be written in cell A3.
question:
Your macros always give error: basic runtime error- undefined subprocedure
answer:
Sorry you're having an error with the macros I've provided. The "sub procedure not defined" error occurs when your code references a sub procedure or function that has not been defined correctly.

To fix this, check if the macro was written correctly and if all variables are defined. Also, check if the subroutine name is correct and if the macro ran correctly.

If the problem persists, try restarting the spreadsheet or copying and pasting the macro again to check for typos or formatting errors.

If you need further assistance, I recommend providing more details about the error you are having, including the macro code and full error message, so I can help you better.

question:
The error is reported on line: value1 = Range("A1").Value
answer:
The "undefined subprocedure" error you are getting may be caused by a problem with the OpenOffice Basic libraries reference.

To fix this, try adding the following line of code to the beginning of your macro, just after the "SubSumCells()" line:

Dim document As Object
document = ThisComponent

In this way, you are declaring a variable called "document" and assigning it the value "ThisComponent", which refers to the current document of the Calc worksheet. This should fix the library reference issue and allow you to access the cells in your spreadsheet.

Also make sure you have selected the right cell before running the macro. For example, if you want the sum to be written to cell A3, select this cell before running the macro.

Hope this can help you solve the problem. If the problem persists, let me know and I'll try to help you further.
question:
The error repeats again at line: value1 = Range("A1").Value
answer:
If the error occurs again at line value1 = Range("A1").Value, there may be a problem with the OpenOffice Basic libraries reference.

To fix this, try replacing the line value1 = Range("A1").Value with the following line:

value1 = ThisComponent.Sheets(0).getCellRangeByName("A1").Value

This code refers to the first sheet (Sheets(0)) of the current document (ThisComponent) and gets the value of cell A1 using the getCellRangeByName method.

Also try to verify that your macro was saved in the correct module of your spreadsheet and that the name of the macro exactly matches the name you used to call it.

I hope these tips help you fix the problem. If the problem persists, let me know and I will try to help you further.
question:
Okay now, I changed the macro for cell A2 as well. How do I edit the macro for cell A3?
answer:
To edit the macro for cell A3, you can simply add a new variable and assign the value of cell A3 to it, as follows:

SubSumCells()
Dim value1 As Double
Dim value2 As Double
Dim value3 As Double

value1 = ThisComponent.Sheets(0).getCellRangeByName("A1").Value
value2 = ThisComponent.Sheets(0).getCellRangeByName("A2").Value

ThisComponent.Sheets(0).getCellRangeByName("A3").Value = value1 + value2
End Sub
question:
It's ok now, it works perfectly
answer:
Great, glad I could help! If you need help in the future, don't hesitate to ask.
question:
In your next answers, keep in mind the suggestions you gave me
Of course, I will take into consideration the suggestions I gave you for my future answers. I hope I can help you again in the future. Thanks for your feedback!
What do you think'? Can we close the Forum?
charlie
Italian AOO Admin
macOS 14 Sonoma M1: Open Office 4.1.15 - LibreOffice 7.5.7.1

http://www.charlieopenoffice.altervista.org
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Macro & ChatGpt

Post by Villeroy »

Yes, I'm so delighted. Nobody will treat us as human macro recorders anymore. ShitGPT is a lot more patient than I ever was, and it does not grow old.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro & ChatGpt

Post by JeJe »

Very impressive... in a got there in the end way.

We could have a "try ChatGPT first" recommendation but there might be an explosion of threads asking why such and such ChatGPT script doesn't do anythng...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Macro & ChatGpt

Post by Villeroy »

Well, the above conversation depends entirely on the questioner's natural intelligence.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
charlie.it
Volunteer
Posts: 417
Joined: Wed Aug 21, 2013 2:12 pm
Location: Italy

Re: Macro & ChatGpt

Post by charlie.it »

A while ago I asked him the question again, the code is slightly different, the cells have been moved horizontally, but the macro works.
It almost seems that he has studied during the past hours :roll: .

answer:
Write a Basic macro for Calc that calculates the sum of two cells
question:
Here is a Basic macro for Calc that calculates the sum of two cells:

Sub CalculateSum()
Dim oDoc as Object
Dim oSheet as Object
Dim oCell1 as Object
Dim oCell2 as Object
Dim or Range as Object
DimnSum as Double

' Set the current document
oDoc = ThisComponent

' Set the current worksheet
oSheet = oDoc.CurrentController.ActiveSheet

' Set the cells to add
oCell1 = oSheet.getCellByPosition(0, 0)
oCell2 = oSheet.getCellByPosition(1, 0)

' Calculate the sum of the cells
nSum = oCell1.getValue() + oCell2.getValue()

' Set the target cell
oRange = oSheet.getCellByPosition(2, 0)

' Write the result to the target cell
oRange.setValue(nSum)
End Sub

This macro sets up the current worksheet and the cells to be added, calculates the sum of the two cells, and then writes the result into the third cell. You can customize the position of the cells and the format of the sum by changing the code accordingly.
charlie
Italian AOO Admin
macOS 14 Sonoma M1: Open Office 4.1.15 - LibreOffice 7.5.7.1

http://www.charlieopenoffice.altervista.org
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro & ChatGpt

Post by JeJe »

ChatGPT can you rewite OpenOffice to fix all the bugs on Bugzilla... then do LibreOffice please... and Windows 10...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro & ChatGpt

Post by RoryOF »

Ant what will win the 2:30 at Longchamp?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
charlie.it
Volunteer
Posts: 417
Joined: Wed Aug 21, 2013 2:12 pm
Location: Italy

Re: Macro & ChatGpt

Post by charlie.it »

Today I quizzed him on a more complex Calc sheet, both with and without macros. He made me make a large number of attempts but was unable to resolve.
Volunteers of the Forum, you can still hope to remain in your post :lol:
charlie
Italian AOO Admin
macOS 14 Sonoma M1: Open Office 4.1.15 - LibreOffice 7.5.7.1

http://www.charlieopenoffice.altervista.org
Post Reply