Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Excel使用宏插入函数列_Vba_Excel - Fatal编程技术网

Vba Excel使用宏插入函数列

Vba Excel使用宏插入函数列,vba,excel,Vba,Excel,我想是一个宏,在每4行之后插入一个函数列,第3行向下将包含一个函数,第1行和第2行将与前一行相同,第3行将显示计划订单 我想添加这个相同的宏,在这个宏之后每5个创建一个宏,然后每6个创建一个宏,每7个创建一个宏 它可以是4个宏,我只要按正确的顺序单击就可以了 我在下面创建了一个宏,它每第四列插入一行,但我必须首先将表移动到三列上,它不会生成最后一列。我怎样才能得到我想要添加到本专栏的函数 Sub insert_column_after_interval_4() Dim iLastCol A

我想是一个宏,在每4行之后插入一个函数列,第3行向下将包含一个函数,第1行和第2行将与前一行相同,第3行将显示计划订单

我想添加这个相同的宏,在这个宏之后每5个创建一个宏,然后每6个创建一个宏,每7个创建一个宏

它可以是4个宏,我只要按正确的顺序单击就可以了

我在下面创建了一个宏,它每第四列插入一行,但我必须首先将表移动到三列上,它不会生成最后一列。我怎样才能得到我想要添加到本专栏的函数

Sub insert_column_after_interval_4()

  Dim iLastCol As Integer

  iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column ' same as CTRL+RIGHT ARROW

  For colx = 5 To iLastCol Step 5

     Columns(colx).Insert Shift:=xlToRight

  Next

End Sub
包括表格当前外观的图片,最终结果将是在该表格之后以及电子表格中每4列之后增加4列公式


下面的代码将在每四列之后插入三列,第一列将添加到第六列,然后在每四列之后插入三列

四列集合中最后两列的数据将粘贴到新列的前两列中

有两件事需要记住。当多个
工作簿
工作表
可用时,您总是希望使用对
工作簿
工作表的直接引用,以避免程序中出现混乱

 Option Explicit

 Sub InsertingColumns()

      'Always want to directly reference the WorkBook and WorkSheet which contains your data
      'Change Book1 and Sheet1 as per your requirement
      Dim CurrentWorkSheet As Worksheet
      Set CurrentWorkSheet = Workbooks("Book1").Worksheets("Sheet1")

      Dim LastColumn As Long
      LastColumn = CurrentWorkSheet.Cells(1, Columns.Count).End(xlToLeft).Column

      'Step 7 to cater for the every forth Column plus the 7 columns being added
      'Started at 6 because "After every four columns" means the fith Column and catering for the Cal Day in Column 1
      Dim CurrentColumn As Long
      For CurrentColumn = 6 To LastColumn Step 7

           'Current Column to CurrentColumn + 2 will have 3 Cells selected
           CurrentWorkSheet.Range(Cells(1, CurrentColumn), Cells(1, CurrentColumn + 2)).Insert Shift:=xlToRight

           'Copy Data from the second column from the right and paste into the first new column
           CurrentWorkSheet.Columns(CurrentColumn - 2).Copy
           CurrentWorkSheet.Columns(CurrentColumn).PasteSpecial Paste:=xlPasteValues

           'Copy Data from the first column from the right and paste into the second new column
           CurrentWorkSheet.Columns(CurrentColumn - 1).Copy
           CurrentWorkSheet.Columns(CurrentColumn + 1).PasteSpecial Paste:=xlPasteValues


      Next CurrentColumn

 End Sub
请参见下面的“之前和之后”:


对于5、6和7个插入,您可以编辑这四个插入循环以满足其他插入循环。

您好,欢迎使用StackOverflow。为了清楚起见,您希望在每第四列之后添加四列吗?那么你想在第五、第六和第七列中都做同样的事情吗?