사용자가 Excel 및 LibreOffice Calc에서 작업 할 스프레드 시트가 있습니다. 두 가지 모두에서 작동하는 매크로를 디자인하고 싶습니다. 다음과 같은 코드를 생각하고있었습니다. Excel 다음 [VBA 코드], 그 외 [기본 또는 Python 코드]
두 키는 두 프로그램이 읽을 수있는 IF / THEN 문 (또는 이에 상응하는 것)이며 두 프로그램 모두 해당되지 않는 코드를 무시하도록하는 방법이라고 생각합니다.
이미 LibreCalc에서 실행하려고 시도했지만 작동하지 않습니다. Excel 및 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
어떤 제안?
답변
Calc 용 API는 Excel 용 API와 매우 다르므로 VBA는 StarBasic과 유사하지만 다른 API 호출을 처리하기 위해 별도의 코드를 작성해야합니다. 작업중 인 파일이 Calc인지 테스트하려면 StarBasic에서 작동합니다.
If oDoc.supportsService("com.sun.star.sheet.SpreadsheetDocument") Then
REM Run code for Calc
Else
REM Run code for Excel
End If
더 큰 문제는 스프레드 시트 내 에서이 코드를 실행하는 것에 대해 이야기하고 있다는 것입니다. 스프레드 시트를 .xls 또는 .xlsx로 저장하면 파일을 열 때 OpenOffice 및 LibreOffice에서 매크로를 비활성화합니다. 스프레드 시트가 .ods 파일로 저장되어 있으면 Excel의 경우와 반대입니다. 모든 코드를 올바르게 작성하더라도 적어도 하나의 프로그램은 실행을 거부합니다.
외부 프로그램이 스프레드 시트를 열고 해당 코드 블록을 실행하는 Excel 또는 Calc인지 테스트 할 수는 있지만 스프레드 시트에 포함 된 매크로로는 작동하지 않습니다.