添加列的按钮会删除上一列吗?

时间:2016-12-16 14:56:56

标签: excel vba

您好我在excel 2013中创建了一个按钮,根据用户输入在我的表中添加了一个新列。现在按钮生成列,但是当我以某种方式打印预览时,分隔它复制的列的行被删除。这就是我的意思:
![新列生成后打印电子表格的预览] [1]

请注意,生成新列时,培训和保修列没有行分隔符?

同样是新生成的列,第一个单元格不是橙色,并且在顶部缺少一条线。我没有想法,也无法使用宏,因为表的行和列发生了变化,这意味着范围会发生变化。

这里是代码

      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 (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

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 (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

1 个答案:

答案 0 :(得分:0)

当您将列或行或单元格添加到现有范围时,我了解到Excel无法按照我期望的方式可靠地格式化新范围。对我来说,显式格式化新添加的范围更容易,所以我知道我得到了什么。在您的情况下,您正在寻找边框,以便您现有的VBA代码可以实现...

unsearchable()