2014-11-27 2 views
0

Harvie (MD5)와 같은 괄호와 텍스트가있는 성을 가진 27,000 개의 레코드가 포함 된 CSV가 있습니다. 텍스트를 삭제하기 위해 단일 셀을 대상으로 할 때 작업을 수행 할 수 있도록 노력했습니다. 브라켓과 브라켓 자체 내부하지만 난 루프 그것을하려고 할 때 내가 얻을 [런타임 오류은 5] -invalid 프로 시저 호출 또는 인수가 .... VBA에서 각 루프의 변수를 찾아서 바꿉니 까?

내 루프

Sub test() 
Application.ScreenUpdating = False 
Dim myCell As Range 
Dim myRange As Range 
Dim cellvalue As String 

Set myRange = Range("D1:D27168") 


For Each myCell In myRange 

cellvalue = myCell.Value 

openingParen = InStr(cellvalue, "(") 
closingParen = InStr(cellvalue, ")") 
enclosedValue = Mid(cellvalue, openingParen + 1, closingParen - openingParen - 1) 

     Cells.Find(What:=enclosedValue, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _ 
     xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _ 
     , SearchFormat:=False).Activate 
    ActiveCell.Replace What:=enclosedValue, Replacement:="", LookAt:=xlPart, _ 
     SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
     ReplaceFormat:=False 

Next myCell 
Application.ScreenUpdating = True 
End Sub 

뭔가가된다 내가 틀렸어, 브래킷을 제거하고 짧게 유지 코드를 교체했습니다.

뭔가 기분 당신은 아마 때문에 openingParenclosingParen 값의 오류가 미스

답변

0

입니다. Mid이 잘못된 정수 입력 (예 : Mid("text", 1, 1 - 3) 또는 Mid("text", -10, 5))을 얻으면 오류 5와 함께 실패합니다.
Mid을 호출하거나 Error Handling을 호출하기 전에 InStr 결과를 테스트하는 것이 좋습니다 (의사 코드, 테스트되지 않음).

Option Explicit 
Option Base 0 
(.....) 
On Error Resume Next 
newRes = Mid("mytext", iFrom, iTo) 
If (Err.Number = 0) Then 
    'do what you planned here 
Else 
    'handle error 
    Call MsgBox(Err.Message & CStr(iFrom) & ", " & CStr(iTo)) 
End If 
(...) 
On Error Goto 0 'turn off error sinking 
관련 문제