开发者

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

开发者 https://www.devze.com 2023-04-12 01:20 出处:网络
Suppose I have the worksheet belo开发者_如何学Pythonw: EmpidEmpNameSal 1david100 2jhon200 3steve300

Suppose I have the worksheet belo开发者_如何学Pythonw:

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

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

Empid  EmpName   Loc   Sal

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


Use insert method from range, for example

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消