如何使vba代码与libre office兼容

use*_*707 13 excel vba openoffice.org libreoffice

我最近从windows迁移到pclinuxos并且似乎喜欢它.我面临的唯一问题是libreoffice,默认的电子表格包与excel宏不兼容.下面是我的vba代码:

Option VBASupport 
Sub DeleteToLeft()
    Selection.SpecialCells(xlBlanks).Delete shift:=xlToLeft
End Sub
Function SinceLastWash()
    Application.Volatile
    WashCount = 0
    WearCount = 0
    CurrentRow = Application.ThisCell.Row
    For i = 3 To 35
        If Range(Cells(CurrentRow, i), Cells(CurrentRow, i)).Value = "a" Then
            WearCount = WearCount + 1
        End If
        If Range(Cells(CurrentRow, i), Cells(CurrentRow, i)).Value = "q" Then
            WashCount = WashCount + 1
            WearCount = 0
        End If
    Next i
    SinceLastWash = WearCount
End Function
Function testhis()
testhis = Application.ThisCell.Row
End Function
Run Code Online (Sandbox Code Playgroud)

有没有办法转换这个代码,使其与libreoffice兼容,还是我必须学习像python这样的全新语言?学习python不是问题,但不是我的问题的解决方案,因为我在excel中有很多与工作相关的文件,它们有很多vba代码,我不可能在工作中使用开放式办公室/ libreoffice ...

我只想补充一点,函数SinceLastWash在我使用它的某些单元格中给出了正确的值,而在其他单元格中给出了错误,#NAME?

谢谢

Rub*_*uck 9

来自LibreOffice的在线帮助文​​件:

除了少数例外,Microsoft Office和LibreOffice无法运行相同的宏代码.Microsoft Office使用VBA(Visual Basic for Applications)代码,LibreOffice使用基于LibreOffice API(应用程序接口)环境的基本代码.虽然编程语言是相同的,但对象和方法是不同的.

如果您在LibreOffice - PreferencesTools - 选项 - 加载/保存 - VBA属性中启用此功能,则最新版本的LibreOffice可以运行一些Excel Visual Basic脚本.

实际上,您很可能需要坐下来使用LibreOffice API并重写功能.


And*_*rew 5

您必须翻译操作文档的部分才能使用 UNO API。遗憾的是,这可能会很棘手,具体取决于您的宏的作用。基本语句直接起作用。修改文档通常不会。

Range(Cells(CurrentRow, i), Cells(CurrentRow, i)).Value = "a"
Run Code Online (Sandbox Code Playgroud)

Cells 命令根据行和列返回特定单元格。因此,您需要当前行。这是获取活动单元格的一些疯狂之处:

Sub RetrieveTheActiveCell()
  Dim oOldSelection 'The original selection of cell ranges
  Dim oRanges       'A blank range created by the document
  Dim oActiveCell   'The current active cell
  Dim oConv         'The cell address conversion service
  Dim oDoc
  oDoc = ThisComponent

  REM store the current selection
  oOldSelection = oDoc.CurrentSelection

  REM Create an empty SheetCellRanges service and then select it.
  REM This leaves ONLY the active cell selected.
  oRanges = oDoc.createInstance("com.sun.star.sheet.SheetCellRanges")
  oDoc.CurrentController.Select(oRanges)

  REM Get the active cell!
  oActiveCell = oDoc.CurrentSelection

  oConv = oDoc.createInstance("com.sun.star.table.CellAddressConversion")
  oConv.Address = oActiveCell.getCellAddress
  Print oConv.UserInterfaceRepresentation
  print oConv.PersistentRepresentation

  REM Restore the old selection, but lose the previously active cell
  oDoc.CurrentController.Select(oOldSelection)
End Sub
Run Code Online (Sandbox Code Playgroud)

当您拥有活动单元格时,您将获得单元格地址,并从中获得该行。您根本不需要使用范围,因为您只关心单个单元格,因此,您获得活动工作表,然后从工作表中获取特定单元格。

像这样的东西:ThisComponent.getCurrentController().getActiveSheet().getCellByPosition(nCol, nRow).getString() = "a"

我不想弄清楚这是做什么的

Selection.SpecialCells(xlBlanks).Delete shift:=xlToLeft
Run Code Online (Sandbox Code Playgroud)