在子字符串数组中搜索特定字符串(VBA)
我正在编写代码来处理传入的电子邮件。大多数方面都工作正常;然而,日期的处理给我带来了一些麻烦。我在 Module1 中定义的 EvaluateDate 函数无法正常工作。当我运行它时没有错误,只是没有输出。 Tabl 是子字符串数组。传入的电子邮件按行分成子字符串。因此,基本上数组的每个索引都是电子邮件中的一行。我希望搜索特定月份,然后为一月指定“01/”,依此类推。传入电子邮件的日期为“2011 年 10 月 20 日星期四”,并且希望在“10/20/11”处理。一切都是 String 类型。任何帮助将不胜感激。如果您需要更多其他代码来确定问题的根源,请告诉我。谢谢。
在 Sheet 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
在 Module1 中,
'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
I am writing code to process incoming e-mails. Most aspects are working correctly; however, processing of the date is giving me some trouble. The EvaluateDate function I defined in Module1 is not working properly. There is no error when I run it, there is just no output. Tabl is an array of substrings. Incoming e-mails are split into substrings by line. So, basically each index of the array is a line from the e-mail. I am looking to search for a particular month and then assign "01/" for January and so forth. Incoming emails are as such "Thursday, October 20, 2011" and would like to be processed to "10/20/11". Everything is of the String type. Any help would be greatly appreciated. Let me know if you need more of the other code to determine the source of the problem. Thanks.
In Sheet 1 code,
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
In Module1,
'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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几点:
1) 在 VBA 函数中,您需要使用函数名称分配返回值。您的代码缺少这样的内容:
此外,EvaluateDate 的返回值可以是字符串:
2)您的 Tabl 变量被声明为 Variant,这实际上是正确的,但您认为它是字符串。
这实际上意味着:
您可以共享 Dim 语句,但不能共享声明。
另请注意,您尚未在按钮代码中声明“mybody”或“m”。
3) 当您的代码输入适当的 If 语句并在月份名称上找到匹配项时,您应该退出循环。我会像这样重写 EvaluateDate 中的 For 循环:
4)您的代码中有这一行:
这将导致自动实例化(请参阅 http://www.cpearson.com/excel/classes.aspx 了解为什么这不好)。只需声明该变量,因为您稍后已经在代码中创建了它。
5) 在按钮代码中,您循环遍历电子邮件的每一行,但随后将整个电子邮件传递给 EvaluateDate 函数并再次循环遍历每一行。因此,如果我的数学正确,那么当您只需要循环 n 次时,您将在电子邮件中循环 n*n 次。这真的是你想要的吗?
A few things:
1) In a VBA function, you need to assign a return value using the name of the function. Your code is missing something like this:
Also, the return value for EvaluateDate can be a String:
2) Your Tabl variable is declared as Variant, which is actually correct, but you think it is a String.
this actually means:
You can share a Dim statement, but not a declaration.
Also note that you haven't declared "mybody" or "m" in your button code.
3) When your code enters the appropriate If statement and finds a match on the month name, you should be exiting the loop. I would rewrite the For loop in EvaluateDate like this:
4) You have this line in your code:
This will cause auto-instantiation (see http://www.cpearson.com/excel/classes.aspx for why this is bad). Simply declare the variable, since you are already creating it later in your code.
5) In your button code you loop through each line of the email, but then you pass the whole email to the EvaluateDate function and loop through every line again. So if my math is correct you are looping n*n times through the email when you only need to loop n times. Is that really what you want?