Como realizar un case en vba de selección de hojas

Estoy tratando de realizar un case para la selección de hojas estas son 6 de las cuales tengo identificado como archivo1, archivo2... Así sucesivamente, de las cuales mi tabla realiza un recorrido de N cantidad de información en mi tabla la cual en la columna DE cada línea la identifico con lo mismo archivo1, archivo2 etc, deseo hacer el case para que esta información pase a la hoja correspondiente y mandarla a imprimir con el case

1 respuesta

Respuesta
2

Te anexo un ejemplo

Sub Copiar_Registros()
    Application.ScreenUpdating = False
    Set h1 = Sheets("Hoja1")
    For i = 2 To h1.Range("D" & Rows.Count).End(xlUp).Row
        hoja = LCase(h1.Cells(i, "D").Value)
        Select Case hoja
            Case "archivo1": Set h2 = Sheets("archivo1")
            Case "archivo2": Set h2 = Sheets("archivo2")
            Case "archivo3": Set h2 = Sheets("archivo3")
        End Select
        'Copia y pega cada registro en la hoja correspondiente
        u2 = h2.Range("D" & Rows.Count).End(xlUp).Row + 1
        h1.Rows(i).Copy
        h2.Rows(u2).PasteSpecial xlValues
    Next
    'Imprimir hojas
    '
    Sheets("archivo1"). PrintOut
    Sheets("archivo2"). PrintOut
    Sheets("archivo3"). PrintOut
    '
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    MsgBox "Fin"
End Sub

Ahora bien, si el contenido de la celda es igual nombre de la hoja, no necesitas un Case, solamente lo pones como nombre de hoja, ejemplo:

Sub Copiar_Registros2()
    Application.ScreenUpdating = False
    Set h1 = Sheets("Hoja1")
    For i = 2 To h1.Range("D" & Rows.Count).End(xlUp).Row
        hoja = LCase(h1.Cells(i, "D").Value)
        Set h2 = Sheets(hoja)
        'Copia y pega cada registro en la hoja correspondiente
        u2 = h2.Range("D" & Rows.Count).End(xlUp).Row + 1
        h1.Rows(i).Copy
        h2.Rows(u2).PasteSpecial xlValues
    Next
    'Imprimir hojas
    '
    Sheets("archivo1"). PrintOut
    Sheets("archivo2"). PrintOut
    Sheets("archivo3"). PrintOut
    '
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    MsgBox "Fin"
End Sub

'.[Sal u dos. Dante Amor. No olvides valorar la respuesta. 
'.[Avísame cualquier duda

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas