1
1
I have a spreadsheet that users will be working with in both Excel and LibreOffice Calc. I'd like to design macros that will work in both. I was thinking of code that would say: If Excel Then [VBA code], Else [Basic or Python code]
I think the two keys would be an IF/THEN statement (or equivalent) that both programs could read, and a way to ensure that both programs would ignore the code that isn't applicable to them.
I've already tried running it in LibreCalc but it won't work. Here's the code I need to run in Excel and Calc:
Public Sub spubInsertCurrDateTimeText()
'DESCRIPTION: inserts current date and time into the active cell in text format.
Dim dtmNow As Date
Dim CurrTemp As Date
Dim CurrTot As Date
Dim NewTot As Date
Dim StartCol As Integer
Dim StopCol As Integer
Dim ClockTimerCol As Integer
Dim TotalTimeCol As Integer
dtmNow = Now 'sets variable to value of function NOW (current date/time)
StartCol = 1 'this is the column where the user enter the starting time
StopCol = 2 'this is the column where the user enter the ending or stop time
ClockTimerCol = 3 'this is the column that calculates elapsed time (StopCol minus StartCol)
TotalTimeCol = 4 'this is the column that tracks the total time elapsed in minutes
If ActiveCell.Column = StartCol Then
ActiveCell = dtmNow 'inserts variable into the active cell
Worksheets(ActiveSheet.Name).Cells(ActiveCell.Row, StopCol).Value = 0
ElseIf ActiveCell.Column = StopCol Then
ActiveCell = dtmNow 'inserts variable into the active cell
CurrTot = Worksheets(ActiveSheet.Name).Cells(ActiveCell.Row, TotalTimeCol).Value
CurrTemp = Worksheets(ActiveSheet.Name).Cells(ActiveCell.Row, ClockTimerCol).Value
NewTot = CurrTot + (CurrTemp * 1440)
Worksheets(ActiveSheet.Name).Cells(ActiveCell.Row, TotalTimeCol).Value = NewTot
Worksheets(ActiveSheet.Name).Cells(ActiveCell.Row, StartCol).Value = 0
Worksheets(ActiveSheet.Name).Cells(ActiveCell.Row, StopCol).Value = 0
Else
ActiveCell = dtmNow 'inserts variable into the active cell
End If
End Sub
Any suggestions?
Could you save two macros, one for each format, and use a similar test to call the appropriate macro? – fixer1234 – 2015-06-19T18:50:11.100
@fixer1234 Not to my knowledge. I think you'd need to have the user open the spreadsheet, then start a separate script. Or, put the script in the startup folder on their computer and have it continuously (every 20 seconds or something) check to see if this file is open and, if it is open, then run the code for the spreadsheet. – Lyrl – 2015-06-19T19:08:44.310