Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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
Vba 检查不同工作表中的重复项,然后打印值_Vba_Excel - Fatal编程技术网

Vba 检查不同工作表中的重复项,然后打印值

Vba 检查不同工作表中的重复项,然后打印值,vba,excel,Vba,Excel,我已经搜索过了,但我似乎不知道如何在找到副本的列旁边打印指定的值。我之前的代码首先指定了不同的范围,然后查看是否在活页X的活页Y中发现了重复项。活页Le是本周信息,活页Be是最后一周信息 如果我在指定范围内发现一个副本,我想在我的交货单上打印在副本旁边的a列中,根据我从函数compareAEO print的输出是真是假,可以是已交货,也可以是未交货 我要寻找的条件是,如果我们能找到表(Be)上表(Le)中B列中的相同值,那么它将检查F列中的文本是否已更改。如果是,则应在表(Le)=交付的A栏中打

我已经搜索过了,但我似乎不知道如何在找到副本的列旁边打印指定的值。我之前的代码首先指定了不同的范围,然后查看是否在活页X的活页Y中发现了重复项。活页Le是本周信息,活页Be是最后一周信息

如果我在指定范围内发现一个副本,我想在我的交货单上打印在副本旁边的a列中,根据我从函数compareAEO print的输出是真是假,可以是已交货,也可以是未交货

我要寻找的条件是,如果我们能找到表(Be)上表(Le)中B列中的相同值,那么它将检查F列中的文本是否已更改。如果是,则应在表(Le)=交付的A栏中打印。否则不交付

然后向se检查M列中的日期是否相同。如果没有,则应在活页(Le)的A列中打印重新规划

不久 如果B列表格(Le)单元格中的值=B列表格(Be)中的值,则 活页Le=“已交付”或“未交付”上A列中的值

然后

如果M列单元格中的值为表(Le),如果M列单元格中的值为表(Be),则A列单元格中的值为表(Le)=“重新规划”

我的数据就是这样的, 表(Le)

对于工作表(Be),我的数据如下所示

Col B   Col F    Col M
PZ2408  X30    2017-02-13
PZ2345  X30    2017-02-23
PZ2463  X30    2017-02-25
PZ2513  X13    2017-02-05
PZ2533  X13    2017-02-10
PZ2561  X60    2017-02-17
在代码完成它的过程后,我希望它显示,例如

表(Le)

基本上,我的未交付、交付和重新计划的陈述不起作用,我的大脑也不起作用

这样能帮我度过难关吗

Sub checkASMT()
Dim rng1 As Range
Dim rng2 As Range
Dim lastRowSource As Long
Dim lastRowTarget As Long
Dim row As Long
Dim ASMT As String

'Looping trough Range
With ThisWorkbook.Worksheets("Le")

   lastRowTarget = .Range("B" & .Rows.Count).End(xlUp).row
   For i = 29 To lastRowTarget
        ASMT = .Range("b" & i).value
        'Define range and see if we can find duplicates
        With ThisWorkbook.Worksheets("Be")
            lastRowSource = .Range("B" & .Rows.Count).End(xlUp).row
            Set rng1 = .Range("B3", "B" & lastRowSource)

            row = findValueInRangeReturnRow(rng1, ASMT)


            'Check FAX 
            If compareAEO(i, row, "FAX") = True Then
                'Debug.Print compareASMT(i, row, "FAX")
                Worksheets("Le").Cells(i, ASMT).value = "Not Delivered"

                Else

                .Worksheets("Le").Cells(i, ASMT).value = "delivered"

                'Check if dax are correct
                If compareAEO(i, row, "DAX") = False Then
                .Worksheets("Le").ASMT.Offset(0, 1).value = "Replan"


            End If



        End With
    Next i

End With


 End Sub
这是我的第一个函数

 Function findValueInRangeReturnRow(rng As Range, value As Variant) As Long

        Set c = rng.Find(value, LookIn:=xlValues)

        If Not c Is Nothing Then

            findValueInRangeReturnRow = c.row

        End If

End Function
我的第二个函数,用于检查在指定范围内是否发现重复项

Function compareAEO(rad1 As Variant, rad2 As Variant, typeCOMPARE As String) As Boolean

Dim col1 As String
Dim col2 As String

  Select Case typeCOMPARE
    Case "FAX"
        col1 = "F"
        col2 = "F"

    Case "DAX"
        col1 = "M"
        col2 = "M"

    End Select

   If ThisWorkbook.Worksheets("Le").Range(col1 & rad1).value =   ThisWorkbook.Worksheets("Be").Range(col2 & rad2).value Then

    compareAEO = True
    Else

    compareAEO = False
End If



End Function

试试这个;将数据放在从B到G的单页中(然后是Le);将此公式放入H列

=IF(VLOOKUP(E2,B$2:D$7,2,FALSE)=F2,IF(G2<D2,"replanned","Not Delivered"),"delivered")

