从 Ms-Word 中将工作簿作为对象打开并使用复选框查找文本,删除整行然后关闭工作簿

Opening Workbook as object from Ms-Word and Find text using checkbox, Delete entire Row then Close workbook

我的情况是,我从 MS Word 打开 Workbook,在那里我找到了一些文本并删除了整行找到的文本。为此,我使用 Userform 具有复选框和按钮。下面的代码循环遍历所有复选框,如果 C.Value return True 它通过按 CEEMEA 按钮执行操作。

我在 Excel Object 命名 Xc 时遇到问题。

我第一次 运行 CEEMEA 宏 运行 正确(打开工作簿-->查找文本-->删除行-->关闭工作簿等);

但是第二次,它 return 错误 Run-time Error '13': Type mismatch. ,到目前为止我认为第一次 运行 可能还剩下一些东西,我没有 Quit/Close/set 一无所获,(重复工作簿)

我检查了所有的拼写,一切都是正确的。

最后我有 Xc.Quit,还有 Set Xc= Nothing

我不明白哪里出了问题。我首先想到的是 运行 可能有些东西我没有 QuitSetNothing。我把整个代码放在下面。请帮助...

如果有更好的方法来完成这项工作,请提出建议。

Dim Xc As Object
Set Xc = CreateObject("Excel.Application")
Xc.Visible = True
Set Wb = Xc.Workbooks.Open("C:\Users\dell\Desktop\EMEA CEEMEA\EMEA CC FINAL LIST.xls")

Dim C As MSForms.Control
For Each C In Me.Controls
    If TypeName(C) = "CheckBox" Then
    If C.Value = True Then
    If C.Caption = "Select All" Then
    Else

    Dim ff As String
    ff = Trim(C.Caption)
  With Wb
        .Application.Sheets("Sheet2").Select
        .Application.Range("A1").Select

        .Application.Cells.Find(What:=ff, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        .Application.ActiveCell.Rows("1:1").EntireRow.Select
        .Application.Selection.Delete Shift:=xlUp
        .Application.Range("A1").Select

  End With

    End If
    End If
    End If
Next C

Wb.Close SaveChanges:=True
Workbooks.Close
Set Wb = Nothing
Xc.Quit
Set Xc = Nothing

您不需要打开工作簿并使其可见。这可能对你有用。 Workbooks.close 将关闭所有存在的工作簿。

Dim WB As Workbook
Set WB = Workbooks.Open("C:\Users\dell\Desktop\EMEA CEEMEA\EMEA CC FINAL LIST.xls")
Dim C As MSForms.Control
For Each C In Me.Controls
If TypeName(C) = "CheckBox" Then
If C.Value = True Then
If C.Caption = "Select All" Then
Else


With WB
    .Application.Sheets("Sheet2").Select
    .Application.Range("A1").Select

    .Application.Cells.Find(What:=C.Caption, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

    .Application.ActiveCell.Rows("1:1").EntireRow.Select
    .Application.Selection.Delete Shift:=xlUp

End With

End If
End If
End If
Next C

WB.Close SaveChanges:=True
Workbooks.Close
Set WB = Nothing