Excel Vba需要停止
大家好,我制作了这个vba程序,它做的是遍历每一页,删除一些单元格和word。我把它制作到第7页,我需要一种方法来阻止它运行,比如说,如果只有5页,我希望它停止在5页,而不是尝试运行其他两页,因为它出错了 我在这方面很新,你也可以看看这个,看看你是否能够缩短它,或者让它运行得更好Excel Vba需要停止,excel,vba,Excel,Vba,大家好,我制作了这个vba程序,它做的是遍历每一页,删除一些单元格和word。我把它制作到第7页,我需要一种方法来阻止它运行,比如说,如果只有5页,我希望它停止在5页,而不是尝试运行其他两页,因为它出错了 我在这方面很新,你也可以看看这个,看看你是否能够缩短它,或者让它运行得更好 Sub Step1() ' 9/20/2013 ' Made by Douglas Covey Sheets("1D_report").Select Rows("3:9").Select
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
以下是一些一般性建议:停止依赖
选择
。看看这个代码(你的):
我没有编写一个模拟点击的宏,而是将其修改为直接处理对象(工作表、单元格、范围等)
现在,让我们只看一下您使用1D_Report
工作表所做的工作,并向您展示如何使用子例程/函数
Sub Test()
Dim r As Range
Dim s As String
Dim ws as Worksheet
If Not SearchAndClear(Worksheets("1D_report"), "Utilization, %", 8, 0) Then Exit Sub
If Not SearchAndClear(Worksheets("1D_report"), "Utilization, %", 0, 1) Then Exit Sub
If Not SearchAndClear(Worksheets("1D_report"), "Total Cost:", 0, 1) Then Exit Sub
End Sub
上述代码依赖于一个函数来执行可重复的操作。以下是函数:
Function SearchAndClear(ws As Worksheet, srchString As String, rOff As Long, cOff As Long) As Boolean
With ws
Set r = .Cells.Find(srchString, .Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
SearchAndClear = False
End If
.Range(r, r.Offset(rOff, cOff)).Clear
SearchAndClear = True
End With
End Function
将所有内容放在一起…
这是未经测试的,但我认为应该做你所做的一切。它的代码要少得多,如果您遇到问题或需要修改内容,则更易于阅读和调试
为可重复代码创建函数/子例程很有价值,这样您就不需要重复它,只需多次调用函数/子例程。如果需要更改代码,这只是将来需要修复或修改的一件事情,而不是需要更新的许多事情
使用Select Case
语句可以根据Case值执行特定操作,在本例中,我们检查工作表的名称。它永远不会对不存在的工作表执行操作:)
子测试()
调光范围
像线一样变暗
将ws设置为工作表
对于ActiveWorkbook.Worksheets中的每个ws
选择Case ws.Name
案例“1D_报告”
与ws
.行(“3:9”)。删除移位:=xlUp
.范围(“E1:F2”).清晰内容
.范围(“H:H”).清晰内容
以
如果没有SearchAndClear(ws,“利用率,%”,8,0),则退出Sub
如果没有SearchAndClear(ws,“利用率,%”,0,1),则退出Sub
如果没有SearchAndClear(ws,“总成本:”,0,1),则退出Sub
ws.Name=“即将发布的报告”
案例“1D_1”、“1D_2”、“1D_3”、“1D_4”、“1D_5”、“1D_6”'我需要的是一种方法来阻止它,如果没有更多的床单了David Zemens,你能帮我吗?请参阅下面我的答案。这是未经测试的,因为我不会尝试复制您的工作簿,但它会让您对如何提高代码的效率有一个很好的了解。我很好奇您是如何想问我的名字的,虽然:)转到您的个人资料,看到您知道vba与excel=DDavid我需要谈谈you@pnuts谢谢你绝对正确此代码未经测试我将进行修订我还省略了与每个ws的对应的下一个语句(现在也进行了修订:)
Sub Test()
Dim r As Range
Dim s As String
Dim ws as Worksheet
If Not SearchAndClear(Worksheets("1D_report"), "Utilization, %", 8, 0) Then Exit Sub
If Not SearchAndClear(Worksheets("1D_report"), "Utilization, %", 0, 1) Then Exit Sub
If Not SearchAndClear(Worksheets("1D_report"), "Total Cost:", 0, 1) Then Exit Sub
End Sub
Function SearchAndClear(ws As Worksheet, srchString As String, rOff As Long, cOff As Long) As Boolean
With ws
Set r = .Cells.Find(srchString, .Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
SearchAndClear = False
End If
.Range(r, r.Offset(rOff, cOff)).Clear
SearchAndClear = True
End With
End Function
Sub Test()
Dim r As Range
Dim s As String
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Select Case ws.Name
Case "1D_report"
With ws
.Rows("3:9").Delete Shift:=xlUp
.Range("E1:F2").ClearContents
.Range("H:H").ClearContents
End With
If Not SearchAndClear(ws, "Utilization, %", 8, 0) Then Exit Sub
If Not SearchAndClear(ws, "Utilization, %", 0, 1) Then Exit Sub
If Not SearchAndClear(ws, "Total Cost:", 0, 1) Then Exit Sub
ws.Name = "Comingsoon_report"
Case "1D_1", "1D_2", "1D_3", "1D_4", "1D_5", "1D_6" '<-- You do the same operations on ALL of these sheets!
With ws
.Rows("4:9").Delete Shift:=xlUp
End With
If Not SearchAndClear(ws, "Qty:", 0, 1) Then Exit Sub
Set r = ws.Cells.Find(What:="Page", After:=ws.Range("E8"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
r.Replace What:="Page", Replacement:="Program", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Case Else
'You could add additional logic for other worksheets, if needed
'
'
End Select
Next
End Sub
Function SearchAndClear(ws As Worksheet, srchString As String, rOff As Long, cOff As Long) As Boolean
With ws
Set r = .Cells.Find(srchString, .Range("A1"))
If r Is Nothing Then
MsgBox s & " could not be found" & vbCrLf & "I'am going on break"
SearchAndClear = False
End If
.Range(r, r.Offset(rOff, cOff)).Clear
SearchAndClear = True
End With
End Function