我需要将名称分配给整个列的帮助

时间:2019-04-19 15:13:35

标签: excel vba

我需要为整个列分配名称的帮助。

Dim x, y, r As Integer 'x is customer , y is rows

For x = 1 To 25
For y = 2 To 102 Step 4


 ActiveWorkbook.NAmes.Add Name:="Name" & x, RefersToR1C1:="='Main '!RyC4"

Next y
Next x

我希望我的结果是: 每个单元格的名称,而不是单元格内容***

第2行,第3行=名称1

第6行,第3行=名称2

第10行,第3列=名称3

依此类推...

1 个答案:

答案 0 :(得分:0)

尽管我有点不知所措,但我认为您的代码错误是使用y并可以更正为:

Dim x, y, r As Integer 'x is customer , y is rows
For x = 1 To 25
    For y = 2 To 102 Step 4
        ActiveWorkbook.NAmes.Add Name:="Name" & x, RefersToR1C1:="='Main '!R" & y & "C4"
    Next y
Next x

经过测试的代码,显示名称管理器:

enter image description here


查看后,我相信您需要修复x循环并命名...您已经覆盖了名称,这意味着您将自己看到每一列,如上图所示。

通过以下方式进行刺杀:

Dim x as long, y as long, r As long 'MUST STATE "AS" FOR EACH VARIABLE, OTHERWISE THEY ARE VARIANT (X, Y, R AS INTEGER MEANS X AND Y ARE VARIANT)
r = 1
For x = 1 To 25
    For y = 2 To 102 Step 4
        ActiveWorkbook.NAmes.Add Name:="Name" & r, RefersToR1C1:="='Main '!R" & y & "C4"
        r = r+1 'this should fix all of your names to keep showing up
    Next y
Next x

请参阅现在更新的名称管理器: enter image description here


所有这些之后,您仍然需要弄清楚您要对数据进行什么操作……x并不是问题(不是真的使用过)

您应该看看下面的内容吗?或者也许x引用了R1C1中的列组件(您当前正在使用y作为行)。

Dim y As Long, r As Long ', x as long
r = 1
'for x = 1 to 25
For y = 2 To 102 Step 4
    ActiveWorkbook.Names.Add Name:="Name" & r, RefersToR1C1:="='Main '!R" & y & "C4" 'or maybe: "='Main '!R" & y & "C" & x  ???
    r = r + 1 'this should fix all of your names to keep showing up
Next y