VBA Excel - 尝试使用函数创建范围时出错

时间:2015-02-18 18:50:33

标签: excel vba excel-vba

我不确定问题出在哪里,我解决了这段代码。我有以下公开声明和功能:

Public g_0 As Range
Public Enum RngType
    A = 1
    H = 2
    X = 3
End Enum


Function RngMk(csTable As String, csType As RngType, Optional csHeaderName As Variant = "")

Dim str As String

Select Case csType
    Case RngType.A
        If csHeaderName = "" Then
            str = csTable & "[#All]"
        Else:
            str = csTable & "[[#All],[" & csHeaderName & "]]"
        End If
    Case RngType.H
        If csHeaderName = "" Then
            str = csTable & "[#Headers]"
        Else:
            str = csTable & "[[#Headers],[" & csHeaderName & "]]"
        End If
    Case RngType.X
        If csHeaderName = "" Then
            str = csTable
        Else:
            str = csTable & Chr(91) & csHeaderName & Chr(93)
        End If
End Select

RngMk = Range(str)

End Function

上面的代码应该在给定的表ListObject中建立任何范围。但是,当我将以下内容添加到Sub

中时
g_0 = RngMk("Table1", A, "Name")

g_0.Select

我收到错误" 91 - 对象变量或With block变量未设置"。我不确定我错过了什么......

1 个答案:

答案 0 :(得分:0)

在你的功能中使用

Set RngMk = Range(str)

这可以解决您的问题。

仍然"设置" g_0 = rngmk(.....)