Excel 如何使vba代码与libre office兼容

Excel 如何使vba代码与libre office兼容,excel,vba,openoffice.org,libreoffice,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 WashCoun

我最近从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
有没有办法转换这段代码,使之与libreoffice兼容,或者我必须学习一种全新的语言,比如python?学习python不会是一个问题,但并不能解决我的问题,因为我在excel中有许多与工作相关的文件,其中包含大量vba代码,我不可能在工作中使用open office/libreoffice

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


谢谢

我知道的唯一自动工具是(请注意,我没有个人或专业经验,也没有与网站的任何联系)

它似乎专门针对OpenOffice,但我认为它也适用于LibreOffice

一般来说,你最好自己做,因为这个工具还远远不够完美

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

如果在LibreOffice-preferencestols-Options-Load/Save-VBA属性中启用此功能,则LibreOffice的最新版本可以运行一些Excel Visual Basic脚本


实际上,您很可能需要坐下来重新编写该功能。

在LibreOffice 4.4中,第一个子例程根本无法工作(我怀疑是因为所有以“xl”开头的变量。如果将此单元格更改为ActiveCell,则其他两个子例程可以完美工作

而不是

Option VBASupport 
我正在使用

Option VBASupport 1
Option Compatible

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

Range(Cells(CurrentRow, i), Cells(CurrentRow, i)).Value = "a"
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
当你有活动单元格时,你得到单元格地址,从中得到行。你根本不需要使用范围,因为你只关心一个单元格,所以,你得到活动工作表,然后从工作表中得到一个特定的单元格

大概是这样的: ThisComponent.getCurrentController().getActiveSheet().getCellByPosition(nCol,nRow).getString()=“a”

我不想弄清楚这是怎么回事

Selection.SpecialCells(xlBlanks).Delete shift:=xlToLeft

Selection.SpecialCells(xlBlanks).Delete shift:=xlToLeft
如果我没有弄错的话,会删除空白单元格

Libre-Office的一个显著特点是它对VBA有一些支持,并运行“一些Excel Visual Basic脚本”.在线帮助文件说明了从Apache Open Office到Libre Office的人们历史的不连续性。