首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对象变量,并使用excel vba中的块设置错误。

对象变量,并使用excel vba中的块设置错误。
EN

Stack Overflow用户
提问于 2017-07-18 15:46:59
回答 1查看 349关注 0票数 0

我有以下代码,但excel给了我错误91: Object变量或带块变量不设置错误

它特别突出显示了我将lr1设置为查找公式的行。

我不知道它为什么要这么做。

请给我。帮助!

以下是代码:

代码语言:javascript
运行
复制
'Start Argument
'This Argument will deal with locating the last empty row and inserting Free Rent & Recoveries Headers
Sub LER()

'The variable lr1 is set as a Long Number
Dim lr1 As Long

'The variable lc1 is set as a Long Number
Dim lc1 As Long

'The variable sr1 is set as a Long Number
Dim sr1 As Long

Sheets("Sheet1").Range("A1").Activate

'The variable lr1 is used to store the find formula to locate the last row of cells containing any data
lr1 = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

'The variable sr1 is used to go to the truly last row of empty cells with no data whatsoever.
'This is always after the last row of cells with data, hence the "lr1 + 1" formula utilization
sr1 = lr1 + 1

'Cells from the last empty row in coloumns A through G are selected to be merged and centered
Union(Cells(sr1, 1), Cells(sr1, 2), Cells(sr1, 3), Cells(sr1, 4), Cells(sr1, 5), Cells(sr1, 6), Cells(sr1, 7)).Select

MsgBox "The last Cell is:" + sr1

'End of Argument
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-18 15:52:02

将对象限定为其父对象:

代码语言:javascript
运行
复制
Dim ws1 as Worksheet
Set ws1 = Worksheets("Sheet1")

With ws1

    Dim rLastCell as Range
    Set rLastCell = .Cells.Find(What:="*", After:=.Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)

    Dim lr1 as Long
    lr1 = rLastCell.Row

    '... more code

End With
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45171598

复制
相关文章

相似问题

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