열 A의 각 값에서 날짜를 알고 싶습니다.이 경우 VBA에서는 VLookup
함수를 사용하지만 값이 없으면 (= N/A) 코드가 멈 춥니 다.결과가 Nothing 일 때 Vlookup이 작동하지 않습니다.
이 오류는 어떻게 처리해야합니까?
Here's 코드 :
Sub lapse_Function()
Dim wsAll As Worksheet
Dim wsDb As Worksheet
Dim lapse As Variant
Dim startDate As Date
Dim Lookup_Range As Range
Dim ID_number As Object
Set wsDb = Worksheets("Database")
Set wsAll = Worksheets("All data")
'Run function
rowNo = 2
Do Until IsEmpty(wsDb.Cells(rowNo, 2))
'Set values
studyDate = DateValue("March 3, 2017")
Set Lookup_Range = wsAll.Range(wsAll.Cells(1, 1), wsAll.Cells(500, 3))
Set ID_number = wsDb.Cells(rowNo, 2)
lapse = Application.WorksheetFunction.VLookup(ID_number, Lookup_Range, 3, False) 'Here is where the code stops. I have next the IF in case of error but it won't work
'Assing value
If IsError(lapse) Then
Cells(rowNo, 23).Value = "Less than 4 months" 'NOT WORKING
Else
If ((studyDate - lapse)/7) < 52 Then
Cells(rowNo, 23).Value = Round(((studyDate - lapse)/7), 0) & " weeks"
Else
Cells(rowNo, 23).Value = Round(((studyDate - lapse)/360), 1) & " months"
End If
End If
rowNo = rowNo + 1
Loop
End Sub
"코드 중지"란 무엇을 의미합니까? 오류 메시지가 나타나면 그 메시지는 무엇입니까? IsError()는'VLOOKKUP'이 반환하는 에러 * 값을 테스트 할 수 있지만 VBA 런타임 에러를 잡아 내지 않습니다. 그것을 위해 설계되지 않았습니다. –
사용 : Application.VLookup. 대신 : Application.WorksheetFunction.VLookup – user3598756
오류가 발생했을 때 실행을 계속하려면'On Error Resume Next'를 사용해야합니다. 실행 취소하려면 'On Error GoTo 0' 오류를 테스트하려면 If Err.Number <> 0 Then :'Reset Error : Err.Clear'. 또한'Dim lapse As Date : 경과 = CDate (... VLookUp ...)' – PatricK