2017-07-18 125 views
0

我有下面的代码,但Excel是给我的错误91:对象变量或带块变量未设置错误对象变量,并与从Excel VBA块未设置错误

它特别突出,我设置LR1行等于查找公式。

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

请注意。帮帮我!

下面是代码:

'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 
+0

大概意思是这样LR1被分配到没有排它没有发现任何东西。 – SJR

回答

2

限定对象,以他们的父母:

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 
+0

谢谢你的帮助!工作很棒! –

相关问题