2014-02-18 3 views
0

Excel에서 원하지 않는 행을 삭제하는 매크로가 있습니다. 오전 9시 30 분 이전과 오후 4시 이후에 행을 삭제합니다. 어떤 이유로 인해 오전 10시에서 오전 10시 9 분, 오전 11시에서 11시 9 분, 오후 12시에서 12시 9 분, 오후 1시에서 1 시까 지의 행을 삭제합니다. 오후 2 시부 터 오후 2시 9 분, 오후 3 시부 터 오후 3시 9 시까 지엑셀 추가 행 삭제

해당 행을 삭제하지 않도록 도와주세요.

내 코드 :

Sub DeleteRows() 

Dim lastRow As Long 
Dim Cell As Long 
Dim dt As Date 

'Work with the active sheet. 
With ActiveSheet 

    'Find the last row of your dataset. 
    lastRow = .Range("A:A").Find("*", searchdirection:=xlPrevious).Row 

    'Format your time column to a readable time. 
    .Columns("B").NumberFormat = "[$-F400]h:mm:ss AM/PM" 

    'Loop through the rows, beginning at the bottom. 
    For Cell = lastRow To 2 Step -1 

     'Piece together the date. 
     dt = Mid(.Cells(Cell, 1), 5, 2) & "/" & _ 
      Mid(.Cells(Cell, 1), 7, 2) & "/" & Left(.Cells(Cell, 1), 4) 

     'If the date is a Sat or Sun, delete the row. 
     If Weekday(dt) = 1 Or Weekday(dt) = 7 Then 
      .Rows(Cell).EntireRow.Delete 

     'If the time is before 9:30am or after 4pm, delete the row. 
     ElseIf CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2))) < 930 Or _ 
    CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2))) > 1600 Then 
      .Rows(Cell).EntireRow.Delete 
     End If 
    Next Cell 
End With 

MsgBox "Done!" 

답변

1

당신은 실제 time에 대해 값을 확인 시도해 볼 수도 있습니다 :

'If the time is before 9:30am or after 4pm, delete the row. 
     ElseIf .Cells(Cell, 2) - Int(.Cells(Cell, 2)) < CDate("09:30:00") Or _ 
     .Cells(Cell, 2) - Int (.Cells(Cell, 2)) > CDate("16:00:01") Then 
    .Rows(Cell).EntireRow.Delete 

참고 상한 16:00:0116:00:00에서 (아마도 반올림) 경계 평가 오류를 방지 할 수 있습니다.

+0

감사합니다. 너는 천재 야 – user1681664

1

대신 사용

CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2))) 

사용 예를 들어, 11시 8분 (11)로 변환되고, 때문에이 문제를 타격

100*Hour(.Cells(Cell, 2)) + Minute(.Cells(Cell, 2)) 

을 시간과 8 분. 코드에서 함께 연결하면 118 (빈의 삭제 조건을 충족 함) g 930 미만). 수정 된 버전 (테스트되지 않음)은이를 1108로 변환하고 삭제 조건을 충족시키지 않아야합니다.

+0

그냥 셀 값을 사용하고 조건을 <.4 or > .66? – pnuts

+0

<.4는 실제로 09:35 또는 그 이전에 시간이있는 행을 삭제합니다! – DMM

+0

충분하지만 - 내 시계는 항상 빠릅니다 :-) – pnuts