vba 插入的列采用从列到左侧的格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22965938/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:43:01  来源:igfitidea点击:

Inserted column takes formatting from column to left?

excelvbaformatting

提问by user1759942

I have a bunch of excel macros, and many of them involve inserting columns and putting calculation formulas in said columns. But I just realized now that when a new column is inserted it takes the formatting of the column to its left. So it may end up being formatted as text or something else, which is starting to negatively affect performance.

我有一堆 excel 宏,其中许多涉及插入列并将计算公式放入所述列中。但我现在才意识到,当插入新列时,它会将列的格式设置为左侧。所以它最终可能会被格式化为文本或其他东西,这开始对性能产生负面影响。

Is there a way to turn this default behavior off? is there a way I can do this in VBA without going through my code and putting entireColumn.numberformat = "Generalafter EVERY time I insert new columns?

有没有办法关闭这个默认行为?有没有一种方法可以在 VBA 中做到这一点,而无需entireColumn.numberformat = "General在每次插入新列时都经过我的代码和放置?

I know you can use the "PAste" options when pasting with right click, but what about VBA?

我知道您可以在右键单击粘贴时使用“粘贴”选项,但是 VBA 呢?

回答by SeanC

There are two options when doing an insert:

进行插入时有两种选择:

Default:

默认:

Columns("D:D").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("D:D").Insert Shift:=xlToRight ' CopyOrigin is default from Left or Above

second option:

第二种选择:

Columns("D:D").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightOrBelow

If neither of these options work, you may consider having a column set up as you require, and insert it into the spreadsheet

如果这些选项都不起作用,您可以考虑根据需要设置一列,并将其插入到电子表格中

Columns("BS:BS").Copy ' note that this should be on another sheet,
                      ' or tracked in some way, as it will move after
                      ' insert on same sheet
Columns("D:D").Insert Shift:=xlToRight