简体   繁体   中英

Insert an empty column with a step by 7 in VBA Excel

I have a 28 columns, but after every 7 columns I want to insert an empty column. Can you help me to create that macro.

初始列

后宏

ActiveWindow.ScrollWorkbookTabs Position:=xlLast
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "LP"
    For i = 2 To 22
        Sheets(i).Activate
        Sheets(i).Cells(5, 2).Select
        Sheets(i).Range(Selection, Selection.End(xlToRight)).Select
        Selection.Copy
        Sheets("Linearity Plot").Activate
        Sheets("Linearity Plot").Cells(i - 1, 1).Select
        ActiveSheet.Paste
    Next i
    ActiveSheet.Range(Cells(1, 1), Cells(21, 121)).Select

ActiveWindow.ScrollWorkbookTabs Position:=xlLast
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Transpose"
    Sheets("Transpose").Activate

Worksheets("LP").Range("A1:DQ21").Copy
Worksheets("Transpose").Range("A2").PasteSpecial Transpose:=True

Try this code :

Sub insertColumns()
    Dim lastCol As Integer
    lastCol = 28
    Dim i As Integer
    i = 0
    Dim nbColAdded As Integer
    nbColAdded = 0

    While i < lastCol + nbColAdded
        i = i + 8
        nbColAdded = nbColAdded + 1
        Cells(1, i).EntireColumn.Insert
    Wend


End Sub

When inserting or deleting rows or columns in a loop, it is considered best practise to start at the outer boundary and work in. For rows, this means start at the bottom and work up; columns you would start at the right-most and work toward the left. This method avoids the renumbering effect that inserting or deleting rows/columns has on the remainder of the rows/columns to be processed.

Since you want to insert one or more columns every x number of columns, you will have to know how many columns you have to start with and determine whether that is a multiple of x . If it is not, you may have to shift right or left before commencing the insert columns loop.

According to your original question, you have 28 columns and the stagger is 'after every 7th column'. I take this as wanting to insert one or more columns at the 29th, 22nd, 15th and 8th position. After repositioning from the insertion of the other columns, the new pairs of columns will be in H:I, Q:R and Z:AA.

Sub insert_columns()
    Dim iStagger As Long, iNumCols As Long, c As Long, lc As Long

    iStagger = 7
    iNumCols = 2
    With ActiveSheet
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
        lc = lc + 8 - (lc Mod iStagger)
        For c = lc To iStagger Step -iStagger
            .Cells(1, c).Resize(1, iNumCols).EntireColumn.Insert
        Next c
    End With
End Sub

Modify the stagger and/or the number of columns to insert in the 3rd and 4th row. Everything else should be taken care of with the calculations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM