vba 每 7 列后插入一个带有公式的新列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21910265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert a new column after every 7 columns with a formula
提问by Shriya Reddy
I have a spreadsheet with 4000 records that keeps on updating new data. The column headers are Employee Name, Project, Activity, 06/29/2013, 06/30/2013... etc. These are individual dates starting from October of last year to this week and each of these columns contains numeric values. I'm trying to modify this week-wise. So if Sunday is considered as the 7th day of a week, then I want to subtract the values from this week's Sunday with the last week's Sunday. And I'd want it to continue doing that for the rest of the columns since the dates will keep on updating so it's going to be taxing to do it manually.
我有一个包含 4000 条记录的电子表格,它不断更新新数据。列标题是员工姓名、项目、活动、06/29/2013、06/30/2013...等。这些是从去年 10 月到本周的各个日期,这些列中的每一个都包含数值。我正在尝试在本周进行修改。因此,如果将周日视为一周的第 7 天,那么我想从本周的周日和上周的周日中减去这些值。而且我希望它继续为其余的列执行此操作,因为日期会不断更新,因此手动操作会很费力。
FYI I'm using Excel 2013.
仅供参考,我正在使用 Excel 2013。
I know how to perform basic operations but this one is beyond my understanding so any help would be much appreciated!
我知道如何执行基本操作,但这超出了我的理解,因此非常感谢任何帮助!
Sub insert_column_after_interval_7()
For colx = 7 To 200 Step 8
Columns(colx).Insert Shift:=xlToRight
Next
End Sub
I have specified the range as 200 but the data might exceed later on. So how do I modify it without giving any specific range?
我已将范围指定为 200,但稍后数据可能会超过。那么如何在不给出任何特定范围的情况下修改它呢?
回答by Our Man in Bananas
you could try something along the lines of this:
你可以尝试一些类似的东西:
Sub insert_column_after_interval_7()
dim iLastCol as integer
iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column ' same as CTRL+RIGHT ARROW
For colx = 7 To iLastCol Step 8
Columns(colx).Insert Shift:=xlToRight
Next
End Sub
of course, as you are likely adding more than 8 columns, you might be better off with a DO WHILE
loop
当然,由于您可能会添加超过 8 列,因此最好使用DO WHILE
循环