我寫代碼來處理傳入電子郵件的陣列。大多數方面工作正常;然而,處理日期給了我一些麻煩。我在Module1中定義的EvaluateDate函數無法正常工作。我運行它時沒有錯誤,只是沒有輸出。 Tabl是一個子串的數組。傳入的電子郵件按行分割爲子字符串。所以,基本上數組的每個索引都是來自電子郵件的一行。我正在尋找一個特定的月份,然後爲1月分配「01 /」等等。傳入的電子郵件是這樣的「2011年10月20日星期四」,並希望被處理爲「10/20/11」。一切都是字符串類型。任何幫助將不勝感激。讓我知道是否需要更多的其他代碼來確定問題的根源。謝謝。搜索串用於特定字符串(VBA)
在表1級的代碼,
Public Sub CommandButton1_Click()
Dim olApp As New Outlook.Application
Dim olExp As Outlook.Explorer
Dim olSel As Outlook.Selection
Dim myArray(8) As String
Dim Line As Long, Addr1 As String
Dim Tabl, str As String
Dim index As Integer
Dim I As Integer, x As Integer, N As Integer, j As Integer
Sheets("EditData").Select
Columns("D:D").NumberFormat = "@"
'Selection.NumberFormat = "@"
On Error Resume Next
' Getting the messages selection
Set olApp = Outlook.Application
Set olExp = olApp.ActiveExplorer
Set olSel = olExp.Selection
' Checking if there is at least one message selected
If olSel.Count < 1 Then
MsgBox "No message selected", vbExclamation, "Error"
Exit Sub
End If
With Sheets("EditData")
' Retrieving the first avaible row to put message in
Line = .Range("D65000").End(xlUp).Row + 1
' looping through message
For x = 1 To olSel.Count
DoEvents
Erase myArray
mybody = Replace(olSel.Item(x).body, Chr(13), "")
' Splitting the message body into an array of substrings,
' using the "line feed" characters as separators
mybody = Replace(mybody, Chr(10) & Chr(10), Chr(10))
Tabl = Split(mybody, Chr(10))
For Each Item In Tabl
Item = Replace(Item, Chr(10), "")
Item = Application.Clean(Item)
Next Item
' Looping through these substrings
For I = 0 To UBound(Tabl)
' Date Received Start
If LCase(Left(Tabl(I), 4)) = "sent" Then
m = Module1.EvaluateDate(Tabl)
.Cells(Line, 2) = m
End If
Next I
Next X
End With
End Sub
在模塊1,
'Function to determine the month, day, and year in this format mm/dd/yy
Public Function EvaluateDate(Tabl As Variant) As Variant
For I = 0 To UBound(Tabl)
If InStr(1, Tabl(I), "January", 1) > 0 Then
m = "01/"
End If
If InStr(1, Tabl(I), "February", 1) > 0 Then
m = "02/"
End If
If InStr(1, Tabl(I), "March", 1) > 0 Then
m = "03/"
End If
If InStr(1, Tabl(I), "April", 1) > 0 Then
m = "04/"
End If
If InStr(1, Tabl(I), "May", 1) > 0 Then
m = "05/"
End If
If InStr(1, Tabl(I), "June", 1) > 0 Then
m = "06/"
End If
If InStr(1, Tabl(I), "July", 1) > 0 Then
m = "07/"
End If
If InStr(1, Tabl(I), "August", 1) > 0 Then
m = "08/"
End If
If InStr(1, Tabl(I), "September", 1) > 0 Then
m = "09/"
End If
If InStr(1, Tabl(I), "October", 1) > 0 Then
m = "10/"
End If
If InStr(1, Tabl(I), "November", 1) > 0 Then
m = "11/"
End If
If InStr(1, Tabl(I), "December", 1) > 0 Then
m = "12/"
End If
Next I
EvaluateDate = m
End Function
請修復與相應的格式化你的代碼。 – Polynomial
vb6,vba,vbscript或vb.net? –