首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在vba中,我得到一个未设置的对象变量(错误91)

在vba中,我得到一个未设置的对象变量(错误91)
EN

Stack Overflow用户
提问于 2018-06-09 06:34:00
回答 1查看 58关注 0票数 1

这是我的一个基本错误或缺乏理解。我在这里搜索了许多问题,似乎没有一个是适用的。

以下是代码

代码语言:javascript
复制
Option Explicit

Public Function ReturnedBackGroundColor(rnge As Range) As Integer
    ReturnedBackGroundColor = rnge.Offset(0, 0).Interior.ColorIndex
End Function

Public Function SetBackGroundColorGreen()
    ActiveCell.Offset(0, 0).Interior.ColorIndex = vbGreen
End Function

Public Function CountBackGroundColorGreen(rnge As Range) As Integer
    Dim vCell As Range

CountBackGroundColorGreen = 0

For Each vCell In rnge.Cells
    With vCell
        If ReturnedBackGroundColor(vCell) = 14 Then
            CountBackGroundColorGreen = CountBackGroundColorGreen + 1
        End If
    End With
Next
End Function

Public Function GetBackgroundColor() As Integer
Dim rnge As Range

GetBackgroundColor = 3
rnge = InputBox("Enter Cell to get Background color", "Get Cell Background Color")

GetBackgroundColor = ReturnedBackGroundColor(rnge)
End Function

我添加了最后一个函数,在此之前所有其他函数都正常工作,并且在该函数的第一个语句中得到错误。

对于该错误,可能的修复方法之一是在适当的库中添加引用。我不知道应该引用什么库,也找不到InputBox包含什么库。这是一个activeX控件,但我在tools->reference下拉菜单中看不到它。我确实检查了microsoft forms 2.0。

我尝试过各种set语句,但我认为我添加的唯一对象是inputbox。

有什么建议吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 06:50:00

使用application.inputbox,添加类型为range,设置返回的range对象。

代码语言:javascript
复制
Option Explicit

Sub main()
    Debug.Print GetBackgroundColor()
End Sub

Public Function GetBackgroundColor() As Integer
    Dim rnge As Range        
    Set rnge = Application.InputBox(prompt:="Enter Cell to get Background color", _
                                    Title:="Get Cell Background Color", _
                                    Type:=8)        
    GetBackgroundColor = ReturnedBackGroundColor(rnge)
End Function

Public Function ReturnedBackGroundColor(rnge As Range) As Integer
    ReturnedBackGroundColor = rnge.Offset(0, 0).Interior.ColorIndex
End Function
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50769193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档