Error Passing Formula from Access to Excel (1 Viewer)

pooldead

Registered User.
Local time
Yesterday, 23:24
Joined
Sep 4, 2019
Messages
136
I wrote the following formula in Excel originally in order to concatenate a bunch of data.
Code:
=TEXTJOIN(";",TRUE,IF(A:A=A2,B:B,""))
I am trying to pass the formula into Excel from Access in order to run the concatenation on selected files. However I keep getting an "Application-defined or object-defined error".
Code:
Dim xlFormula as String
Dim xlSheet as Excel.Worksheet

xlFormula = "=TEXTJOIN("""";"""",TRUE,IF(A:A=A2,B:B,""""))"
With xlSheet
    .range("C2").formula = xlFormula
End With
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:24
Joined
Sep 21, 2011
Messages
14,238
You have to set the xlsheet object as well as the excel app object?
 

pooldead

Registered User.
Local time
Yesterday, 23:24
Joined
Sep 4, 2019
Messages
136
Sorry, I shortened the code. I do have as my declarations:
Diff:
Dim xlApp As Excel.Application
Dim xlWork As Excel.workbook
Dim xlSheet As Excel.Worksheet
Dim xlFormula As String
Code:
Set xlApp = CreateObject("Excel.Application")
Set xlWork = xlApp.Workbooks.Open(fileName)
Set xlSheet = xlWork.Worksheets(sheetName)
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:24
Joined
Sep 21, 2011
Messages
14,238
Please show ALL the code, otherwise it is just a guessing game.?
 

pooldead

Registered User.
Local time
Yesterday, 23:24
Joined
Sep 4, 2019
Messages
136
My apologies
Code:
Public Function excelModify(fileName As String, sheetName As String)

    Dim xlApp As Excel.Application
    Dim xlWork As Excel.workbook
    Dim xlSheet As Excel.Worksheet
    Dim newPath As String, fileNameWOPath As String, fileNameWOExt As String, path As String, xlFormula As String
    
    xlFormula = "=TEXTJOIN("""";"""",TRUE,IF(A:A=A2,B:B,""""))"

    Set xlApp = CreateObject("Excel.Application")
        Set xlWork = xlApp.Workbooks.Open(fileName)
            xlApp.Visible = False
            
            Set xlSheet = xlWork.Worksheets(sheetName)
                With xlSheet
                    .Range("C1").Formula2R1C1 = "Workgroup_Members"
                    .Range("C2").Formula = xlFormula
                    .Range("C2").AutoFill Destination:=.Range("C2:C" & .Range("B" & .Rows.Count).End(xlUp).Row)
'                        .AutoFill Destination:=Range("C2:C" & .Range("B" & .Rows.Count).End(xlUp).Row)
'                    .Range("C2:C" & .Range("B" & .Rows.Count).End(xlUp).Row) = "=TEXTJOIN(';',TRUE,IF(A:A=A2,B:B,""))"
                    .Range("C2", .Range("C2").End(xlDown)).Copy
                    .Range("D2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    .Columns("B:C").Delete shift:=xlToLeft
                    .Range("A1:B" & .Range("B" & .Rows.Count).End(xlUp).Row).RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
                    .Range("A1").Select
                End With
            Set xlSheet = Nothing
            
            fileNameWOPath = Right(fileName, Len(fileName) - InStrRev(fileName, "\"))
            fileNameWOExt = Left(fileNameWOPath, InStr(fileNameWOPath, ".") - 1)
            path = Left(fileName, InStrRev(fileName, "\"))
            
            newPath = path & fileNameWOExt & "_Edited.csv"
            
            xlWork.SaveAs newPath
        Set xlWork = Nothing
        
        xlApp.Quit
    Set xlApp = Nothing

End Function
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:24
Joined
Sep 21, 2011
Messages
14,238
Try setting that xlFormula in the immediate window. I get an error.
I think it is confused with all the quotes?
1622135617204.png
 

Attachments

  • 1622135539022.png
    1622135539022.png
    9.4 KB · Views: 390

Gasman

Enthusiastic Amateur
Local time
Today, 07:24
Joined
Sep 21, 2011
Messages
14,238
I think you need this for the formula?
Code:
xlFormula = "=TEXTJOIN("";"",TRUE,IF(A:A=A2,B:B,""""))"

TEST it in the immediate window as it is not recognised in 2007
 

pooldead

Registered User.
Local time
Yesterday, 23:24
Joined
Sep 4, 2019
Messages
136
I received the same error you did when I tested in Immediate
 

pooldead

Registered User.
Local time
Yesterday, 23:24
Joined
Sep 4, 2019
Messages
136
Yeah, when I post it like that in Excel it works fine. It's when I try to translate it from Access that it goes haywire. I tried running the string you posted in Immediate and it error'd out
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:24
Joined
Sep 21, 2011
Messages
14,238
Yeah, when I post it like that in Excel it works fine. It's when I try to translate it from Access that it goes haywire. I tried running the string you posted in Immediate and it error'd out
So in that picture in post #9, is that not the correct syntax?
 

pooldead

Registered User.
Local time
Yesterday, 23:24
Joined
Sep 4, 2019
Messages
136
The syntax is correct, but Access is producing that error on the formula when I try to run it. The only difference between the pic and Access is having an extra set of double quotes around ";" in order to define it as a string when the formula gets passed into Excel
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:24
Joined
Sep 21, 2011
Messages
14,238
But I pasted that TEXTJOIN string using your code into an empty worksheet using Access? :unsure:
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:24
Joined
May 7, 2009
Messages
19,230
.Range("C2:C" & .Range("B" & .Rows.Count).End(xlUp).Row) = "=TEXTJOIN(';',TRUE,IF(A:A=A2,B:B,''))"
 

Users who are viewing this thread

Top Bottom