2017-11-07 20 views
0

저는 시각적 기본을 상당히 새로 습득했습니다. 열을 반복하고 # N/A를 모두 변경하려고합니다. 다른 모든 # N/A를 번갈아 가며 첫 번째가 유형 1로 변경된 후 두 번째 유형이 다음과 같이 변경됩니다. 나는 곤경에 빠져 누군가가 나를 올바른 방향으로 넣을 수 있는지 궁금해하고 있었다. 여기에 내가 지금까지 가지고 있고 미리 감사드립니다!VBA 루핑 범위 및 변경 N/A 셀

Sub TruckFilterType() 

Dim counter As Integer 
Dim state As Integer 

counter = 2 
state = 1 

For counter = 2 To 100 
    If Worksheets("Sheet1").Cells(counter, 1).Value = "#N/A" Then 
     If state = 1 Then 
      Worksheets("Sheet1").Cells(counter, 1).Value = "TYPE 1" 
      state = 2 

     ElseIf state = 2 Then 
      Worksheets("Sheet1").Cells(counter, 1).Value = "TYPE 2" 
      state = 1 
     End If 
    End If 
Next counter 

End Sub 

답변

0

셀에 #NA이 포함되어 있는지 확인하려면 오류를 트랩해야합니다.

코드

Option Explicit 

Sub TruckFilterType() 

Dim counter As Long 
Dim state As Long 
Dim CellVal As Variant 

counter = 2 
state = 1 

With Worksheets("Sheet1") 
    For counter = 2 To 100 
     ' trap #NA error section 
     On Error Resume Next 
     CellVal = .Cells(counter, 1).Value2 
     On Error GoTo 0 
     If IsError(CellVal) Then 
      If CellVal = CVErr(xlErrNA) Then ' check if current error if xlErrNA (2042) 
       If state = 1 Then 
        .Cells(counter, 1).Value = "TYPE 1" 
        state = 2 
       Else 
        .Cells(counter, 1).Value = "TYPE 2" 
        state = 1 
       End If 
      End If 
     End If 
    Next counter 
End With 

End Sub 
+0

신난다. 나는 모든 것이 "On Error GoTo 0"이라는 줄을 이해한다고 생각합니다. 설명해 주시겠습니까? –

+0

@DustinSmith 여기 전문가의 링크 http://www.cpearson.com/excel/errorhandling.htm –

+0

위대한! 도와 주셔서 감사합니다! –