[attach]16108[/attach]
[attach]16109[/attach]
程式碼如下:
Public Function Sample(product As Variant) As Variant
Application.Volatile
Dim oRange As Range
Dim rng As Range
Dim ws As Worksheet
Dim ExitLoop As Boolean
Dim FoundAt As String
Dim temp(18) As Variant
Dim i As Long
Dim MySearch As Variant
On Error GoTo Err
If TypeName(product) = "String" Then
MySearch = Range(product).Value
ElseIf TypeName(product) = "Range" Then
MySearch = product.Value
End If
With Sheets("material").Columns("A")
Set rng = .Find(What:=MySearch)
If Not rng Is Nothing Then
FirstAddress = rng.Address
Do
temp(i) = rng.Offset(0, 1).Text
Set rng = .FindNext(rng) '函數執行到這裡時,會出錯
'經偵錯,前面第一個find找到的位置$A$19沒錯
'本次的findnext執行後,rng變成nothing
'但正確的位置應該是$A$20
'If rng Is Nothing Then Exit Do
i = i + 1
Loop While Not rng Is Nothing And rng.Address <> FirstAddress
End If
End With
Sample = temp
Set rng = Nothing
Exit Function
Err:
Set rng = Nothing
MsgBox Err.Description
Sample = "N/A"
End Function作者: ltwinery 時間: 2013-9-19 00:09