动态文本Join与另一个工作簿中的条件查找VBA查找

发布于 2025-01-30 02:59:48 字数 987 浏览 2 评论 0 原文

如何使用VBA代码进行动态文本Join,并从另一个工作簿中查找条件?

我想将此公式转换为vba:

= textjoin(“,”,true,if($ c $ 15 = [export.xlsx] sheet1!$ e $ 2:$ e $ 13047,[export.xlsx] sheet1! $ 2:$ a $ 13047,“”))

当前这是我到目前为止所做的VBA代码,并且正在弹出错误,说明“运行时错误'91':对象变量或block变量未设置”

Sub join()

Dim exportWb As Workbook
Dim exportWs As Worksheet
Set exportWs = exportWb.Sheets("Sheet1")

Workbooks.Open ("C:\Users\desktop\export.xlsx")
Set exportWb = ActiveWorkbook
ThisWorkbook.Activate

Dim exportlastRow1 As Range
Dim exportlastRow2 As Range

exportlastRow1 = exportWs.Cells(exportWs.Rows.Count, "A").End(xlUp).Row
exportlastRow2 = exportWs.Cells(exportWs.Rows.Count, "E").End(xlUp).Row

Dim existSAPID As String
existSAPID = ActiveSheet.Evaluate("TEXTJOIN(", ",TRUE,IF(C15 = 
exportlastRow2,exportlastRow1,""))")

End Sub

How do i work up the vba code for a Dynamic textjoin with IF condition lookup from another workbook?

I would like to convert this formula into a vba:

=TEXTJOIN(", ",TRUE,IF($C$15=[export.XLSX]Sheet1!$E$2:$E$13047,[export.XLSX]Sheet1!$A$2:$A$13047,""))

Currently this is the vba code i have done so far and it is popping up error stating "Run-time error '91': Object variable or With block variable not set"

Sub join()

Dim exportWb As Workbook
Dim exportWs As Worksheet
Set exportWs = exportWb.Sheets("Sheet1")

Workbooks.Open ("C:\Users\desktop\export.xlsx")
Set exportWb = ActiveWorkbook
ThisWorkbook.Activate

Dim exportlastRow1 As Range
Dim exportlastRow2 As Range

exportlastRow1 = exportWs.Cells(exportWs.Rows.Count, "A").End(xlUp).Row
exportlastRow2 = exportWs.Cells(exportWs.Rows.Count, "E").End(xlUp).Row

Dim existSAPID As String
existSAPID = ActiveSheet.Evaluate("TEXTJOIN(", ",TRUE,IF(C15 = 
exportlastRow2,exportlastRow1,""))")

End Sub

enter image description here

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

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

发布评论

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

评论(1

我还不会笑 2025-02-06 02:59:48

VBA代码的公式

Sub JoinMatches()

    ' Source
    Const sFilePath As String = "C:\Test\T2022\72284158\export.xlsx"
    Const sName As String = "Sheet1"
    Const sLookupFirstCellAddress As String = "E2"
    Const sJoinColumn As String = "A"
    ' Destination
    Const dName As String = "Credit Assessment"
    Const dLookupCellAddress As String = "C15"
    Const dJoinCellAddress As String = "C10"
    
    ' Reference the source (lookup and join) ranges.
    
    Dim sFileName As String: sFileName = Dir(sFilePath)
    If Len(sFileName) = 0 Then
        MsgBox "The file '" & sFilePath & "' doesn't exist.", vbCritical
        Exit Sub
    End If
    
    Dim swb As Workbook: Set swb = Workbooks.Open(sFilePath)
    Dim sws As Worksheet: Set sws = swb.Worksheets(sName)
    
    Dim slrg As Range
    Dim sjrg As Range
    
    With sws.Range(sLookupFirstCellAddress)
        
        Dim lCell As Range
        Set lCell = .Resize(sws.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , , xlPrevious)
        If lCell Is Nothing Then
            MsgBox "No data in column range.", vbCritical
            Exit Sub
        End If
        
        Set slrg = .Resize(lCell.Row - .Row + 1)
        Set sjrg = slrg.EntireRow.Columns(sJoinColumn)
    
    End With
    
    ' Reference the destination join cell.
    
    Dim dwb As Workbook: Set dwb = ThisWorkbook ' workbook containing this code
    Dim dws As Worksheet: Set dws = dwb.Worksheets(dName)
    Dim djCell As Range: Set djCell = dws.Range(dJoinCellAddress)
    
    ' Evaluate.
    djCell.Value _
        = dws.Evaluate("TEXTJOIN("", "",TRUE,IF(" & dLookupCellAddress _
        & "='[" & sFileName & "]" & sName & "'!" & slrg.Address _
        & ",'[" & sFileName & "]" & sName & "'!" & sjrg.Address & ",""""))")
    
    ' Close.
    'swb.Close SaveChanges:=False
    
    ' Inform.
    MsgBox "Data joined.", vbInformation

End Sub

Formula to VBA Code

Sub JoinMatches()

    ' Source
    Const sFilePath As String = "C:\Test\T2022\72284158\export.xlsx"
    Const sName As String = "Sheet1"
    Const sLookupFirstCellAddress As String = "E2"
    Const sJoinColumn As String = "A"
    ' Destination
    Const dName As String = "Credit Assessment"
    Const dLookupCellAddress As String = "C15"
    Const dJoinCellAddress As String = "C10"
    
    ' Reference the source (lookup and join) ranges.
    
    Dim sFileName As String: sFileName = Dir(sFilePath)
    If Len(sFileName) = 0 Then
        MsgBox "The file '" & sFilePath & "' doesn't exist.", vbCritical
        Exit Sub
    End If
    
    Dim swb As Workbook: Set swb = Workbooks.Open(sFilePath)
    Dim sws As Worksheet: Set sws = swb.Worksheets(sName)
    
    Dim slrg As Range
    Dim sjrg As Range
    
    With sws.Range(sLookupFirstCellAddress)
        
        Dim lCell As Range
        Set lCell = .Resize(sws.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , , xlPrevious)
        If lCell Is Nothing Then
            MsgBox "No data in column range.", vbCritical
            Exit Sub
        End If
        
        Set slrg = .Resize(lCell.Row - .Row + 1)
        Set sjrg = slrg.EntireRow.Columns(sJoinColumn)
    
    End With
    
    ' Reference the destination join cell.
    
    Dim dwb As Workbook: Set dwb = ThisWorkbook ' workbook containing this code
    Dim dws As Worksheet: Set dws = dwb.Worksheets(dName)
    Dim djCell As Range: Set djCell = dws.Range(dJoinCellAddress)
    
    ' Evaluate.
    djCell.Value _
        = dws.Evaluate("TEXTJOIN("", "",TRUE,IF(" & dLookupCellAddress _
        & "='[" & sFileName & "]" & sName & "'!" & slrg.Address _
        & ",'[" & sFileName & "]" & sName & "'!" & sjrg.Address & ",""""))")
    
    ' Close.
    'swb.Close SaveChanges:=False
    
    ' Inform.
    MsgBox "Data joined.", vbInformation

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