=IF(VLOOKUP(E2,B$2:D$7,2,FALSE)=F2,IF(G2您得到的是每个循环中两个页面的最后一行。只需在循环之外的顶部获取一次。对于您设置的范围相同。您可以看到,我将它们放在循环之前的顶部

我真的不知道您使用ASMT的目的是什么。看起来您试图在某些编码中将其用作范围,而不是范围(“B”&I)。我在测试时使用了Le的“B”列中的字符串与Be的“B”列进行比较

它对我有用。你必须改变它以适应你的需要。你不需要所有的函数,它们完成的都在这个子程序中

Sub checkASMT()
Dim rng1 As Range
Dim rng2 As Range
Dim lastRowLE As Long
Dim lastRowBe As Long
Dim row As Long
Dim ASMT As String
Dim LEws As Worksheet
Dim tmpRng As Range

    Set LEws = Worksheets("Le")
    lastRowLE = Sheets("Le").Cells(ActiveSheet.Rows.Count, "B").End(xlUp).row
    lastRowBe = Sheets("Be").Cells(ActiveSheet.Rows.Count, "B").End(xlUp).row
    Set rng1 = Sheets("Be").Range("B3", "B" & lastRowBe)


   For i = 29 To lastRowLE
        Set tmpRng = Sheets("Le").Range("b" & i)
        ASMT = tmpRng.Value

        Set c = rng1.Find(ASMT, LookIn:=xlValues)
        If Not c Is Nothing Then
                row = c.row
                If ThisWorkbook.Worksheets("Le").Range("F" & i).Value = ThisWorkbook.Worksheets("Be").Range("F" & row).Value Then
'                    Worksheets("Le").Cells(i, ASMT).Value = "Not Delivered"
'                   Did you intend to use ASMT as the column number?
'                   I'm going to hard code that as column 27 for my purposes.  You can change it if you need to
                    LEws.Cells(i, 27).Value = "Not Delivered"   ' column 27 is "AA"
                Else
                    LEws.Cells(i, 27).Value = "Delivered"
                End If
                If ThisWorkbook.Worksheets("Le").Range("M" & i).Value = ThisWorkbook.Worksheets("Be").Range("M" & row).Value Then
'                    .Worksheets("Le").ASMT.Offset(0, 1).Value = "Replan"
'                       again I don't understand the reference to ASMT.  That is a string value - unless it is a numeric value in the string
'                       I'm going to assume that you intended for "Replan" to go into column C on row i
                Else
                    LEws.Range("C" & i).Value = "Replan"
                End If
         End If
    Next i



 End Sub

这将有助于你展示你的数据“结构”,以及我的帖子中关于你评论的通缉输出示例!谢谢!你可能想在运行“好”之后添加数据的最终状态macroDone和done先生!您的代码中围绕
If
语句发布了几个问题。1.什么是
检查传真
,这应该是一个注释吗?2.
compareASMTECOPART
是在某个地方单独定义的,还是应该是
compareAEO
?3.您的“DAX”
If
块没有相应的
End If
.4.
.Worksheets(“Le”).ASMT.Offset(0,1).value=“Replan”
不起作用,因为
工作表前面不应该有
ASMT
字符串,而不是
范围。谢谢!!@user2648008会在我一小时到办公室后检查:)我非常坚持这一点……希望这能起到作用。出于我的学习目的,annons也有VBA方法吗?这让我走上了正确的方向。我在helper列中完成了两个不同的公式,然后有一个VBA代码从这些列复制到a列。嘿!@John_Muggins U是VBA神!比ks.你对我如何处理VBA以成为一名真正优秀的程序员有什么建议吗?继续努力吧。还有,当你的例程正常工作时,把它们保存到一个特殊的模块中,你可以将它们插入到未来的项目中,并将它们放在手边。查找最后使用的行是一个简单的代码,但经过30年的编程,我仍然需要查找如何做到这一点是的。把它放在我的特殊模块里,我永远不需要走多远就能找到它。
=IF(VLOOKUP(E2,B$2:D$7,2,FALSE)=F2,IF(G2<D2,"replanned","Not Delivered"),"delivered")
Sub checkASMT()
Dim rng1 As Range
Dim rng2 As Range
Dim lastRowLE As Long
Dim lastRowBe As Long
Dim row As Long
Dim ASMT As String
Dim LEws As Worksheet
Dim tmpRng As Range

    Set LEws = Worksheets("Le")
    lastRowLE = Sheets("Le").Cells(ActiveSheet.Rows.Count, "B").End(xlUp).row
    lastRowBe = Sheets("Be").Cells(ActiveSheet.Rows.Count, "B").End(xlUp).row
    Set rng1 = Sheets("Be").Range("B3", "B" & lastRowBe)


   For i = 29 To lastRowLE
        Set tmpRng = Sheets("Le").Range("b" & i)
        ASMT = tmpRng.Value

        Set c = rng1.Find(ASMT, LookIn:=xlValues)
        If Not c Is Nothing Then
                row = c.row
                If ThisWorkbook.Worksheets("Le").Range("F" & i).Value = ThisWorkbook.Worksheets("Be").Range("F" & row).Value Then
'                    Worksheets("Le").Cells(i, ASMT).Value = "Not Delivered"
'                   Did you intend to use ASMT as the column number?
'                   I'm going to hard code that as column 27 for my purposes.  You can change it if you need to
                    LEws.Cells(i, 27).Value = "Not Delivered"   ' column 27 is "AA"
                Else
                    LEws.Cells(i, 27).Value = "Delivered"
                End If
                If ThisWorkbook.Worksheets("Le").Range("M" & i).Value = ThisWorkbook.Worksheets("Be").Range("M" & row).Value Then
'                    .Worksheets("Le").ASMT.Offset(0, 1).Value = "Replan"
'                       again I don't understand the reference to ASMT.  That is a string value - unless it is a numeric value in the string
'                       I'm going to assume that you intended for "Replan" to go into column C on row i
                Else
                    LEws.Range("C" & i).Value = "Replan"
                End If
         End If
    Next i



 End Sub