Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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,这件事让我很难受。这段代码帮助我突出显示并匹配列J和G之间的值,然后将值从列J移动到列H中,这样它就可以位于列G中相同的值旁边,但只有在它位于同一行时才起作用。我希望它能够工作,即使它在J列的不同行中。这张图显示了我的问题 现在,请查看您的逻辑陈述: =如果Cellsresultado.Row,colG=Cellsresultado.Row,colJ,则 这就是为什么它只适用于同一行。相反,您应该做的是遍历列J范围 在显示一个简单的消息框之前,请在If Cellsresultado.ROw,G=

这件事让我很难受。这段代码帮助我突出显示并匹配列J和G之间的值,然后将值从列J移动到列H中,这样它就可以位于列G中相同的值旁边,但只有在它位于同一行时才起作用。我希望它能够工作,即使它在J列的不同行中。这张图显示了我的问题


现在,请查看您的逻辑陈述: =如果Cellsresultado.Row,colG=Cellsresultado.Row,colJ,则

这就是为什么它只适用于同一行。相反,您应该做的是遍历列J范围

在显示一个简单的消息框之前,请在If Cellsresultado.ROw,G=Cellsresultado.ROw,colJ处尝试此替换,直至结束If:

Dim cel As Range, cel2 As Range
Dim lastRow&

lastRow = Cells(Rows.Count, 10).End(xlUp).Row

For Each cel In Range(Cells(1, 7), Cells(lastRow, 7))
    cel.Select ' Comment this out when using, it's just here so you can walk through (with F8) and visually see the cel
    For Each cel2 In Range(Cells(1, 10), Cells(lastRow, 10))
        cel2.Select 'Same here, re: comment
        If cel2.Value = cel.Value Then
            cel.Offset(0, 1).Value = cel2.Value
            cel2.Value = ""
            Exit For
        End If
    Next cel2

Next cel

您也可以使用H列中的公式来执行此操作

示例中的公式为:

=IF(ISERROR(INDEX($C$1:$C$8,MATCH(A1,$C$1:$C$8,0))),"",INDEX($C$1:$C$8,MATCH(A1,$C$1:$C$8,0)))
您只需修改引用即可。它在A1中查看C:C中是否有匹配项。如果是,它将值放入B1中


谢谢!两个答案都对我有帮助!这是我修改过的代码

Public Sub series()
        'Definición de variables (Definition of variables)
        Dim rango As String, valor As String, resultado As Range
        Dim primerResultado As String, cont As Integer


        'Solicitar información al usuario (Get information from the user)
        rango = "A1:XFD1048576"
        valor = MsgBox("Deseas Mover los Valores?:")
        If valor = "" Then Exit Sub


        cont = 0    'Inicializar contador de coincidencias (Initialize Find)

        'Primera búsqueda del valor dentro del rango (First search for value in the range)
        Set resultado = Range(rango).Find(What:=valor, _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False, _
                        SearchFormat:=False)

        'If Not resultado Is Nothing Then    'Si el resultado de la búsqueda no es vacío

        ' primerResultado = resultado.Address
            ' Do                              'Inicia bucle para hacer varias búsquedas
                ' If MsgBox("Resaltar Valor?", vbYesNo) = vbYes Then
                    ' cont = cont + 1
                   ' resultado.Interior.ColorIndex = 4    'Cambia el color de fondo de la celda

                ' End If
                ' Set resultado = Range(rango).FindNext(resultado) 'Vuelve a buscar el valor

            Dim cel As Range, cel2 As Range
            Dim lastRow As Long
            lastRow = Cells(Rows.Count, 10).End(xlUp).Row
            For Each cel In Range(Cells(1, 7), Cells(lastRow, 7))
            For Each cel2 In Range(Cells(1, 10), Cells(lastRow, 10))
            If cel2.Value = cel.Value Then
            cel.Offset(0, 1).Value = cel2.Value
            cel2.Value = ""
            End If
            Next cel2
            Next cel

            ' If Cells(resultado.Row, colG) = Cells(resultado.Row, colJ) Then

            ' OPTION 1
            ' if value in column G has the same value that in column I
            ' move the value from column I to column H
            ' Cells(resultado.Row, colH) = Cells(resultado.Row, colJ)
            ' resultado.Value = ""

            ' OPTION 2
            ' if G3 has the same value that I5, move the value from I5 to H3.
            ' Note the use of -2
            ' Cells(resultado.Row, colH) = Cells(resultado.Row, colI - 2)

            ' now clear teh source cell
            ' resultado.Value = ""


            ' End If

                ' Display a simple message box.


            ' Loop While Not resultado Is Nothing And resultado.Address <> primerResultado
            ' MsgBox ("Valores Encontrados: " & cont)
        ' Else
            ' MsgBox "Se encontraron " & cont & " coincidencias."
        ' End If
    End Sub
Public Sub series()
        'Definición de variables (Definition of variables)
        Dim rango As String, valor As String, resultado As Range
        Dim primerResultado As String, cont As Integer


        'Solicitar información al usuario (Get information from the user)
        rango = "A1:XFD1048576"
        valor = MsgBox("Deseas Mover los Valores?:")
        If valor = "" Then Exit Sub


        cont = 0    'Inicializar contador de coincidencias (Initialize Find)

        'Primera búsqueda del valor dentro del rango (First search for value in the range)
        Set resultado = Range(rango).Find(What:=valor, _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False, _
                        SearchFormat:=False)

        'If Not resultado Is Nothing Then    'Si el resultado de la búsqueda no es vacío

        ' primerResultado = resultado.Address
            ' Do                              'Inicia bucle para hacer varias búsquedas
                ' If MsgBox("Resaltar Valor?", vbYesNo) = vbYes Then
                    ' cont = cont + 1
                   ' resultado.Interior.ColorIndex = 4    'Cambia el color de fondo de la celda

                ' End If
                ' Set resultado = Range(rango).FindNext(resultado) 'Vuelve a buscar el valor

            Dim cel As Range, cel2 As Range
            Dim lastRow As Long
            lastRow = Cells(Rows.Count, 10).End(xlUp).Row
            For Each cel In Range(Cells(1, 7), Cells(lastRow, 7))
            For Each cel2 In Range(Cells(1, 10), Cells(lastRow, 10))
            If cel2.Value = cel.Value Then
            cel.Offset(0, 1).Value = cel2.Value
            cel2.Value = ""
            End If
            Next cel2
            Next cel

            ' If Cells(resultado.Row, colG) = Cells(resultado.Row, colJ) Then

            ' OPTION 1
            ' if value in column G has the same value that in column I
            ' move the value from column I to column H
            ' Cells(resultado.Row, colH) = Cells(resultado.Row, colJ)
            ' resultado.Value = ""

            ' OPTION 2
            ' if G3 has the same value that I5, move the value from I5 to H3.
            ' Note the use of -2
            ' Cells(resultado.Row, colH) = Cells(resultado.Row, colI - 2)

            ' now clear teh source cell
            ' resultado.Value = ""


            ' End If

                ' Display a simple message box.


            ' Loop While Not resultado Is Nothing And resultado.Address <> primerResultado
            ' MsgBox ("Valores Encontrados: " & cont)
        ' Else
            ' MsgBox "Se encontraron " & cont & " coincidencias."
        ' End If
    End Sub