0
我已經編寫了一個代碼,它將通過列A搜索特定元素,它將返回搜索元素的單元格地址。但是當我執行代碼時,我得到錯誤號13,說明類型不匹配。我已經完成了大量的代碼,但仍然沒有成功來應對錯誤。Excel VBA - 類型不匹配錯誤(錯誤號13)
這裏去我的VBA代碼
Private Sub CommandButton24_Click()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsx")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet2")
With WsEPC
.Activate
With .Range("A1:A10000")
' I am getting the error in the below Set cF module.
Set cF = .Find(What:="CTPT", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
num = cF.Address ' Here we will the get the cell address of CTPT
WsEPC.Range(cF.Offset(0, 1), cF.Offset(0, 2).End(xlDown)).Copy
WriteRow = WsCPT.Range("E" & WsCPT.Rows.Count).End(xlUp).Row + 1
WsCPT.Range("E" & WriteRow).PasteSpecial (xlPasteValues)
End with
End with
End Sub
任何幫助表示讚賞!
你確保了'.Find'成功
之前
Set cF
線?你可以在調試器的'num = cF.Address'行設置一個斷點並檢查結果。我懷疑自從你從'ActiveCell'開始搜索之後,你不知道這個活動單元在哪裏。爲什麼不從第一個單元格開始搜索(「A1」)? –我會避免使用這種''語句嵌套'。您可以將'With WsEPC'和'With .Range(「A1:A10000」)'更改爲'With WsEPC.Range(「A1:A10000」)'並且移除其中一個'End With' –
@ASH - The .Find成功了,它有一個值$ A $ 14 –