vba 如何向现有工作表添加新列并为其命名?

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

How to add a new column to an existing sheet and name it?

excelvba

提问by Raj

Suppose I have the worksheet below:

假设我有下面的工作表:

Empid  EmpName  Sal
1      david     100
2      jhon      200
3      steve     300

How can I insert a new column named "Loc"?

如何插入名为“Loc”的新列?

Empid  EmpName   Loc   Sal

1      david     uk    100
2      jhon      us    200
3      steve     nj    300

回答by Bruno Leite

Use insert method from range, for example

使用范围内的插入方法,例如

Sub InsertColumn()
        Columns("C:C").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("C1").Value = "Loc"
End Sub

回答by brettdj

For your question as asked

对于您提出的问题

Columns(3).Insert
Range("c1:c4") = Application.Transpose(Array("Loc", "uk", "us", "nj"))

If you had a way of automatically looking up the data (ie matching uk against employer id) then you could do that in VBA

如果您有一种自动查找数据的方法(即将英国与雇主 ID 匹配),那么您可以在 VBA 中执行此操作