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 –