简体   繁体   English

公式数组中的Excel VBA引用字符串

[英]Excel VBA referencing string in Formula Array

Need help inserting string into formula array to bypass the 255 character limit. 需要帮助将字符串插入公式数组以绕过255个字符限制的帮助。

Sub FindCAHS()

Dim HS As Range
Dim refworkbook As Variant
Dim ref1 As Workbook
Dim ref2 As Worksheet
Dim ref3 As String
Dim lastRow As Long


    refworkbook = Application.GetOpenFilename(".xlsx Files (*.xlsx), *.xlsx", 1, "Select the HS Reference xlsx")
    If refworkbook = False Then Exit Sub

    Set HS = Range("J2")
    Set ref1 = Workbooks.Open(refworkbook)
    Set ref2 = ref1.Worksheets.Item(2)

    lastRow = Sheet1.Range("D" & Rows.Count).End(xlUp).Row

        For j = 2 To lastRow
            If IsEmpty(Cells(j, 10).Value) = True Then
                 ref3 = "'" & ref1.Path & Application.PathSeparator & _
                 "[" & ref1.Name & "]" & ref2.Name & "'"
                HS.Offset(j - 2, 0).FormulaArray = _
                "=vlookup($F$" & j & "&$G$" & j & "&$H$" & j & ",Choose({1,2},""" & ref3 & """!$C:$C&ref3!$D:$D&""" & ref3 & """!$E:$E,""" & ref3 & """!$F2:$F5000),2,0)"
                Else: Exit Sub
            End If
        Next

    ref1.Close False
End Sub

I've tried double-quotes ("" & ref3 & "") and double-double-quotes ("""" & ref3 & """") and I keep getting an application-defined or object-defined error. 我已经尝试使用双引号(“”&ref3&“”)和双双引号(“”“”&ref3&“”“”),但不断收到应用程序定义或对象定义的错误。 I feel like this is an easy fix, but I can't seem to figure it out. 我觉得这很容易解决,但似乎无法解决。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

You can't get around a property's character limit by setting the string in VBA instead of through the UI. 您无法通过在VBA中而不是通过UI设置字符串来绕过属性的字符限制

If the length of your string is truly beyond 255-characters, it will continue to fail. 如果字符串的长度确实超过255个字符,则它将继续失败。 As an alternative, you may control the length of the formula better if you use the Names manager to define a named range pointing to your external workbook , and reference that range in your formula. 或者,如果使用名称管理器定义指向外部工作簿命名范围并在公式中引用该范围,则可以更好地控制公式的长度。 Ie: 即:

ActiveWorkbook.Names.Add Name:="MyRange", RefersTo:=Rng1 

I also agree with JohnRC's comments above - be precise and qualify your references to aid in debugging. 我也同意JohnRC的上述评论-准确并有资格参考您的内容以帮助调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM