Dividir excel en varias filas

Hace unos días pedí una macro para dividir un excel en filas, en concreto era de 400000 y quería en tramos de 10000, ahora necesito hacer de los 10000 tramos de 200 y no me esta funcionando porque me sale error 9 en tiempo de ejecución, subíndice fuera del intervalo.

Decir que yo solo modificaba los 10000 por 200, el compañero que me la creo me comento que modificara a1 y a2 según mis necesidades pero estoy un poco verde y no se a que se refería.

Sub separa()
Set datos = Sheets("hoja1").Range("a1").CurrentRegion
With datos
f = .Rows.Count
c = .Columns.Count
Set tabla = .Rows(2).Resize(f - 1, c)
filas = 10000
hojas = (f - 1) / filas
For i = 2 To hojas
On Error Resume Next
Sheets("hoja" & i).Select
If Err.Number > 0 Then
Sheets.Add After:=Sheets(Sheets.Count)
End If
On Error GoTo 0
If i = 2 Then Set tabla2 = tabla.Resize(f - 1, c)
If i > 2 Then Set tabla2 = tabla2.Rows(filas + 1).Resize(filas, c)
tabla2.Copy: Range("a2").PasteSpecial
tabla.Rows(0).Copy: Range("a1").PasteSpecial
Next i
End With
Set tabla = Nothing: Set tabla2 = Nothing
End Sub

Respuesta
1

La macro que te pase no te sirve para lo que quieres, cuando mencione lo de cambiar los rangos quize decir que si tus datos estaban en otra columna B o C por ejemplo solo ajustabas poniendo b1 o C1 en vez de A1, la macro que ocupas es esta

Sub separa_filas()
Set h1 = Worksheets("hoja1")
Set datos = h1.Range("a2").CurrentRegion
matriz = datos
With datos
    f = .Rows.Count:    filas = 10000:    tramos = 200
    hojas = f / filas + 1:    cantidad = filas / tramos
    For i = 2 To hojas
    On Error Resume Next
        Sheets("hoja" & i).Select
        If Err.Number > 0 Then
            Sheets.Add After:=Sheets(Sheets.Count)
        End If
        For j = 1 To cantidad
            If j = 1 Then
                Set tabla = .Resize(tramos, 1)
                Set tabla2 = Range("c2").Resize(tramos, 1)
            End If
            If j > 1 Then
                Set tabla = tabla.Rows(tramos + 1).Resize(tramos, 1)
                Set tabla2 = tabla2.Rows(tramos + 3).Resize(tramos, 1)
            End If
            tabla.Copy: tabla2.PasteSpecial
        Next j
    On Error GoTo 0
    Next i
    Set tabla = Nothing: Set tabla2 = Nothing
End With
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas