如何将 If 条件传递给 Excel VBA 文本连接?

发布于 2025-01-13 00:18:40 字数 560 浏览 0 评论 0原文

如何将 Textjoin 中的 if 条件传递到 VBA 等效项中? IE。我当前在 Excel 中的工作公式是:

=TEXTJOIN("",TRUE,IF(M1:M100=1,N1:N100,""))

如果在 M 列中设置了标志,则这是连接 N 列中的行。

我想要得到的是在 VBA 中复制此内容,例如:

abc = WorksheetFunction.TextJoin(" ", True, IF(Range(M1:M100)=1,Range(N1:N100),"")

但似乎无法弄清楚如何将条件传递给工作表函数。 有什么想法吗?

我想这个问题也与 WorksheetFunction.Filter 有关,关于如何向其传递过滤条件,例如:

 WorksheetFunction.Filter(Range("M:M"), Range("N:N") & "=1", False)

我这么说就好像我可以计算出过滤器一样,我可能可以直接将该范围传递给文本连接。

谢谢!

How does one pass an if condition in Textjoin into the VBA equivalent?
ie. my working formula in Excel currently is:

=TEXTJOIN("",TRUE,IF(M1:M100=1,N1:N100,""))

This is to join rows in column N if the flag is set in column M.

What I am trying to get is to replicate this in VBA, something like:

abc = WorksheetFunction.TextJoin(" ", True, IF(Range(M1:M100)=1,Range(N1:N100),"")

But can't seem to work out how to pass the criteria to the worksheet function.
Any ideas?

I guess the question also relates to WorksheetFunction.Filter, on how you can pass a filter condition to it, something like:

 WorksheetFunction.Filter(Range("M:M"), Range("N:N") & "=1", False)

I say this as if I can work out the filter, I can probably just pass that range to the text join directly.

Thanks!

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

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

发布评论

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

评论(1

单挑你×的.吻 2025-01-20 00:18:40

TEXTJOIN 在 VBA 中使用 If 条件数组

在此处输入图像描述

  • 该图像显示您的公式不太正确。通过引入 If 语句,第二个参数变得无关紧要。您已选择 True 来跳过空白,但它们不会被跳过。您可以使用另一个(嵌套)If 语句来覆盖它。
  • 以下过程中的三个示例展示了如何使用您的想法的 VBA 版本。前两个主要基于 Evaluate 方法,而第三个则使用下面的函数。
  • 我没有 Office 365,因此您可以期待使用它包含的这些新功能的另一个答案。
Option Explicit

Sub Test()
    With ActiveSheet
        
        ' 1.) Evaluate
        Debug.Print .Evaluate("=TEXTJOIN("""",FALSE," _
            & "IF(M1:M100=1,IF(N1:N100="""","""",N1:N100),""""))")
        
        ' 2.) Worksheet Function + Evaluate
        Debug.Print WorksheetFunction.TextJoin("", False, _
            .Evaluate("IF(M1:M100=1,IF(N1:N100="""","""",N1:N100),"""")"))
        
        ' 3.) Function (UDF)
        Debug.Print TextJoinIf(.Range("M1:N100"), 2, 1, 1, "")
    
    End With
End Sub
  • 您可以在 VBA 中使用以下函数,或者如果您拥有 2019 年之前的 Office,也可以将其用作 UDF。假设跳过空格。
Function TextJoinIf( _
    ByVal rg As Range, _
    ByVal ConcatColumn As Long, _
    ByVal FlagColumn As Long, _
    ByVal Flag As Variant, _
    Optional ByVal Delimiter As String = " ") _
As String
    Const ProcName As String = "TextJoinIf"
    On Error GoTo ClearError
    
    Dim Data As Variant: Data = rg.Value
    Dim sr As Long
    Dim rString As String
    For sr = 1 To rg.Rows.Count
        If Data(sr, FlagColumn) = Flag Then
            If Len(Data(sr, ConcatColumn)) > 0 Then
                rString = rString & Data(sr, ConcatColumn) & Delimiter
            End If
        End If
    Next sr
    If Len(rString) > 0 Then
        TextJoinIf = Left(rString, Len(rString) - Len(Delimiter))
    End If

ProcExit:
    Exit Function
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
End Function

TEXTJOIN With an If Conditioned Array in VBA

enter image description here

  • The image shows that your formula isn't quite correct. By introducing the If statement, the second argument has become irrelevant. You have chosen True to skip blanks but they won't be skipped. You can cover for it with another (nested) If statement.
  • The three examples in the following procedure show how you can use a VBA version of your idea. The first two are mostly based on the Evaluate method, while the third uses the function below.
  • I don't have Office 365, so you could expect another answer using those new functions it contains.
Option Explicit

Sub Test()
    With ActiveSheet
        
        ' 1.) Evaluate
        Debug.Print .Evaluate("=TEXTJOIN("""",FALSE," _
            & "IF(M1:M100=1,IF(N1:N100="""","""",N1:N100),""""))")
        
        ' 2.) Worksheet Function + Evaluate
        Debug.Print WorksheetFunction.TextJoin("", False, _
            .Evaluate("IF(M1:M100=1,IF(N1:N100="""","""",N1:N100),"""")"))
        
        ' 3.) Function (UDF)
        Debug.Print TextJoinIf(.Range("M1:N100"), 2, 1, 1, "")
    
    End With
End Sub
  • You can use the following function in VBA or if you have Office prior to 2019, also as a UDF. Skipping blanks is assumed.
Function TextJoinIf( _
    ByVal rg As Range, _
    ByVal ConcatColumn As Long, _
    ByVal FlagColumn As Long, _
    ByVal Flag As Variant, _
    Optional ByVal Delimiter As String = " ") _
As String
    Const ProcName As String = "TextJoinIf"
    On Error GoTo ClearError
    
    Dim Data As Variant: Data = rg.Value
    Dim sr As Long
    Dim rString As String
    For sr = 1 To rg.Rows.Count
        If Data(sr, FlagColumn) = Flag Then
            If Len(Data(sr, ConcatColumn)) > 0 Then
                rString = rString & Data(sr, ConcatColumn) & Delimiter
            End If
        End If
    Next sr
    If Len(rString) > 0 Then
        TextJoinIf = Left(rString, Len(rString) - Len(Delimiter))
    End If

ProcExit:
    Exit Function
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文