VBA:如何按定义的名称显示单元格值?

发布于 2024-09-26 23:17:10 字数 469 浏览 3 评论 0原文

我已经为工作表中的特定列定义了名称。 如何通过定义的名称显示单元格值?

我已经尝试过这些:

  Public Sub Test()
    Dim R As Range
    Set R = ThisWorkbook.Names("SomeName").RefersToRange
    MsgBox CStr(R.Value)
  End Sub

但发生运行时错误“类型不匹配”(错误代码:13)。

代码有什么问题吗? RefersToRange.Value 的实际数据类型是什么?

文档说 RefersToRange 返回 Range 对象,但它似乎与 ActiveCell 返回的 Range 对象不同,因为我对以下代码没有问题:

  MsgBox CStr(ActiveCell.Value)

我一直在使用 Excel 2003

I have already a defined name for a particular column in my worksheet.
How to display a cell value by its defined name?

I've tried these:

  Public Sub Test()
    Dim R As Range
    Set R = ThisWorkbook.Names("SomeName").RefersToRange
    MsgBox CStr(R.Value)
  End Sub

but run-time error occured "Type Mismatch" (error code: 13).

What's wrong with the code?
What is the actual data type of RefersToRange.Value?

The documentation says that RefersToRange returns the Range object, but it seems differ with the Range object returned by ActiveCell, because I've no problem with the following code:

  MsgBox CStr(ActiveCell.Value)

I've been using Excel 2003

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

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

发布评论

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

评论(4

把人绕傻吧 2024-10-03 23:17:10

RefersToRange 确实返回一个 Range 对象。我假设您在 Cstr 线上发现类型不匹配。如果该区域有多个单元格,则 Value 属性返回一个 Variant 数组。在立即窗口中执行此操作(VBE 中的 Control+G)。

?typename(thisworkbook.Names("SomeRange").RefersTorange)
Range
?typename(thisworkbook.Names("SomeRange").RefersToRange.Value)
Variant()

CStr 函数无法处理数组参数,这就是您收到该错误的原因。您可以访问该范围内的特定单元格,例如第一个单元格,如下所示

ThisWorkbook.Names("SomeRange").RefersToRange(1).Value

,或者按照 Fink 的建议循环遍历它们。

RefersToRange does return a Range object. I assume you're getting your Type Mismatch on the Cstr line. If the range has multiple cells, the Value property returns a Variant array. Do this in the Immediate Window (Control+G in the VBE).

?typename(thisworkbook.Names("SomeRange").RefersTorange)
Range
?typename(thisworkbook.Names("SomeRange").RefersToRange.Value)
Variant()

The CStr function can't handle an array argument and that's why you're getting that error. You can get to a particular cell in the range, like the first cell, like this

ThisWorkbook.Names("SomeRange").RefersToRange(1).Value

Or loop through them as Fink suggests.

月亮是我掰弯的 2024-10-03 23:17:10

如果范围跨越多个单元格,您需要迭代该范围内的单元格并分别获取每个单元格的值。否则会抛出错误。

Sub Test()

    Dim r As Range
    Dim cell As Range

    Set r = Range("Something")

    If r.Cells.Count > 1 Then
        For Each cell In r.Cells
            MsgBox cell.Value
        Next cell
    Else
        MsgBox r.Value
    End If
End Sub

但是,您可以通过设置多单元格区域的值来将该值设置为区域中定义的所有单元格,如下所示:

Sub Test()

    Dim r As Range

    Set r = Range("Something")

    r.Value = "Test"
End Sub

You Need to iterate through the cells within that range and get the value of each cell separately if the range spans multiple cells. Otherwise it throws an error.

Sub Test()

    Dim r As Range
    Dim cell As Range

    Set r = Range("Something")

    If r.Cells.Count > 1 Then
        For Each cell In r.Cells
            MsgBox cell.Value
        Next cell
    Else
        MsgBox r.Value
    End If
End Sub

However, you can set the value to all of the cells defined in the range by setting the value of a multi-cell range like this:

Sub Test()

    Dim r As Range

    Set r = Range("Something")

    r.Value = "Test"
End Sub
吻安 2024-10-03 23:17:10

您应该能够只向 Range(甚至 Cells)属性提供名称:

Set R = ThisWorkbook.Range("SomeName")

You should be able to just supply the name to the Range (or even the Cells) property:

Set R = ThisWorkbook.Range("SomeName")
梦幻的心爱 2024-10-03 23:17:10

您可以通过名称获取单元格值,如下所示:-

Workbook.Range("SomeName").Value

You can get cell value by its name as follows:-

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