Editar Macro Excel para transferir datos de hoja2 a Hoja1

Para Adriel

Hola Adriel, tengo tu macro la cual quier ousar para lo siguiente; según imágenes explicitas

Sub compara_copia_ordena()
'//Por Adriel Ortiz
'
Set h1 = Sheets("Hoja1")
Set h2 = Sheets("Hoja2")
'
For i = 2 To h2.Range("A" & Rows.Count).End(xlUp).Row
    Set b = h1.Columns("A").Find(h2.Cells(i, "A"), lookat:=xlWhole)
        If b Is Nothing Then
            u1 = h1.Range("A" & Rows.Count).End(xlUp).Row + 1
            h2.Cells(i, "A").Copy h1.Range("A" & u1)
            u1 = u1 + 1
    End If
Next i
    u = h1.Range("A" & Rows.Count).End(xlUp).Row
        With h1.Sort
            .SortFields.Clear
            .SortFields.Add Key:=h1.Range("A2:A" & u), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange h1.Range("A1:A" & u)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
End Sub

Tengo Hoja 1

Tengo Hoja 2

Según tu macro, la cual no esta echa para el fin que deseo, ejecutándola me queda así

Pero quiero que queda así

Ordenado hoja1 por B, trayendo todos los datos de cada producto

Si se puede también ordenar la Hoja2 con la misma macro

1 Respuesta

Respuesta
1

H o l a 

te paso la macro 

Sub compara_copia_ordena()
'//Por Adriel Ortiz
'
Set h1 = Sheets("Hoja1")
Set h2 = Sheets("Hoja2")
'
For i = 2 To h2.Range("B" & Rows.Count).End(xlUp).Row
    Set b = h1.Columns("B").Find(h2.Cells(i, "B"), lookat:=xlWhole)
        If b Is Nothing Then
            u1 = h1.Range("A" & Rows.Count).End(xlUp).Row + 1
            h2.Rows(i).Copy h1.Range("A" & u1)
            u1 = u1 + 1
    End If
Next i
'
    u = h1.Range("A" & Rows.Count).End(xlUp).Row
        With h1.Sort
            .SortFields.Clear
            .SortFields.Add Key:=h1.Range("B2:B" & u), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange h1.Range("A1:I" & u)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
'
   u2 = h2.Range("B" & Rows.Count).End(xlUp).Row
        With h2.Sort
            .SortFields.Clear
            .SortFields.Add Key:=h2.Range("B2:B" & u2), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange h2.Range("A1:I" & u2)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        MsgBox "fin"
End Sub

valora para finalizar saludos!

La comparación lo hago con la columna B, ya que los códigos del prod. no son únicos, tienen sub categorías. Por ejemplo alambre.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas