2012-08-22 72 views
1

我正在製作一個宏,它將搜索一個列表並查找列表中的所有條目,這些條目中的第一個是spectraseven。這將是將這些記錄複製到每個條目的表單中。運行時錯誤'1004':應用程序定義的錯誤或對象定義的錯誤

隨着代碼到目前爲止它失敗:

運行時錯誤「1004」:應用程序定義或對象定義的錯誤。

Sub testWild() 
startCell = 0 
Dim FoundCell As Range 
Dim LastCell As Range 
Dim FirstAddr As String 
cellRange = "A1:A20" 
topCount = startCell 
With Range("A1:A20") 
Set LastCell = .Cells(.Cells.Count) 
End With 
Dim findString As String 
findString = "spectraseven*" 
Set FoundCell = Range(cellRange).Find(what:=findString, after:=LastCell) 



If Not FoundCell Is Nothing Then 
FirstAddr = FoundCell.Address 
End If 
Do Until FoundCell Is Nothing 
    Debug.Print FoundCell.Address 
Set FoundCell = Range(cellRange).FindNext(after:=FoundCell) 

Count = FoundCell.Row 
    Set FoundCell = Range(cellRange).FindNext(after:=FoundCell) 

---> Sheets(1).Range("a" & topCount) = Sheets(1).Range("e" & Count) 
    topCount = topCount + 1 



If FoundCell.Address = FirstAddr Then 
    Exit Do 
End If 
Loop 
End Sub 

的箭頭指向錯誤。

回答

1

您設置startCell等於0

然後,設置topCount = startCell

你不做任何其他與topCount開始循環之前。

因此,這樣的:

Sheets(1).Range("a" & topCount) = Sheets(1).Range("e" & Count) 

則計算結果爲

Sheets(1).Range("a0") = Sheets(1).Range("e" & FoundCell.Row) 

有沒有這樣的東西作爲細胞A0。嘗試從startCell開始1.

+0

謝謝。我想我習慣於其他編程語言,從0開始 – monkthemighty

+0

澄清,數值(整數,長,雙,單)變量從0開始在VBA中,但是,當直接在Excel中引用單元格像'Range( 「A1」)'它們從1開始。另外,當引用一個範圍內的單元格時,還必須使用1個鹼基,因此'Range(「A1:A10」)。Cells(1,1).Address = $ A $ 1' –

相關問題