Button Generates the columns from user input but not the cell lines?

前端 未结 1 670
生来不讨喜
生来不讨喜 2021-01-29 06:50

I implemented a button that ask the user where to add a column, and the button takes the user input(A-Z) and generates the column until the end of the table NOT SPREADSH

1条回答
  •  暖寄归人
    2021-01-29 07:19

    you could try this:

    Private Sub CommandButton2_Click()
        Dim colIndex As Variant
    
        colIndex = Application.InputBox("Enter a column that you want to add: ", "What column?", , , , , , 2) '<--| force a text
        If colIndex = "" Then Exit Sub
        With ThisWorkbook.Sheets("Sheet1").Columns(colIndex) '<--| reference column you want to insert
            .Insert shift:=xlRight '<--| insert a new column , then the referenced one shifts one column to the right of the inserted one 
            .Offset(, -2).Copy '<--| copy the column two columns to the left of the referenced one (i.e. one column left of the new one)
            .Offset(, -1).PasteSpecial xlPasteFormats '<--| paste formats to the new column 
            Application.CutCopyMode = False
        End With
    End Sub
    

    0 讨论(0)
提交回复
热议问题