大家好我做了这个VBA程序,它是做什么是去通过每个工作表,并删除一些单元格和单词的好,我做了它到表7我需要一种方法来阻止它运行说如果只有5张我希望它停止在5,而不是尝试运行其他两个,因为它出错了。Excel Vba需要停止
我是非常非常新的这个,你也可以看看这个,看看你是否能够缩短它,或者使它运行得更好。
Sub Step1()
' 9/20/2013
' Made by Douglas Covey
Sheets("1D_report").Select
Rows("3:9").Select
Selection.Delete Shift:=xlUp
Range("E1:F2").Select
Selection.ClearContents
Columns("H:H").Select
Selection.ClearContents
Selection.ClearContents
'
' Search and Delete.
'
Dim r As Range
Dim s As String
s = "Utilization, %"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(8, 0)).Clear
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Clear
s = "Total Cost:"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Clear
Sheets("1D_report").Name = "Comingsoon_report"
'
' Sheet Number Two
'
Sheets("1D_1").Select
Rows("4:9").Select
Selection.Delete Shift:=xlUp
s = "Qty:"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Delete Shift:=xlUp
Range("E8").Select
Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="Page", Replacement:="Program", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'
' Sheet Number Tree
'
Sheets("1D_2").Select
Rows("4:9").Select
Selection.Delete Shift:=xlUp
s = "Qty:"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Delete Shift:=xlUp
Range("E8").Select
Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="Page", Replacement:="Program", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'
' Sheet Number Four
'
Sheets("1D_3").Select
Rows("4:9").Select
Selection.Delete Shift:=xlUp
s = "Qty:"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Delete Shift:=xlUp
Range("E8").Select
Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="Page", Replacement:="Program", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'
' Sheet Number Five
'
Sheets("1D_4").Select
Rows("4:9").Select
Selection.Delete Shift:=xlUp
s = "Qty:"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Delete Shift:=xlUp
Range("E8").Select
Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="Page", Replacement:="Program", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'
' Sheet Number Six
'
Sheets("1D_5").Select
Rows("4:9").Select
Selection.Delete Shift:=xlUp
s = "Qty:"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Delete Shift:=xlUp
Range("E8").Select
Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="Page", Replacement:="Program", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'
' Sheet Number Seven
'
Sheets("1D_6").Select
Rows("4:9").Select
Selection.Delete Shift:=xlUp
s = "Qty:"
Set r = Cells.Find(What:=s, After:=Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
Exit Sub
End If
Range(r, r.Offset(0, 1)).Delete Shift:=xlUp
Range("E8").Select
Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="Page", Replacement:="Program", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
我需要的是一种方法来阻止它,如果没有更多的张留 – Dmcovey1993
大卫Zemens你能帮忙吗? – Dmcovey1993
请参阅下面的答案。它没有经过测试,因为我不打算尝试复制您的工作簿,但它应该让您了解如何使您的代码更高效。 –