LibreOffice Calc VBA如何替代Excel的CurrentRegion获取当前区域?
在LibreOffice VBA中替代Excel的CurrentRegion
方法1:使用UNO API原生方法getCurrentRegion()
LibreOffice的单元格范围对象支持getCurrentRegion()方法,和Excel的CurrentRegion逻辑完全一致——自动扩展到被空行/空列分隔的连续非空单元格区域。修正后的代码示例:
Dim shtData As Object Dim rgeData As Object Set shtData = ThisComponent.Sheets.getByName("MASTER INDEX") ' 获取A4所在的连续数据区域 Set rgeData = shtData.getCellRangeByName("A4").getCurrentRegion()
注意:LibreOffice VBA中对象赋值必须用Set关键字,原代码缺少该关键字会报错。
方法2:手动扩展范围(兼容旧版本)
如果遇到部分旧版本LibreOffice对getCurrentRegion()支持不佳的情况,可以手动遍历扩展范围:
Dim shtData As Object Dim startCell As Object Dim lastRow As Long, lastCol As Long Dim rgeData As Object Set shtData = ThisComponent.Sheets.getByName("MASTER INDEX") Set startCell = shtData.getCellRangeByName("A4") ' 向下查找最后一行非空单元格 lastRow = shtData.getCellRangeByPosition(startCell.CellAddress.Column, startCell.CellAddress.Row, _ startCell.CellAddress.Column, shtData.Rows.Count - 1).getEndOfUsedArea().Row ' 向右查找最后一列非空单元格 lastCol = shtData.getCellRangeByPosition(startCell.CellAddress.Column, startCell.CellAddress.Row, _ shtData.Columns.Count - 1, startCell.CellAddress.Row).getEndOfUsedArea().Column ' 构建最终数据区域 Set rgeData = shtData.getCellRangeByPosition(startCell.CellAddress.Column, startCell.CellAddress.Row, _ lastCol, lastRow)
内容的提问来源于stack exchange,提问作者lvr123
相关产品推荐
相关产品推荐

