2016-10-17 142 views
0

我有一個宏爲前36行工作,但然後顯示運行時91錯誤。它顯示find語句中的錯誤。宏的目的是計算列中數據的第90百分位數,計算等於或大於百分位的值的數量,並提供各部門之間的劃分。任何人都可以幫我糾正錯誤嗎?VBA運行時錯誤91 - 2

For bb = 1 To temcnt 
cc = Sheets("tem").Cells(bb, ttc + 4).Value 
Sheets("Geographic Strength").Activate 
Cells.Find(What:=cc, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
    :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
    False, SearchFormat:=False).Activate 

    ff1 = ActiveCell.Column 
    Sheets("tem").Activate 
    rnggg = Range(Cells(2, 6), Cells(ttr, 6)) 
    mamm = WorksheetFunction.CountIf(Range(Cells(2, 6), Cells(ttr, 6)), cc) 
Sheets("geographic strength").Activate 
f222 = Sheets("individual strength").Cells(1, iii).Value 

**Cells.Find(What:=f222, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
    :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
    False, SearchFormat:=False).Activate 
    f333 = ActiveCell.Row** 

'Error is in the above statement(Cells.Find) 

Cells(f333, ff1).Value = mamm 
Next bb 
Sheets("tem").Delete 
Next iii 
Application.DisplayAlerts = True 
Application.ScreenUpdating = True 
End Sub 
+0

使用'.Activate','.Select'等通常是一個壞主意b因爲它會放慢宏觀並增加犯錯的風險。 [Here](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)和[here](http://stackoverflow.com/documentation/excel -vba/1107/vba-best-practices/9292/avoid-using-select-or-activate)是如何避免它的一些提示。 – arcadeprecinct

回答

1

這是因爲

Cells.Find(What:=f222, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
    :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
    False, SearchFormat:=False) 

沒有找到想要的細胞,因此返回Nothing成功,你不能ActivateNothing

,所以你可以去像如下:

'... your code before 
f222 = Sheets("individual strength").Cells(1, iii).Value 

Dim found As Range 
Set found = Cells.Find(What:=f222, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ 
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ 
False, SearchFormat:=False) 

If Not found Is Nothing Then '<-- check if any cell has been found 
    found.Activate 
    f333 = ActiveCell.Row 

    '... rest of your code should f222 have been found 

End If 

' rest of your code 
+0

非常感謝您的幫助。它現在正在完美工作。 :) –