Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用excel vba比较2个工作表列,但仅与第一个点比较_Excel_Vba - Fatal编程技术网

使用excel vba比较2个工作表列,但仅与第一个点比较

使用excel vba比较2个工作表列,但仅与第一个点比较,excel,vba,Excel,Vba,我有一张主数据表和一张日报表,我想将日报表中的电子邮件地址与主数据表(a列)和msgbox中的地址进行比较主数据表中没有的地址列表,问题是我只需要比较到第1个点,但需要消息框中的完整电子邮件地址 比如说 Master file Daily file john.co.uk john.com gim elephant.com jeff.com.org

我有一张主数据表和一张日报表,我想将日报表中的电子邮件地址与主数据表(a列)和msgbox中的地址进行比较主数据表中没有的地址列表,问题是我只需要比较到第1个点,但需要消息框中的完整电子邮件地址

比如说

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\u ell As Range,Sh\u D As Worksheet,Sh\M As Worksheet
尺寸F________________________________
Set Sh_D=Sheets(“”)首先设置活动工作表
'打开主工作簿
出错时继续下一步
如果IsError(工作簿(“”),则
工作簿。打开文件名:=ActiveWorkbook.Path&“\”和“
其他的
工作簿(“”)。激活
如果结束
错误转到0
'设置母版页以供参考
设置Sh_M=图纸(“”)
上海
F_ound=False
对于范围内的每个单元格(“A1”,单元格(Rows.Count,1).End(xlUp))
施穆
对于范围内的每个C_ell2(“A1”,单元格(Rows.Count,1).End(xlUp))
如果InStr(1,C_ell2,Left(C_ell,InStr(1,C_ells,“.”,vbTextCompare)),vbTextCompare)为0,则
F_ound=True
如果结束
下一个
如果没有找到,那么
单元格(Rows.Count,1).结束(xlUp).偏移量(1,0)=C_单元格
如果结束
F_ound=False
下一个

希望它有意义

在这里,我在两张不同的工作表上列出了您的两个电子邮件地址列表,“主”和“每日”

这段代码将两个电子邮件范围存储为数组,然后比较这些数组的元素。您可以使代码更简洁,但我一直保持这种方式,使其更易于阅读和查看发生了什么

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
,因为这可能会导致问题。为了更好的解释。

我们可以看看您尝试了什么吗?很抱歉,这是我迄今为止尝试的方法,found=False用于范围内的每个C_ell2(“A1”,单元格(Rows.count,1)。End(xlUp))Sh_M.激活范围内的每个C_ell2(“A1”,单元格(Rows.count,1)。End(xlUp))如果InStr(1,C_ell2,左(C_ell2,InStr(1,C_-ell,“.”,vbTextCompare)),vbTextCompare)0则F_-ound=真结束如果下一个不F_-ound则结束单元格(Rows.count,1)。结束(xlUp)。偏移量(1,0)=C_-ell结束如果F_-ound=假结束如果F_-ound=下一个
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