添加列的button删除以前的列?

您好我在Excel 2013中做了一个button,基于用户input添加一个新的列到我的表。 现在button生成的列,但是当我去打印预览不知何故分隔列复制的行被删除。 这是我的意思:
![新列生成后的电子表格打印预览] [1]

注意培训和保修列没有行分隔符,当新的列产生?

此外,新生成的列,第一个单元格不是橙色,并在顶部缺less一行。 我出来的想法,不能使用macros,因为表的行和列的变化,这意味着范围的变化。

这里是代码

  Private Sub CommandButton22_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(, -3).Copy '< copy the column two columns to the left of the referenced one (ie one column left of the new one)' .Offset(, -1).PasteSpecial xlPasteFormats '< paste formats to the new column' Application.CutCopyMode = False End With End Sub 

当你添加一列或一行或单元格到一个现有的范围,我已经知道Excel不能可靠地格式化新的范围。 对于我来说,明确地格式化新添加的范围会更容易,所以我知道我得到了什么。 在你的情况,你正在寻找边界,所以你现有的VBA代码可以做到这一点…

 Option Explicit Sub test() Dim newCol As Range Set newCol = ActiveSheet.Range("S1:S100") With newCol.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlColorIndexAutomatic End With End Sub