简体   繁体   English

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

[英]VBA Excel - Error when trying to create range using a function

I'm not sure where the problem is with this bit of code I worked out. 我不知道问题出在哪里,我编写了这些代码。 I have the following public declarations and function: 我有以下公共声明和功能:

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

The code above should establish any range inside a given table ListObject. 上面的代码应该在给定的表ListObject中建立任何范围。 However, when I add the following into a Sub 但是,当我将以下内容添加到Sub中时

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

g_0.Select

I get the error "91 - Object variable or With block variable not set". 我收到错误“91 - 对象变量或未设置块变量”。 I'm not sure what I'm missing... 我不确定我错过了什么......

In your function use 在你的功能使用

Set RngMk = Range(str)

This should solve your issue. 这应该可以解决您的问题。

And still "set" g_0 = rngmk(.....) 仍然“设置”g_0 = rngmk(.....)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM