VBA VLOOKUP 公式引用另一个工作表中的动态范围

发布于 2025-01-15 00:56:09 字数 1639 浏览 3 评论 0原文

VBA VLOOKUP 公式引用另一个工作表中的动态范围

嗨, 我正在尝试将 VLOOKUP 公式合并到名为“RESULTS”的工作表中,并通过 VBA 在另一个名为“DATA”的工作表中引用动态数据范围 - 我的公式工作正常,但它不是动态的,我无法复制它在宏观上,因此,鉴于我有限的 VBA 技能,我遇到了一些障碍,因此任何建议和指导将不胜感激。

这是我想要复制以在宏中使用的公式,但正如前面提到的,它是一个固定范围,我需要它是动态的:

=IF(IFERROR(VLOOKUP(A2,IF({1,0},DATA!$C$5:$C$20000,DATA!$B$5:$B$20000),2,0),"")= 0,"",IFERROR(VLOOKUP(A2,IF({1,0},DATA!$C$5:$C$20000,DATA!$B$5:$B$20000),2,0),""))

这是我编写的 VBA - 只是 VLOOKUP 公式不起作用,COUNTIFS 公式工作正常。

Sub DATA_Report()
        
    'Add in formulae
    Dim DataLastRow As Long
    Dim ResultsLastRow As Long

    'Determine last row of DATA Scheme Information sheet
     With Sheets("DATA")
        DataLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    'Determine last row in col B for RESULTS sheet - Add in formula - MEMBER REFERENCE
    With Sheets("RESULTS")
    ResultsLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        'The following formula is not working
        .Range("G2:G" & .Cells(.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = "=IF(IFERROR(VLOOKUP(RC[-6],IF({1,0}," & "DATA!R5C3:R" & DataLastRow & ",DATA!R5C2:R" & DataLastRow & "),2,0)," & """"")=0," & """""," & "IFERROR(VLOOKUP(RC[-6],IF({1,0}," & "DATA!R5C3:R" & DataLastRow & ",DATA!R5C2:R" & DataLastRow & "),2,0),""""))"
        'The following COUNTIFS formula works fine
        .Range("H2:AA" & ResultsLastRow).FormulaR1C1 = "=IF(COUNTIFS(DATA!R5C2:R" & DataLastRow & "C2,RC7,DATA!R5C1:R" & DataLastRow & "C1,R1C)>0,""Yes"","""")"
    End With
End Sub

预先非常感谢, TE

VBA VLOOKUP formula to refer to a dynamic range in another sheet

Hi,
I am trying to incorporate a VLOOKUP formula into a worksheet called "RESULTS" and reference a dynamic data range in another sheet called "DATA" via VBA - the formula I have works fine but it is not dynamic and I have not been unable to replicate it in the macro, as such, given my limited VBA skills, I have hit a bit of a brick wall so any adice and guidance would be greatly appreciated.

This is the formula I want to replicate for use in the macro but, as mentioned, it is a fixed range and I need it to be dynamic:

=IF(IFERROR(VLOOKUP(A2,IF({1,0},DATA!$C$5:$C$20000,DATA!$B$5:$B$20000),2,0),"")=0,"",IFERROR(VLOOKUP(A2,IF({1,0},DATA!$C$5:$C$20000,DATA!$B$5:$B$20000),2,0),""))

Here is the VBA I have written - it is just the VLOOKUP formula that isn't working, the COUNTIFS formula is working fine.

Sub DATA_Report()
        
    'Add in formulae
    Dim DataLastRow As Long
    Dim ResultsLastRow As Long

    'Determine last row of DATA Scheme Information sheet
     With Sheets("DATA")
        DataLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    'Determine last row in col B for RESULTS sheet - Add in formula - MEMBER REFERENCE
    With Sheets("RESULTS")
    ResultsLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        'The following formula is not working
        .Range("G2:G" & .Cells(.Rows.Count, "A").End(xlUp).Row).FormulaR1C1 = "=IF(IFERROR(VLOOKUP(RC[-6],IF({1,0}," & "DATA!R5C3:R" & DataLastRow & ",DATA!R5C2:R" & DataLastRow & "),2,0)," & """"")=0," & """""," & "IFERROR(VLOOKUP(RC[-6],IF({1,0}," & "DATA!R5C3:R" & DataLastRow & ",DATA!R5C2:R" & DataLastRow & "),2,0),""""))"
        'The following COUNTIFS formula works fine
        .Range("H2:AA" & ResultsLastRow).FormulaR1C1 = "=IF(COUNTIFS(DATA!R5C2:R" & DataLastRow & "C2,RC7,DATA!R5C1:R" & DataLastRow & "C1,R1C)>0,""Yes"","""")"
    End With
End Sub

Many thanks in advance,
TE

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文