2013-09-21 4 views
-1

안녕 모두 내가이 VBA 프로그램을 만든 무엇을하고 각 시트를 통해 가서 일부 셀과 단어를 잘 지워요 내가 시트 7까지 그것을 만든 내가 그것을 실행하는 것을 멈출 필요가있다라고하면 내가 5시에 멈추고 오류가 나기 때문에 다른 두 개를 실행하려고하지 않는 5 장만 있습니다.엑셀 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 
+0

내가 필요한 것은 더 이상 매 다윗 Zemens 당신이 도와 줄 수 – Dmcovey1993

+0

남아 있지있을 경우 그것을 막을 수있는 방법은? – Dmcovey1993

+0

내 대답보기. 통합 문서를 복제하고 복제하지 않기 때문에 테스트되지 않지만 코드를보다 효율적으로 만드는 방법에 대한 좋은 아이디어를 제공합니다. –

답변

1

다음은 일반적인 조언입니다. Selection에 의존하지 마세요. (당신)이 코드를보고 :

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 "<-- This line is redundant 

이 매크로 레코더가 당신에게 코드를 제공하는 방법입니다 - 을 거의 모든 사람들이 엑셀에서 VBA 시작 그래서 치욕이 없다 어떻게입니다. 그러나 레코더는 모든 문자 입력, 선택, 활성화 등을 기록하는 매우 문자 적입니다. 을 보는 것이 유용하지만 거의 항상 통합 될 수 있습니다. 코드를 통합하면 사람이 읽을 수 있고 실행 속도가 빨라지고 유지 관리가 쉬워집니다.

With Sheets("1D_report") 
    .Rows("3:9").Delete Shift:=xlUP 
    .Range("E1:F2").ClearContents 
    .Range("H:H").ClearContents 
End With 

모방 포인트 앤 클릭 매크로를 작성, 내가 수정하는 대신 그 객체 (워크 시트, 셀, 범위/등)에 직접를 작동 :

이 코드에 있다고 비교 .

이제는 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 문을 사용하면 사례 값을 기반으로 특정 작업을 수행 할 수 있습니다.이 경우 우리는 워크 시트의 이름을 확인합니다. 그것은 워크 시트에 결코 행위 :)

을 존재하지 않는 것
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 
+0

와 함께 vba를 알았던 것을 보았다. 다음번에 컴파일없이 오류가 발생했다. – Dmcovey1993

+0

David 나는 당신과 이야기해야한다. – Dmcovey1993

+0

@pnuts 당신이 절대적으로 올바른 코드입니다.이 코드는 테스트되지 않았습니다. 내가 수정합니다. –

관련 문제