【问题标题】:How do I make vba code compatible with libre office如何使 vba 代码与 libre office 兼容
【发布时间】:2014-09-03 15:11:03
【问题描述】:

我最近从 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 代码,我无法在工作中使用开放式办公室/libreoffice...

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

谢谢

【问题讨论】:

    标签: excel vba openoffice.org libreoffice


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您必须翻译操作文档的部分以使用 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
      

      【讨论】:

        【解决方案3】:

        在 LibreOffice 4.4 中,第一个子例程根本不起作用(我怀疑是因为所有变量都以 'xl' 开头。如果将 ThisCell 更改为 ActiveCell,其他两个子例程将完美运行。

        而不是

        Option VBASupport 
        

        我正在使用

        Option VBASupport 1
        Option Compatible
        

        【讨论】:

          【解决方案4】:

          来自LibreOffice's online help file:

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

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

          实际上,您很可能需要与the LibreOffice API 坐下来重写功能。

          【讨论】:

          • Libre Office(Open Office 的 IBM 分支,StarWriter 的 Sun 版本)的一个显着特点是它支持 VBA,并运行“一些 Excel Visual Basic 脚本”。在线帮助文​​件说明了人们从 Apache Open Office 转向 Libre Office 的历史不连续性。
          • VBA 支持是由 Novell 开发的 initially,但他们 worked together 使用 Sun,它在 OOo 3.0 中支持 shipped(虽然还没有完成 anywhere
          【解决方案5】:

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

          它似乎是 OpenOffice 特有的,但我认为它也适用于 LibreOffice。

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

          【讨论】:

            猜你喜欢
            • 2011-05-14
            • 1970-01-01
            • 1970-01-01
            • 2011-07-27
            • 2023-03-15
            • 2022-01-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多