我有一张主表和每日表,我想将每日表中的电子邮件地址与主表(A列)和msgbox中的地址进行比较,列出任何不在主表上的地址,问题是我只需要比较第一个点,但需要在消息框中的完整电子邮件地址
例如
Master file Daily file
john.co.uk john.com
gim elephant.com
jeff.com.org jeff.co.com
scream.com scream
fish.cpl banana
result in msg box
elephant.com
banana
Dim C_ell As Range, Sh_D As Worksheet, Sh_M As Worksheet
Dim F_ound As Boolean, C_ell2 As Range
Set Sh_D = Sheets("") 'Set the active worksheet first
'Open Master workbook
On Error Resume Next
If IsError(Workbooks("")) Then
Workbooks.Open Filename:=ActiveWorkbook.Path & "\" & ""
Else
Workbooks("").Activate
End If
On Error GoTo 0
'Set the Master sheet for reference
Set Sh_M = Sheets("")
Sh_D.Activate
F_ound = False
For Each C_ell In Range("A1", Cells(Rows.Count, 1).End(xlUp))
Sh_M.Activate
For Each C_ell2 In Range("A1", Cells(Rows.Count, 1).End(xlUp))
If InStr(1, C_ell2, Left(C_ell, InStr(1, C_ell, ".", vbTextCompare)), vbTextCompare) <> 0 Then
F_ound = True
End If
Next
If Not F_ound Then
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = C_ell
End If
F_ound = False
Next
希望它有意义
答案 0 :(得分:0)
在这里,我在两个不同的工作表上提供了两个电子邮件地址列表,&#34; master&#34;和&#34;每日&#34;。
此代码将两个电子邮件范围存储为数组,然后比较这些数组的元素。您可以使代码更简洁,但我保持这种方式,以便更容易阅读和查看正在进行的操作。
Sub test2()
Dim arrMaster() As Variant ' array of emails on 'master' sheet
Dim arrDaily() As Variant ' array of emails on 'daily' sheet
Dim strComp1 As String ' string to compare
Dim strComp2 As String ' string to compare
Dim txt As String ' text to output
Dim counter As Integer
Dim booFound As Boolean
Dim wksMaster As Worksheet ' Master worksheet
Dim wksDaily As Worksheet ' Daily worksheet
Dim i As Integer
Dim j As Integer
Set wksMaster = Worksheets("master")
Set wksDaily = Worksheets("daily")
counter = 0
arrMaster = wksMaster.Range("A1", wksMaster.Range("A" & Rows.Count).End(xlUp))
arrDaily = wksDaily.Range("A1", wksDaily.Range("A" & Rows.Count).End(xlUp))
For i = 1 To UBound(arrDaily())
If InStr(1, arrDaily(i, 1), ".", vbTextCompare) > 0 Then
strComp1 = Left(arrDaily(i, 1), (InStr(1, arrDaily(i, 1), ".", vbTextCompare)) - 1)
Else
strComp1 = arrDaily(i, 1)
End If
For j = 1 To UBound(arrMaster())
If InStr(1, arrMaster(j, 1), ".", vbTextCompare) > 0 Then
strComp2 = Left(arrMaster(j, 1), (InStr(1, arrMaster(j, 1), ".", vbTextCompare)) - 1)
Else
strComp2 = arrMaster(j, 1)
End If
booFound = False
'test if the strings match
If strComp1 = strComp2 Then
booFound = True
Exit For
End If
Next j
If booFound = False Then
'no match was found - create text output
If counter = 0 Then
txt = arrDaily(i, 1)
counter = counter + 1
Else
txt = txt & vbCr & arrDaily(i, 1)
counter = counter + 1
End If
End If
Next i
'output text
MsgBox txt
Set wksMaster = Nothing
Set wksDaily = Nothing
End Sub
您还应避免在代码中使用Activate
,因为它可能会导致问题。 See here有一个很好的解释。