2014-03-31 1 views
1

저는 마스터 시트와 일일 시트가 있습니다. 일일 시트의 전자 메일 주소를 마스터 시트의 주소 (두 열 A)와 msgbox와 비교하고 싶습니다 마스터 시트에없는 임의의 목록, 문제는 내가에만 1 점까지 비교하지만, 메시지 상자 예를with excel vba는 두 개의 워크 시트 열을 비교하지만 첫 번째 점만

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 

h ope 의미가 있습니다

+0

시도해 보셨습니까? –

+0

이 내가 각 C_ell에서 범위 ("A1"셀 (Rows.count, 1) .END (xlUp)) 각 C_ell2에서 범위 Sh_M.Activate (들어 지금까지 F_ound = 거짓 을 시도한 것입니다 죄송합니다InStr (1, C_ell2, Left (C_ell, InStr (1, C_ell, ".", vbTextCompare)), vbTextCompare) <> 0 인 경우 "A1", Cells (Rows.count, 1) .End (xlUp) F_ound = 진정한 종료 다음 만약 그렇지 않은 경우 F_ound 그런 세포 (Rows.count, 1) .END (xlUp) .Offset (1, 0) = C_ell 끝 F_ound = 거짓 다음의 경우 – user3360439

답변

0

두 개의 다른 워크 시트 "master"와 "daily"에 두 개의 전자 메일 주소 목록이 있습니다.

이 코드는 두 개의 전자 메일 범위를 배열로 저장 한 다음 해당 배열의 요소를 비교합니다. 코드를 좀 더 간결하게 만들 수 있지만,이 방법을 사용하여 읽고 이해하기 쉽도록했습니다.

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입니다.

관련 문제