断开代码中分配给命名范围的地址与生成的命名范围地址之间的连接

发布于 2024-10-17 16:08:37 字数 1917 浏览 3 评论 0原文

我正在尝试在 Excel 2003(不是我选择的版本)中编写 VBA 脚本,将工作表上的预定义范围划分为十个命名范围。工作表名称是“paste_data”,我想要将脚本限制到的单元格“块”是 A4:AO111。有时,当我运行脚本时,它可以工作,但在其他时候,它似乎将有效的起始单元格从 A4 转移到另一个单元格。这是一个不良结果的示例(抱歉,我无法发布图像,因为我是新人):

命名范围 table.emergency.count 引用了范围 V6:AO25,而它应该引用范围 V4:AO23。

我的代码在这里:

Sub tables_assign()
Dim j As Integer
Dim range_ref, range_name, rref As String
Dim tbles(1 To 10) As String
Dim rw1, rw2 As Integer
'##########################################################################################
'CREATION AND NAMING OF TABLES
'##########################################################################################
tbles(1) = "table.emergency.score": tbles(2) = "table.emergency.count": tbles(3) = "table.eol.score": tbles(4) = "table.eol.count": tbles(5) = "table.inpatient.score": tbles(6) = "table.inpatient.count": tbles(7) = "table.outpatient.score": tbles(8) = "table.outpatient.count": tbles(9) = "table.sds.score": tbles(10) = "table.sds.count"
For j = 1 To 10
    If j Mod 2 <> 0 Then
        If j = 1 Then
            rw1 = 4
            rw2 = 23
        Else
            rw1 = 4 + 22 * Application.WorksheetFunction.Ceiling((j / 2 - 1), 1)
            rw2 = 23 + 22 * Application.WorksheetFunction.Ceiling((j / 2 - 1), 1)
        End If
        rref = Trim(Application.WorksheetFunction.Substitute("=paste_data!A" & Str(rw1) & ":T" & Str(rw2), " ", ""))
        ActiveWorkbook.Names.Add tbles(j), rref
    Else
        If j = 2 Then
            rw1 = 4
            rw2 = 23
        Else
            rw1 = 4 + 22 * (j / 2 - 1)
            rw2 = 23 + 22 * (j / 2 - 1)
        End If
        rref = Trim(Application.WorksheetFunction.Substitute("=paste_data!V" & Str(rw1) & ":AO" & Str(rw2), " ", ""))
        ActiveWorkbook.Names.Add tbles(j), rref
    End If
Next j
End Sub

有谁知道为什么会发生这种情况?我的直觉是工作表的“usedrange”是罪魁祸首。

I am trying to write a VBA script in Excel 2003 (not my choice of version) to partition a predefined range on a worksheet into ten named ranges. The worksheet name is "paste_data" and the 'block' of cells that I want to confine the script to is A4:AO111. Sometimes, when I run the script, it works, but at other times, it seems to shift the effective starting cell from A4 to another cell. Here is an example of bad results (sorry, I can't post an image because I'm new):

The named range table.emergency.count refers to range V6:AO25 when it should refer to range V4:AO23.

My code is here:

Sub tables_assign()
Dim j As Integer
Dim range_ref, range_name, rref As String
Dim tbles(1 To 10) As String
Dim rw1, rw2 As Integer
'##########################################################################################
'CREATION AND NAMING OF TABLES
'##########################################################################################
tbles(1) = "table.emergency.score": tbles(2) = "table.emergency.count": tbles(3) = "table.eol.score": tbles(4) = "table.eol.count": tbles(5) = "table.inpatient.score": tbles(6) = "table.inpatient.count": tbles(7) = "table.outpatient.score": tbles(8) = "table.outpatient.count": tbles(9) = "table.sds.score": tbles(10) = "table.sds.count"
For j = 1 To 10
    If j Mod 2 <> 0 Then
        If j = 1 Then
            rw1 = 4
            rw2 = 23
        Else
            rw1 = 4 + 22 * Application.WorksheetFunction.Ceiling((j / 2 - 1), 1)
            rw2 = 23 + 22 * Application.WorksheetFunction.Ceiling((j / 2 - 1), 1)
        End If
        rref = Trim(Application.WorksheetFunction.Substitute("=paste_data!A" & Str(rw1) & ":T" & Str(rw2), " ", ""))
        ActiveWorkbook.Names.Add tbles(j), rref
    Else
        If j = 2 Then
            rw1 = 4
            rw2 = 23
        Else
            rw1 = 4 + 22 * (j / 2 - 1)
            rw2 = 23 + 22 * (j / 2 - 1)
        End If
        rref = Trim(Application.WorksheetFunction.Substitute("=paste_data!V" & Str(rw1) & ":AO" & Str(rw2), " ", ""))
        ActiveWorkbook.Names.Add tbles(j), rref
    End If
Next j
End Sub

Does anyone have an idea why this would happen? My hunch is that the worksheet's 'usedrange' is the culprit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2024-10-24 16:08:37

当您在定义的名称中使用相对引用时,定义是相对于活动单元格的。为了避免这种情况,请使用绝对引用,例如 $V$4:$AO$23。使用绝对引用,命名范围将始终指向相同的单元格。

例子:
选择单元格 A1 并将名称 test_relative 定义为“=A1”。现在选择单元格 B10 并重新打开定义的名称框,选择 test_relative,您将看到类似“=Sheet1!B10”的内容。

要修复代码,请在范围引用中插入 $

rref = Trim(Replace("=paste_data!$A$" & Str(rw1) & ":$T$" & Str(rw2), " ", ""))

另请注意,

Dim rw1, rw2 As Integer

尺寸 rw1 作为变体。使用

Dim rw1 As Integer, rw2 As Integer

When you use relative references in defined names, the definition is relative to the activecell. To avoid that, use absolute references, like $V$4:$AO$23. With absolute references, the named range will always point to the same cells.

Example:
Select cell A1 and define the name test_relative as "=A1". Now select cell B10 and reopen the defined name box, select test_relative and you'll see something like "=Sheet1!B10"

To fix your code, insert the $ in the range references

rref = Trim(Replace("=paste_data!$A$" & Str(rw1) & ":$T$" & Str(rw2), " ", ""))

Also note that

Dim rw1, rw2 As Integer

dimensions rw1 as a Variant. Use

Dim rw1 As Integer, rw2 As Integer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文