Calc: macro replacing cells connected to some date under some fix cell with the same date?

Hi,

I am just wondering if my attempts are worth pursuing or if the best I can expect is to be told by ChatGPT that “this version of code really cannot fail” (a hundred times). Please briefly consider the following.

I would like to have a macro that

  1. works with cells containing dates (active sheet only).
  2. cuts the text from these cells (and four cells on the right side), with an exception of row A which is always maintened,
  3. goes to the A-cell containing the corresponding date (not necessarily on the same sheet) and -crucial point - inserts the text so that any text would be be lost.

A few months ago ChatGPT wrote some macros for me for the first tiome; without it, I would never attempt this. Some macros work fine.

Could a macro like that be easily repaired, or would I be better off not wasting my time trying to do so with AI? How much would some programmer ask for this task?

Thank you.

Sub PresunDataPodleDatumu()
    Dim oDoc As Object, oSheet As Object
    Dim i As Long, col As Long
    Dim oCell As Object, oDate As Variant
    Dim qSheetName As String
    Dim targetSheet As Object
    Dim targetCol As Long, targetRow As Long
    Dim sourceSheetName As String
    Dim maxCols As Long
    
    oDoc = ThisComponent
    oSheet = oDoc.CurrentController.ActiveSheet
    sourceSheetName = oSheet.getName()
    maxCols = oSheet.getColumns().getCount()
    
    For i = 1 To oSheet.getRows().getCount() - 1
        For col = 0 To maxCols - 5 ' Aby bylo místo pro 4 buňky '
            oCell = oSheet.getCellByPosition(col, i)
            
            If oCell.Type = com.sun.star.table.CellContentType.VALUE Then
                oDate = oCell.getValue()
                If IsDate(oDate) Then
                    Dim datum As Date
                    datum = oDate
                    
                    Dim mesic As Integer
                    mesic = Month(datum)
                    
                    ' Zjisti název listu dle měsíce '
                    Select Case mesic
                        Case 1 To 3
                            qSheetName = "Q1"
                        Case 4 To 6
                            qSheetName = "Q2"
                        Case 7 To 9
                            qSheetName = "Q3"
                        Case 10 To 12
                            qSheetName = "Q4"
                        Case Else
                            qSheetName = ""
                    End Select
                    
                    If qSheetName <> "" Then
                        targetSheet = oDoc.Sheets.getByName(qSheetName)
                        targetCol = NajdiSloupecSPresnymDatem(targetSheet, datum)
                        
                        If targetCol >= 0 Then
                            targetRow = NajdiPrvniPrazdnyRadek(targetSheet, targetCol)
                            ' Zkopíruj 4 buňky vpravo '
                            Dim j As Integer
                            For j = 0 To 3
                                targetSheet.getCellByPosition(targetCol + j, targetRow).setValue(oSheet.getCellByPosition(col + j, i).getValue())
                            Next j
                        End If
                    End If
                End If
            End If
        Next col
    Next i
    MsgBox "Hotovo."
End Sub
' 🔧 Pomocné funkce: '
' basic Copy Edit '
Function NajdiSloupecSPresnymDatem(oSheet As Object, hledaneDatum As Date) As Long
    Dim col As Long, maxCols As Long
    maxCols = oSheet.getColumns().getCount()
    
    For col = 0 To maxCols - 1
        Dim bunka As Object
        bunka = oSheet.getCellByPosition(col, 0)
        If bunka.Type = com.sun.star.table.CellContentType.VALUE Then
            If Int(bunka.getValue()) = Int(hledaneDatum) Then
                NajdiSloupecSPresnymDatem = col
                Exit Function
            End If
        End If
    Next col
    NajdiSloupecSPresnymDatem = -1 ' nenalezeno '
End Function

Function NajdiPrvniPrazdnyRadek(oSheet As Object, colIndex As Integer) As Long
    Dim i As Long
    For i = 1 To oSheet.getRows().getCount() - 1
        If oSheet.getCellByPosition(colIndex, i).getType() = com.sun.star.table.CellContentType.EMPTY Then
            NajdiPrvniPrazdnyRadek = i
            Exit Function
        End If
    Next i
    NajdiPrvniPrazdnyRadek = oSheet.getRows().getCount()
End Function

Ahoj!

For whom is it better? If you don’t spend your time (mind you, spend YOUR time absolutely free of charge!), give him tasks and check all his solutions, point out his mistakes and explain how it should have been done - then he will never learn to program…

Do you want to pay for the solution? The price may vary. A good programmer will do it in 30 minutes and for a glass of beer, a bad programmer will demand 200 EURO and will torment the AI for a week to make at least something similar to the solution of your problem.
The brilliant programmers who have gathered here will do it for you for free, for a simple “Thank you!” - simply because for them it is a hobby, they are interested in it. But be prepared that they will not do it right away - first they will definitely tell you that this is a task for a database, and not for Calc, that you did not tell enough about the data in your book, that it was not worth messing with the AI and many other offensive words. But you will get the solution. It will be better and faster than communicating with ChatGPT.

2 Likes

If I understood the code from the question correctly, the task is formulated approximately as follows:
I have a spreadsheet in which the data is divided into four separate sheets, one sheet per quarter of the year. Each table on the sheet contains a date in the first column and data related to this date in the next four columns. Sometimes I receive a list of data based on which I must update, actualize the information in the sheets. I insert this list into a separate sheet and want the macro, focusing on the date in column A, to update or supplement the data in the sheet with the corresponding quarter.
Tell me please, does this look like your task?

