如何在 R1C1 公式中使用变量

问题描述 投票:0回答:1

我在 Stack Overflow 上读过很多关于如何做到这一点的示例。但是,我的语法仍然不正确。该公式按原样运行良好。但是,我试图在添加或删除列或行时使公式更加动态。我已经捕获了所需列和行的索引 ID,每个索引 ID 都在自己的变量中。

我只是尝试用包含索引 ID 的两个变量替换 R1C1 表示法。一切都顺利,直到我尝试这个。我想我在某个地方弄乱了语法,或者更糟。我只将变量应用于公式的前半部分。我想我会让它发挥作用,然后继续处理公式的其余部分。最终,我将用定义最后一行和最后一列的变量替换偏移的宽度和高度,使整个公式动态化(或者我希望如此)。

targetSheet 是我要应用公式的工作表。

aCellr 是行索引 ID。 aCellc 是列索引 ID。

`Sub somesub
    Dim strSearch As String
    Dim aCellc As Range
    Dim aCellr As Range
    Dim targetSheet As Worksheet
    Dim sourceSheet As Worksheet

    Set sourceSheet = dataWorkbook.Worksheets("Glass Schedule")
    Set targetSheet = ThisWorkbook.Worksheets("QC")

    '---------------------------------------------------------------------------
    'Find Column Index ID
    '---------------------------------------------------------------------------

    With sourceSheet

        strSearch = "Opening #"
        Set aCellc = .Range("A:M").Find(What:=strSearch, LookIn:=xlValues, _
        lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlDown, _
        MatchCase:=False, SearchFormat:=False)
        
    End With

    If Not aCellc Is Nothing Then
        MsgBox (strSearch & " found in column " & aCellc.Column)
    Else
        MsgBox (strSearch & " not found")
    End If
    
    '---------------------------------------------------------------------------
    'Find Row Index ID
    '---------------------------------------------------------------------------
    
    With sourceSheet
    
        strSearch = "Opening #"
        Set aCellr = .Range("A:M").Find(What:=strSearch, LookIn:=xlValues, _
        lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlDown, _
        MatchCase:=False, SearchFormat:=False)
        
    End With
    
    If Not aCellr Is Nothing Then
        MsgBox (strSearch & " found in row " & aCellr.Row)
    Else
        MsgBox (strSearch & " not found")
    End If

    ' Comment: Original formula works fine
    
'        .Range("A7").Formula2R1C1 = "=FILTER(FILTER(OFFSET('[" & tgtSheet & "]Glass Schedule'!R8C3,0,0,3001,16),'[" & tgtSheet & "]Glass Schedule'!R8C4:R3008C4<>""""),{1,1,1,1,0,0,1,1,0,0,0,0,0,0,1,1})"

    ' Comment: New formula inserted with the variables does not work.
    
        .Range("A7").Formula2R1C1 = "=FILTER(FILTER(OFFSET('[" & tgtSheet & "]Glass Schedule'!R[" & aCellr & "]c [" & aCellc & "],0,0,3001,16),'[" & tgtSheet & "]Glass Schedule'!R8C4:R3008C4<>""""),{1,1,1,1,0,0,1,1,0,0,0,0,0,0,1,1})"
    
    End With

End Sub`
excel variables excel-r1c1-notation
1个回答
0
投票

发现我的错误。

'Added the following variables:

Dim rowStart As Integer
Dim colStart As Integer

'Set Variables:
colStart = aCellc.Find(strSearch).Offset(0, 0).Column
rowStart = aCellc.Find(strSearch).Offset(1, 0).Row

'Changed the following line of code:

    .Range("A7").Formula2R1C1 = "=FILTER(FILTER(OFFSET('[" & tgtSheet & "]Glass Schedule'!R" & rowStart & "C" & colStart - 1 & ",0,0,3001,16),'[" & tgtSheet & "]Glass Schedule'!R" & rowStart & "C" & colStart & ":R3008C4<>""""),{1,1,1,1,0,0,1,1,0,0,0,0,0,0,1,1})"
© www.soinside.com 2019 - 2024. All rights reserved.