如何将此查找和移动代码应用于我的Excel工作簿中的所有工作表?

时间:2016-07-18 21:59:56

标签: excel excel-vba macros vba

Sub FindSchedule()
Dim xsheet As Worksheet

For Each ws In ThisWorkbook.Worksheets
With ws
With Application.FindFormat.Font
    .FontStyle = "Italic"
    .Superscript = False
    .Subscript = False
    .TintAndShade = 0
End With
Cells.Find(What:="Schedule", LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=True, SearchFormat:=True).Activate
Selection.Copy
Range("B1").Select
ActiveSheet.Paste
End With
Next ws
End Sub

这是我试图在我的所有“工作表”上运行的代码。虽然它似乎对大约一半的纸张起作用,但对于另一半,B1槽最终变成空白。我不太清楚为什么会发生这种情况,因为手动完成宏编程就可以正常工作。

作为旁注,这个宏也需要比我想象的要长得多。

1 个答案:

答案 0 :(得分:0)

我在这里猜一点,但有点像:

Sub FindSchedule()
    Dim xsheet As Worksheet

    With Application.FindFormat.Font
        .FontStyle = "Italic"
        .Superscript = False
        .Subscript = False
        .TintAndShade = 0
    End With

    For Each ws In ThisWorkbook.Worksheets

        ws.Cells.Find(What:="Schedule", LookIn:=xlFormulas, _
           LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
           MatchCase:=True, SearchFormat:=True).Copy ws.Range("B1")

    Next ws
End Sub