VBA and MID Function to extract strings in Emails...Loop maybe?

Kreiz

New Member
Joined
May 6, 2011
Messages
38
Have my emails populating into Excel and need assistance pulling a substring (8 digit number) out of the string placing in column F, then equipment into G. I think I need loop maybe?

Excel Workbook
ABCDEFGHIJ
1**********
28/26/2011 12:57Tender - FCL - SAN ANGELO, TX - 09/01 04:00Anheuser-Busch, Inc.FALSEORIGINAL LOAD TENDER Carrier: THIS COMPANYBUNCHES OF INFON/A 88888811 THE 8 DIGIT NUMBER I NEED AND IS DRY88888811****
38/26/2011 12:57Tender - FCL - SAN ANGELO, TX - 09/01 03:00Anheuser-Busch, Inc.FALSEORIGINAL LOAD TENDER Carrier: THIS COMPANYBUNCHES OF INFON/A 88888812 THE 8 DIGIT NUMBER I NEED AN IS REEFER88888811****
Sheet6



'need use these for column F and G through VBA, i can only get the first email's number i need entire column.
'=MID($E2,FIND("N/A",$E2)+6,8) =MID($E2,FIND("N/A",$E2)+25,3)

I can get the macro to pull the string needed (8 digit number i.e. '88888811' in this case) from the body of the email for the first row but not the rest of the rows.

Code:
ActiveCell.FormulaR1C1 = "=MID($E2,FIND("n/a",$E2)+6,8)"
I thought the above code would place this into the cell but no.

So I am using tndrID = Mid(E, myloc + 6, 8)

Code:
Sub SplSubjNAutoFillDWN()

Dim Lastrow As Long
Dim G As Range, H As Range
Dim i As Range, J As Range
Dim K As Range, B As Range
Dim c As Range, E As Range
Dim D As Range, F As Range
Dim myloc As Variant, tndrID As String
Dim tndrEQ

Lastrow = ActiveSheet.Range("a:a").Cells.SpecialCells(xlCellTypeConstants).Count + 1

Set D = Range("d2")
Set E = Range("e" & atvrw)
Set E = Range("e2")
Set F = Range("f2:f" & Lastrow&)
Set G = Range("g2")
Set H = Range("h2")
Set i = Range("i2")
Set J = Range("j2")
Set K = Range("k2")
'define location in body to start the search from.
schstr = "n/a"
myloc = Application.WorksheetFunction.Search(schstr, E)
'define Tender ID and Tender EQ from email body
tndrID = Mid(E, myloc + 6, 8)
tndrEQ = Mid(E, myloc + 25, 3)
Application.DisplayAlerts = False
'F.Select
'ActiveCell.FormulaR1C1 = "=MID($E2,FIND("n/a",$E2)+6,8)"
F.Value = tndrID
G.Value = tndrEQ
'need use these for column F and G 
'=MID($E2,FIND("N/A",$E2)+6,8)   =MID($E2,FIND("N/A",$E2)+25,3)

   Columns("B:B").Select
    Selection.TextToColumns Destination:=Range("H1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=True, Comma:=True, Space:=False, Other:=True, OtherChar:= _
        "-", FieldInfo:=Array(Array(1, 9), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)) _
        , TrailingMinusNumbers:=True


   Range("F2:g2").Select
        'Selection.AutoFill Destination:=Range("$F2:g" & Lastrow&)
 Application.DisplayAlerts = False
    Calculate
    
End Sub


This is the code I have modified to find in cell e2 the desired number and place it in column F then the equipment type (dry or reefer) in column G. I believe I've made a mess as it is not placing the MID function into the cell but just the result, so when i fill down the column I am not getting the function to fill down but the extracted string's value.

Have a feeling I need a loop, i am bad with loops......Please assist,
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top