2014-09-18 2 views
0

중첩 For 루프를 작성했으며 조건이 충족 되더라도 For 루프 코드를 실행하지 않습니다. 가장 바깥 쪽 For 루프를 주석 처리했지만 내부 루프도 작동하지 않습니다. I 2007Excel 2007의 VBA는 중첩 된 for 루프를 실행하지 않습니다.

Sub CalcAll() 

Dim a As Integer 
a = 10 
Dim b As Integer 
b = 10 

For a = 10 To a = (Range("B" & Rows.Count).End(xlUp).Row) Step 1 

    For b = 10 To b = (Worksheets("DistanceLookupTable").Cells(2, Sheet1.Columns.Count).End(xlToLeft).Column) Step 1 

     If IsEmpty(Cells(a, i).Value) Then 
      Exit Sub 
     Else 
      'Lots of code reading values from the worksheet and printing 
      'calculated values to the worksheet 
     End If 
    Next b 
Next a 
End Sub 

주셔서 감사합니다 도움을 Excel에서 일하고 있어요

귀하의 루프는 다음과 같이 작성해야
+0

당신은'셀 (a, i)'을 가지고 있지만 어디에도 'i'가 정의되어 있지 않습니다. '셀 (a, b)'이어야합니까? –

답변

2

:

For a = 10 To XXX 

보다는 :

For a = 10 To a = XXX 
+0

이것은 트릭을 했어, 고마워. – Kate

2

이 시도 :

Dim a As Integer 
'a = 10 'Unnecessary to assign value here, as you assign the starting value in the For loop 
Dim b As Integer 
'b = 10 'Again, this line not necessary 

For a = 10 To Range("B" & Rows.Count).End(xlUp).Row Step 1 

    For b = 10 To Worksheets("DistanceLookupTable").Cells(2, Sheet1.Columns.Count).End(xlToLeft).Column Step 1 

     If IsEmpty(Cells(a, i).Value) Then '<- do you mean 'b' instead of 'i'? I don't see 'i' assigned anywhere... 
     Exit Sub 
     Else 
     'Lots of code reading values from the worksheet and printing 
     'calculated values to the worksheet 
     End If 
    Next b 
Next a 

관련없는 메모에서는 첫 번째 for 루프 (Worksheets("worksheetName").Range("B" & Rows.Count)... 대신 Range("B" & Rows.Count)... 대신에)의 범위를 완전히 한정하는 것이 좋습니다. 현재는 현재 활성화 된 시트의 범위를 사용합니다. 그래서 그것이 당신의 의도가 아니라면 명시 적으로하는 것이 낫습니다.

+0

For 루프가 작동합니다. 감사합니다. 그러나 내가 = 10 워크 시트 ("DistanceLookupTable") (범위 ("B"& Rows.Count) .End (xlUp) .Row)에 대해 1 단계 그것은 빨간색으로 강조 표시하고 구문 오류가 있다고합니다 – Kate

+0

거기 범위 부분을 괄호로 묶어서는 안됩니다. for 루프에서 직접 하드 코딩하는 대신 변수에 할당하는 것을 고려할 수 있습니다. 'lastColumn = Worksheets ("DistanceLookupTable"). Range ("B"& Rows.Count) ...'등. 이렇게하면 코드를 단계별로 평가하여 해당 문장이 무엇인지 확인할 수 있습니다. 그리고 for 루프는'For a = 10 to lastColumn'입니다. –