如何在 VBA 中更改标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/30448673/
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
How to change headers in VBA
提问by Lee
I'm having problem to modify the column headers to my desired naming. This is my table:
我在将列标题修改为我想要的命名时遇到问题。这是我的表:
st_name  st_id  st_num
Alex      112   1113
Alice     110   1132
Expected output:
预期输出:
Name      ID    Number
Alex      112   1113
Alice     110   1132
How to rename the headers as shown in the expected output table.
如何重命名标题,如预期的输出表中所示。
回答by Vasily
there are several variants of how to achieve what you need
如何实现您的需求有多种变体
Please note that example based on used range "A1:C1"
请注意基于使用范围的示例 "A1:C1"
1 Variant
1 变体
Cells(1, 1).Value = "Name"
Cells(1, 2).Value = "ID"
Cells(1, 3).Value = "Number"
2 variant
2 变体
Cells(1, "A").Value = "Name"
Cells(1, "B").Value = "ID"
Cells(1, "C").Value = "Number"
3 Variant
3 变体
Range("A1").Value = "Name"
Range("B1").Value = "ID"
Range("C1").Value = "Number"
4 Variant
4 变体
[A1].Value = "Name"
[B1].Value = "ID"
[C1].Value = "Number"
5 Variant, as alternative in case when range goes one after another
5 变体,当范围一个接一个变化时作为替代方案
[A1:C1].Value = Split("Name ID Number")
or
或者
Range("A1:C1").Value = Split("Name;ID;Number",";")
or
或者
Range(Cells(1, 1), Cells(1, 3)).Value = Split("Name ID Number")
or
或者
Range(Cells(1, "A"), Cells(1, "C")).Value = Split("Name ID Number")
or
或者
[A1:C1].Value = Split("Name ID Number")
... and so on ... depending on task which required to be achieved
......等等......取决于需要完成的任务
回答by jrdnmdhl
In a macro, use the following:
在宏中,使用以下内容:
Worksheets("YourSheetName").Range("A1").value = "Name"
Worksheets("YourSheetName").Range("A2").value = "ID"
Worksheets("YourSheetName").Range("A3").value = "Number"
Change argument to Worksheets to match the name of the worksheet the change is being made on. If those are not the first three columns you will need to change the cell reference in the Range method's argument.
将参数更改为 Worksheets 以匹配正在进行更改的工作表的名称。如果这些不是前三列,您将需要更改 Range 方法参数中的单元格引用。