defintitly need a good FAQ for this.

  • first and foremost, join a .ods example
  • make sure you know about XY problem - Wikipedia ;
    chances are there’s no real need for a macro ;
    and formulas, far easier to maintain, can do the job

Thank you for your time.

I have a spreadsheet in which the data is divided into four separate sheets, one sheet per quarter of the year. Each table on the sheet contains a date in the first column and data related to this date in the next four columns. Sometimes I receive a list of data based on which I must update, actualize the information in the sheets. I insert this list into a separate sheet and want the macro, focusing on the date in column A, to update or supplement the data in the sheet with the corresponding quarter.

Yes, I will precize. I work with Calc. Now, I have one large sheet with words I repete regulary based on speced repetition. I don´t want to use Anki etc. cause it isn´t flexible enough. The words are in Calc, column A: date when it should be learnt, for example 01.05.2025, C: the sentence,“clue” to the word or words, D: the word / words. I already have a macro that makes my approach more efficient: in row B, I write numbers of day when the word should be repeated, f. e. 5, 30…, and the macro creates sheet named 5, 30… from which I cut and paste all contents to the original sheet manually (with some additional macros). I had an idea that it would be fine to automatize also this phase. Then it would be enough to write numbers to B only.

So I considered different approach: to have four sheets, one for the three months in one year (later, they would be more but not much more, for example eight as the intervals aren´t longer that two years: here, I hope AI can efficiently help if the foundation is built). Within sheet 01-03, A1 can be 01.1.2026, E1 02.1.2026, I1 03.1.2026 and so on -three months on one sheet: the days to the first row would write manually.

The idea is that if I would run the macro on active sheet, it will search all dates in columns A1, E1, I1… (in active sheet but not in the first row) for the (maybe only future, maybe not, it isn´ crucial) dates and cut their contents (with the three rows on their right side) and paste it to “their” main columns on active or another sheet of the doc. (I deleted all manual formatting from my text.)

I have also a macro that works within one sheet (creates temp. sheets according to numbers from B, computes when exactly the words should be learned -one same date for one temp. sheet, then finds the first row in the main sheet that contains the date , pastes below so many rows as they are on the temporarily sheet - 1 (row with date), than cuts and pastes from the temp. sheet ald deletes this sheet),but I don´t think it is secure to my data. If another macro counts words in C+D before and after, the numbers differ: f.e. 30 signs but I can´t find any good explanation. It would be easier to improve this macro but the priority is to prevent any data lost so I considered to try it with more sheets instead.

As I only writes macros since three months with AI, I have no idea of how difficult it is to achieve my goals. Chat GPT wrotes macros for mne that are much complicater and that really works but here I can´t fint a solution.

I will create a complete Calc sample in on hour. und load it.

The file that I promised:
Table_for_macro.ods (12.6 KB)

just cuious, how did you populate these dates ?

I wrote for example 01.8. and the rest changed automatically. When AI wrote another macro for me that changes numers to dates, there was a problem with such automatically created dates (not detected as dates initially) but AI made a macro that changed their format so that it can be detected.

Be aware that you can configure the date acceptance patterns - you don’t need a macro.

See Languages (Options) and scroll down a bit.

Are you talking about date acceptance patterns (only working when something is entered or edited by the user) and a kind of simulation working when something is entered based on a macro?

in other words: the layout and the design for this AI-produced »Learning-platform« is complete garbage!

But despite all,AI is the only way I and many other could write macros.They are problematical in many ways but without AI I would never (really never) consider to write macros. I lost a lot of time beleaving what AI promised (“this will finally work”) but some macros are fine. Someone more skilled would write my macro in 20 minutes (not 10 hours like mine),but that´s an another point.

From your own description and your »proto-type.ods« I had no Idea about what should solved.
It was only after I looked at the Anki website that I could imagine what you actually wanted.

Did you read my question concerning this in my comment above?
Would you be so kind to answer it?

BTW:
Near München (Munich) we have a “Max-Planck-Institut für Biologische Intelligenz”.

I want to finish with this topic now, and attach therefore a related example without waiting for an anwer.
disask125139realatedDemo.ods (17.3 KB)

Excuse me for waiting. “I wrote for example 01.8. and the rest changed automatically” - here,I mean without any macro. Later,there was some problems with these dates because they was not recognized as dates. Then I needed a macro that changed the type of cells that were “like-dates” but “no-dates”) (simple manual change in format - cells didnt´t help). But after all, they were recognized as dates. (I didn´t aply this macro on my sample so it is probable that the "date-cells"are not "normal-date-cells).

You do NOT write these macros - and AI also doesn’t.
What you get from AI is a collection of misunderstood fragments of programs written by humans like those who are trying to help here.
We have never been asked for permission by the robbers who organize AI bots and call the money-printing machinery their “intellectual property.”

1 Like

“What you get from AI is a collection of misunderstood fragments of programs written by humans like those who are trying to help here.” I know, but sometimes it helps and can save also your time, as you are not asked the questions you would be asked if there wasn´t AI. I will save your macro and apply it to solve any other problems with dates,thanks. But I think what I am facing now isn´t about dates (I tried now with your macro applied before mine).

NO it doesnt help, but you and AI waste our time!