Forum Discussion
Is there a way to have a dynamic textjoin with IF condition lookup from another workbook VBA
- May 19, 2022
Sorry
Sub join() Dim ws As Worksheet Dim exportWb As Workbook Dim exportWs As Worksheet Dim exportlastRow As Long Set ws = Sheet1 Set exportWb = Workbooks.Open("C:\Users\desktop\export.xlsx") Set exportWs = exportWb.Sheets("Sheet1") exportlastRow = exportWs.Cells(exportWs.Rows.Count, "E").End(xlUp).Row ThisWorkbook.Activate ws.Range("C10") = ws.Evaluate("TEXTJOIN("", "",TRUE,IF([export.XLSX]Sheet1!E2:E" & _ exportlastRow & "=C15,[export.XLSX]Sheet1!A2:A" & exportlastRow & ",""""))") End Sub
You use exportWb before you have assigned a value to it. This can easily be corrected by moving down the line
Set exportWs = exportWb.Sheets("Sheet1")
But the 3rd argument of TEXTJOIN makes no sense:
IF(C15=exportlastRow2,exportlastRow1,"")
does not evaluate to anything that can be concatenated.
(And the use of quotes is wrong, but that's for later)
What are you trying to do?
- hrh_dashMay 19, 2022Iron Contributor
HansVogelaar , previously i will using this formula below and now i would like it to be executing via macro.
TEXTJOIN(", ",TRUE,IF($C$15=[export.XLSX]Sheet1!$E$2:$E$13047,[export.XLSX]Sheet1!$A$2:$A$13047,""))
- HansVogelaarMay 19, 2022MVP
Try this:
Sub join() Dim ws As Worksheet Dim exportWb As Workbook Dim exportWs As Worksheet Dim exportlastRow As Range Set ws = Sheet1 Set exportWb = Workbooks.Open("C:\Users\desktop\export.xlsx") Set exportWs = exportWb.Sheets("Sheet1") exportlastRow = exportWs.Cells(exportWs.Rows.Count, "E").End(xlUp).Row ThisWorkbook.Activate ws.Range("C10") = ws.Evaluate("TEXTJOIN("", "",TRUE,IF([export.XLSX]Sheet1!E2:E" & _ exportlastRow & "=C15,[export.XLSX]Sheet1!A2:A" & exportlastRow & ",""""))") End Sub
- hrh_dashMay 19, 2022Iron Contributor
HansVogelaar , vba is showing an error - "Run-time error '91' Object variable or with block variable not set
Should i have 2 exportlastRow1 and exportlastRow2 as i have 2 different columns; A and E?
and
Should the C15 be right after IF function?
Dim exportlastRow1 As Range Dim exportlastRow2 As Range Set exportlastRow1 = exportWs.Cells(exportWs.Rows.Count, "E").End(xlUp).Row Set exportlastRow2 = exportWs.Cells(exportWs.Rows.Count, "A").End(xlUp).Row ws.Range("C10") = ws.Evaluate("TEXTJOIN("", "",TRUE,IF(C15 = [export.XLSX]Sheet1!E2:E" & _ exportlastRow1 & ",[export.XLSX]Sheet1!A2:A" & exportlastRow2 & ",""""))")