Excel VBA - 类型不匹配错误(错误号13)

时间:2015-11-24 02:20:33

标签: excel vba excel-vba

我编写了一个代码,它将通过A列搜索特定元素,并返回搜索元素的单元格地址。但是当我执行代码时,我收到错误号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

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

可能是由于After:=ActiveCell参数

尝试删除它,或添加

.Range("A1").Activate

Set cF

之前