简体   繁体   中英

inserting array formula into excel using vba and referencing cell value exported from outlook email?

I am using a vba code in outlook to export some text from an email body into the next available row in excel. I am also exporting an array formula in the cell next door, so In this instance intRow4 as an integer represents the next available row on my worksheet.

And In this example my next available row is row 34. So we are exporting the description text from our email into cell (D34) in excel and are also inserting an array formula from outlook into excel, cell (E34). The formula is an index lookup formula that references D34 and should lookup the first 5 letters of a word and match it with a 3 letter code from another worksheet.

So on my other worksheet (worksheet 2) I have a list of data in columns

Description     Other      Other       Other     Other     Code
Hotels                                                     NFP
Catering                                                   PLQ
Travel                                                     LMC

so where my email contains a description text like Catering then this will be exported into my excel cell (D34) and the formula which gets inserted into (E34) should find the corresponding description by referencing cell (D34) and matching it to the description in my column on sheet 2. This should then produce the 3 letter code in column 6. In this case it would be PLQ

My Description text in my email looks like:

Description of Provisional Supplier:
Catering

My export code seems to work fine and cell D34 gets populated with the description text from my email. And the formula gets inserted as an array in cell E34.

The problem is my formula is not producing the 3 letter code, it is showing #NUM! error. However if I go into my cell D34 and place my cursor to the left of the text and hit the backspace key and hit enter again then the formula produces the 3 letter code.

I am removing spaces and line breaks from my description string at the point when it gets exported into excel and I can not explain why this is happening.

can someone please show me where I am going wrong and show me how I can get this working without me having to manually go in and edit the contents of my cell. thanks

Const SHEET_NAME4 = "Statistics"
intRow4 As Integer, _
Set excWks4 = excWkb.Worksheets(SHEET_NAME4)
intRow4 = excWks4.UsedRange.Rows.Count + 1


Dim l As String
l = excWks4.Cells(intRow4, 4).Address
excWks4.Cells(intRow4, 5).FormulaArray = "=IF(ISERROR(INDEX('Up'!$A$1:$G$10004,SMALL(IF(LEFT('Up'!$B$1:$B$10004,5)=LEFT(" & l & ",5),ROW($B$1:$B$10004)),ROW(1:1)),6)),"""",INDEX('Up'!$A$1:$G$10004,SMALL(IF(LEFT('Up'!$B$1:$B$10004,5)=LEFT(" & l & ",5),ROW($B$1:$B$10004)),ROW(1:1)),6))"


Dim b7 As String
 If TypeName(olkMsg) = "MailItem" Then
    b7 = olkMsg.Body

    Dim indexOfNameb4 As Integer
        indexOfNameb4 = InStr(UCase(b7), UCase("Description of the provisional Supplier:"))


    Dim indexOfNamec4 As Integer
       indexOfNamec4 = InStr(UCase(b7), UCase("Current Status: "))

    Dim finalStringb4 As String
    Dim LResult3364 As String
    Dim LResult33644 As String


        finalStringb4 = Mid(b7, indexOfNameb4, indexOfNamec4 - indexOfNameb4)

        LResult3364 = Replace(finalStringb4, "Description of the provisional Supplier:", "")
        LResult33644 = Replace(LResult3364, Chr(10), "")

        Dim TrimString As String
        Dim TrimString2 As String
        Dim TrimString3 As String
        TrimString = Trim(LResult33644)
        TrimString3 = Replace(TrimString, " ", "")
        excWks4.Cells(intRow4, 4) = TrimString3


End If

My Excel cells are formatted as general format if this helps

Have you tried adding the line

excWks4.Cells(intRow4, 4) = Trim(excWks4.Cells(intRow4, 4))

after the line

l = excWks4.Cells(intRow4, 4).Address

Alternatively you could may be able to add the TRIM function to your long formula around all references to those cells.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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