2014-04-07 6 views
1

사용자에게 지정된 셀 범위를 자동으로 지정하기위한 VBA 매크로가 있는데 제대로 작동합니다. 그러나 사용자가 지정된 범위에서 행을 삭제하려고하면 내가 무한 루프로 빌드 한 오류 메시지가 트리거됩니다.Excel VBA 매크로 행 삭제 무한 루프

코드는 다음과 같습니다

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim rTimeCells As Range 

    Set rTimeCells = Range("D2:G15") 

    Application.EnableEvents = False 


    If Not Application.Intersect(rTimeCells, Range(Target.Address)) Is Nothing Then 
      Call Time_Format(Range(Target.Address)) 
    End If 

    Application.EnableEvents = True 

    End Sub 

    Private Sub Time_Format(rCells As Range) 
    Dim RegEXFormat1 As Object 
    ... 
    Dim RegEXFormatX As Object 

    Set RegEXFormat1 = CreateObject("VBScript.RegExp")  
    ... 
    Set RegEXFormatX = CreateObject("VBScript.RegExp") 


    RegEXFormat1.Global = True 
    ... 
    RegEXFormatX.Global = True 


    RegEXFormat1.IgnoreCase = True 
    ... 
    RegEXFormatX.IgnoreCase = True 

    RegEXFormat1.Pattern = "..." 
    ... 
    RegEXFormatX.Pattern = "..." 


    For Each cell In rCells 
     Dim sValue As String 
     Dim sMonth As String 
     Dim sDay As String 
     Dim sYear As String 
     Dim bCorrectFormat As Boolean 

     bCorrectFormat = True 
     sValue = CStr(cell.Text) 

     If (RegEXFormat1.test(sValue)) Then 
     ... 
     ElseIF(RegEXFormatX.test(sValue) Then 
     ... 
     Else 
      If (sValue = "" Or sValue = "<Input Date>") Then 
      Else 
       MsgBox ("Please Input the date in correct numeric format") 
       cell.value = "<Input Date>" 
     End If 
    Next 

사용자는 그들이 루프에이 매크로를 전송하지 않고 데이터의 행을 삭제할 수있는 기능이 필요하다는 것을 주장한다. 이 발생을 허용하기 위해 내가 가지고있는 것을 어떻게 수정할 수 있습니까?

는 (I 명확하게 코드를 수정하고 내가 느끼는 그나마 여기 필요했다 및 페이지와 페이지 오래되는 것을 내 게시물을 중지하는 많은을 떠났다.)이 대신

+0

+1 "사용자는이 매크로를 루프로 보내지 않고 데이터 행을 삭제할 수 있어야한다고 주장합니다. " – Brad

답변

1

:

If Not Application.Intersect(rTimeCells, Range(Target.Address)) Is Nothing Then 
    Call Time_Format(Range(Target.Address)) 
End If 

을 당신은 아마 당신이 이런 식으로 뭔가를 사용하여 Time_Format에 전달 범위를 제한하려는 :

Dim rngTime as Range 
Set rngTime = Application.Intersect(rTimeCells, Target) 
If Not rngTime Is Nothing Then 
    Call Time_Format(rngTime) 
End If 

참고 : Range(Target.Address)은에 해당

+0

좋아, 교차로를 확인하는 더 좋은 방법입니다. 그러나 문제가 해결되지 않습니다. –

+0

변경 처리 중 이벤트 처리를 일시 중단하고 있기 때문에 루프가 어디서 오는가? 아마도 누락되었습니다. 네 질문에 뭔가있어 ... –