¿Cómo fijar una condición en un función set de una macro?

De nuevo Dante,

Por favor, necesito saber cómo fijar una condición dentro de una función set a la hora de hacer la siguiente macro:

Sub Pasar_Datos_2()
'Por Dante Amor
Set h1 = Sheets("NO TOCAR_1")
Set h2 = Sheets("TABLA 1")
Set h3 = Sheets("TABLA 3")
'
'Limpiar hojas
H2.Range("C20:D" & Rows. Count). ClearContents
H3.Range("C17:H" & Rows. Count). ClearContents
'
'Leer tabla1 de la hoja "no tocar_1"
j = 20
i = 16
Do While h1.Cells(i, "E").Value <> ""
h2.Cells(j, "C").Value = h1.Cells(i, "E").Value
h2.Cells(j, "D").Value = h1.Cells(i, "H").Value
j = j + 1
i = i + 1
Loop
'
'Leer tabla3 de la hoja "no tocar_1"
j = 17
Set b = h1.Columns("C:K").Find("Tabla 3", lookat:=xlWhole)
If Not b Is Nothing Then
i = b.Row + 1
Do While h1.Cells(i, "E").Value <> ""
h3.Range(h3.Cells(j, "C"), h3.Cells(j, "H")).Value = _
h1.Range(h1.Cells(i, "E"), h1.Cells(i, "J")).Value
j = j + 1
i = i + 1
Loop
Else
MsgBox "No se encuentra el texto 'Tabla 3' en la hoja 'No tocar_1'"
Exit Sub
End If
MsgBox "Fin"
End Sub

Los datos de origen son los siguientes: 

Y los de destino estos:

La condición es la siguiente: 

Los datos de la columna I y J del libro NO TOCAR_1 se deben de copiar en las columnas F y G del libro TABLA 1 pero solo en el lugar que corresponda. Es decir, si los datos están en el tipo A se deben de copiar solo en la columna del tipo A en el destino y dejar la columna de tipo B en blanco. Simplemente sería decirle que cuando hubiera un valor diferente de 0 o en blanco que lo copie sino que lo deje sin valor.

¿Cómo se mete eso en la macro? ¿Sería algo así? 

Do While h1.Cells(i, "E").Value > 1

h2.Cells(j, "F").Value = h1.Cells(i, "I").Value

h2.Cells(j, "G").Value = h1.Cells(i, "J").Value

¿pero eso se puede hacer dentro del do while anterior [Do While h1.Cells(i, "E").Value <> ""]

1 respuesta

Respuesta
1

Te anexo la macro con el cambio

Sub Pasar_Datos()
'Por Dante Amor
    Set h1 = Sheets("NO TOCAR_1")
    Set h2 = Sheets("TABLA 1")
    Set h3 = Sheets("TABLA 3")
    '
    'Limpiar hojas
    h2.Range("C20:G" & Rows.Count).ClearContents
    h3.Range("C17:H" & Rows.Count).ClearContents
    '
    'Leer tabla1 de la hoja "no tocar_1"
    j = 20
    i = 6
    Do While h1.Cells(i, "E").Value <> ""
        h2.Cells(j, "C").Value = h1.Cells(i, "E").Value
        h2.Cells(j, "D").Value = h1.Cells(i, "H").Value
        If h1.Cells(i, "I").Value <> 0 And h1.Cells(i, "I").Value <> "" Then
            h2.Cells(j, "F").Value = h1.Cells(i, "I").Value
        End If
        If h1.Cells(i, "J").Value <> 0 And h1.Cells(i, "J").Value <> "" Then
            h2.Cells(j, "G").Value = h1.Cells(i, "J").Value
        End If
        j = j + 1
        i = i + 1
    Loop
    '
    'Leer tabla3 de la hoja "no tocar_1"
    j = 17
    Set b = h1.Columns("C:K").Find("Tabla 3", lookat:=xlWhole)
    If Not b Is Nothing Then
        i = b.Row + 1
        Do While h1.Cells(i, "E").Value <> ""
            h3.Range(h3.Cells(j, "C"), h3.Cells(j, "H")).Value = _
            h1.Range(h1.Cells(i, "E"), h1.Cells(i, "J")).Value
            j = j + 1
            i = i + 1
        Loop
    Else
        MsgBox "No se encuentra el texto 'Tabla 3' en la hoja 'No tocar_1'"
        Exit Sub
    End If
    MsgBox "Fin"
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas