Insertar Columnas o Filas de manera intercalada en Excel

 


Código:

Sub InsertaColumnasoFilasIntercaladas()


Dim Ws As Worksheet, Rng As Range, fc As String

Dim J As Integer, O As Integer, R As Integer, G As Integer, E As Integer, i As Integer


Set Ws = ActiveSheet


fc = InputBox("C para insertar columnas o F para insertar filas")

fc = UCase(fc)


If fc <> "F" And fc <> "C" Then

    MsgBox "Debes ingresar C para columnas o F para filas.", vbExclamation, "Error"

    Exit Sub

End If


On Error Resume Next


Set Rng = Application.InputBox("Selecciona el rango donde se insertarán las " & IIf(fc = "F", "Filas", "Columnas"), Type:=8)


On Error GoTo 0

If Rng Is Nothing Then

    MsgBox "No seleccionaste un rango válido.", vbExclamation, "Error"

    Exit Sub

End If


J = InputBox("¿Cada cuántas " & IIf(fc = "F", "Filas", "Columnas") & "?")


If Not IsNumeric(J) Or J <= 0 Then

    MsgBox "Debes ingresar un número válido mayor que 0.", vbExclamation, "Error"

    Exit Sub

End If


O = InputBox("¿Número de " & IIf(fc = "F", "Filas", "Columnas") & " a insertar?")


If Not IsNumeric(O) Or O <= 0 Then

    MsgBox "Debes ingresar un número válido mayor que 0.", vbExclamation, "Error"

    Exit Sub

End If


Application.ScreenUpdating = False


If fc = "F" Then

    R = Rng.Rows.Count

    G = Rng.Row

Else

    R = Rng.Columns.Count

    G = Rng.Column

End If


E = 0


For i = 1 To R

    If (i Mod J) = 0 Then

        If fc = "F" Then

            Ws.Rows(G + i + E).Resize(O).Insert Shift:=xlDown

        Else

            Ws.Columns(G + i + E).Resize(, O).Insert Shift:=xlToRight

        End If

        

        E = E + O

    End If

Next i


Application.ScreenUpdating = True

MsgBox "Inserción de " & IIf(fc = "F", "Filas", "Columnas") & " realizada.", vbInformation

End Sub


⭐ Si te gustó, por favor regístrate en nuestra Lista de correo y Suscríbete a mi canal de YouTube para que estés siempre enterado de lo nuevo que publicamos.

Entradas que pueden interesarte

Sin comentarios

Etiquetas:
#aprendiendoexcel365, #josealcaldealias, #Excel, #funciones, #UDF, #Visual Basic, #VBA, #DAX, #Tips, #Basico, #Microsoft Excel, #Cursos Excel OnLine, #Aprende Excel, #Aprende Excel OnLine, #Excel 365, #Power Pivot, #Power BI, #Power Query, #Google Sheets, #Macros, #Código VBA, #Tutoriales Excel, #MTV, #Vbscript, #TypeScript, #Lenguaje M, #fórmulas, #funciones, #paso a paso, #funciones Excel, #libros, #tablas, #formatos, #hojas, #datos, #gráfico, #análisis de datos, #base de datos, #dashboards, #tablas dinámicas, #excel desde cero, #hoja de cálculo, #plantillas de Excel