如何在不修改电子表格的情况下返回修改范围

发布于 2025-02-04 08:06:55 字数 1006 浏览 2 评论 0原文

我想要我的vlookup_range()服用byval vba 范围'search>'search'返回修改的范围对象,以= sumproducts使用(vlookup_range(H2:i2; a2:b11; 2); h3:i3),但也修改了表(键由值替换)。

我的猜测是,一个范围仅包含坐标和表键。

是否可以使用像我的示例这样的函数,也许与另一种对象类型一样?

Option VBASupport 1
Option Compatible

Function VLOOKUP_RANGE(ByVal range_to_modify, ref_range, index As Integer, Optional sorted As Boolean = False)
    calc_built_in = getProcessServiceManager().createInstance("com.sun.star.sheet.FunctionAccess")
    sheet = ThisComponent.getCurrentController.ActiveSheet
    'VBA Range to LibreOffice
    lo_ref_range = sheet.getCellRangebyName(ref_range.Address)
    
    Dim sum
    For Each cell In range_to_modify
        If cell.Value <> 0 Then
            cell.Value = calc_built_in.callFunction("VLOOKUP", Array(cell.Value, lo_ref_range, index, sorted))
        End If
    Next
    
    VLOOKUP_RANGE = range_to_modify
End Function

I want my VLOOKUP_RANGE() taking a ByVal VBA Range 'search' to return a modified Range object, to use like =SUMPRODUCT(VLOOKUP_RANGE(H2:I2;A2:B11;2);H3:I3) but the sheet is modified too (the keys are replace by the values).

My guess is that a Range contains only coords and a sheet key.

Is it possible to do use a function like my example, maybe with another object type ?

Option VBASupport 1
Option Compatible

Function VLOOKUP_RANGE(ByVal range_to_modify, ref_range, index As Integer, Optional sorted As Boolean = False)
    calc_built_in = getProcessServiceManager().createInstance("com.sun.star.sheet.FunctionAccess")
    sheet = ThisComponent.getCurrentController.ActiveSheet
    'VBA Range to LibreOffice
    lo_ref_range = sheet.getCellRangebyName(ref_range.Address)
    
    Dim sum
    For Each cell In range_to_modify
        If cell.Value <> 0 Then
            cell.Value = calc_built_in.callFunction("VLOOKUP", Array(cell.Value, lo_ref_range, index, sorted))
        End If
    Next
    
    VLOOKUP_RANGE = range_to_modify
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何止钟意 2025-02-11 08:06:55

在libreoffice中,可以写下这样的功能,例如,

Option Explicit 

Function VLOOKUP_RANGE(range_to_modify As Variant, ref_range As Variant, index As Integer) As Variant 
Dim i As Long, j As Long, m As Long

    If index < LBound(ref_range,2) Or index > UBound(ref_range,2) Then Exit Function 

    For i = LBound(range_to_modify,1) To UBound(range_to_modify,1)
        For j = LBound(range_to_modify,2) To UBound(range_to_modify,2)
            For m = LBound(ref_range,1) To UBound(ref_range,1)
                If range_to_modify(i, j) = ref_range(m, 1) Then
                    range_to_modify(i, j) = ref_range(m, index)
                    Exit For 
                EndIf 
            Next m
        Next j
    Next i
    VLOOKUP_RANGE = range_to_modify
End Function

如评论中所建议的那样,最好使用由Calc的内置功能组成的公式。

In LibreOffice, such a function can be written, for example, like this:

Option Explicit 

Function VLOOKUP_RANGE(range_to_modify As Variant, ref_range As Variant, index As Integer) As Variant 
Dim i As Long, j As Long, m As Long

    If index < LBound(ref_range,2) Or index > UBound(ref_range,2) Then Exit Function 

    For i = LBound(range_to_modify,1) To UBound(range_to_modify,1)
        For j = LBound(range_to_modify,2) To UBound(range_to_modify,2)
            For m = LBound(ref_range,1) To UBound(ref_range,1)
                If range_to_modify(i, j) = ref_range(m, 1) Then
                    range_to_modify(i, j) = ref_range(m, index)
                    Exit For 
                EndIf 
            Next m
        Next j
    Next i
    VLOOKUP_RANGE = range_to_modify
End Function

However, it is preferable to use formulas made up of Calc's built-in functions, as suggested in a comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文