根据C3中的输入插入列并复制所有列值

时间:2015-07-28 11:52:38

标签: excel-vba vba excel

enter image description here我有一张工作表,我们需要根据所需的单位数自动化列条目数。

每行中的单元格具有基于输入参数和小计的公式。要求是根据所需的单位(可以是输入框或单元格引用)插入定义的列数,这将导致将所有公式和格式复制到所有插入的列。插入的列应位于最后三列之前,即Total,Budget和Variance。

我找到了下面的代码,但这不适合我的例子。

enter image description here

  Private Sub Worksheet_Change(ByVal Target As Range)
Dim LC As Integer
Dim I As Integer

Application.ScreenUpdating = False
If Target.Address <> "$C$3" Then Exit Sub
If Target.Value = "" Or Target.Value = 0 Then MsgBox "Value must be superior to 0": Exit Sub
LC = Cells(2, Application.Columns.Count).End(xlToLeft).Column
If LC = 5 Then
    If Target.Value = 1 Then
         Exit Sub
     Else
          For I = 2 To Target.Value
              Columns(4).Copy
              Columns(I + 3).Insert Shift:=xlToRight
              Cells(2, I + 3).Value = "Unit " & I
              Cells(3, I + 3) = I
          Next I
      End If
      Application.CutCopyMode = False
      Exit Sub
 End If
  Range(Cells(2, 5), Cells(2, LC - 1)).EntireColumn.Delete
 For I = 2 To Target.Value
      Columns(4).Copy
      Columns(I + 3).Insert Shift:=xlToRight
      Cells(2, I + 3).Value = "Unit " & I
      Cells(3, I + 3) = I
 Next I
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
 End Sub

请帮我解决这个问题。

请告诉我如何发送工作表供您参考。 谢谢,

祝你好运, 托马斯

0 个答案:

没有答案
相关问题