2014-01-07 4 views
0

this link에서 첫 번째 빈 셀을 찾기 위해이 멋진 코드를 찾았습니다. 그러나 값이있는 범위 내에서 2 개의 빈 셀이 연속적으로 있으면이 코드는 작동하지 않습니다. 첫 번째를 원하면 두 번째 빈 셀만 선택합니다. 연속 셀은 범위 내에서 처음 2 또는 중간 2 또는 마지막 2 일 수 있습니다. 또한 행 계산식을 사용할 수 없으므로 3,4,5 연속 셀이 될 수 있습니다. 코드를 변경하는 방법을 누군가에게 알려 주시면 감사하겠습니다.VBA가 범위 내의 연속 빈 셀을 찾습니다.

Public Sub SelectFirstBlankCell() 
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer 
Dim currentRowValue As String 

sourceCol = 6 'column F has a value of 6 
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 

'for every row, find the first blank cell and select it 
For currentRow = 3 To rowCount 
    currentRowValue = Cells(currentRow, sourceCol).Value 
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then 
     Cells(currentRow, sourceCol).Select 
    End If 
Next 
End Sub 

또한, 그냥 데이터 사이의 범위 내에있는 다수의 비 연속적인 빈 행이있는 경우, 그것뿐만 아니라 마지막 빈 행을 선택 발견 (안 마지막 행을!)

답변

1

트릭은 빈 셀이 감지되면 Exit For를 추가하여 루프를 중단하는 것입니다.

또한 코드를 좀 더 확장 가능하게 만들려면 sourceCol을 하위에 정의 된 매개 변수로 포함시키는 것이 좋습니다. 이 당신이, 내가 그 그것을 가지고가는 모두 믿을 수없는 컬럼에 대한

Public Sub SelectFirstFromF() 
    Call SelectFirstBlankCell() 
End Sub 

Public Sub SelectFirstFromB() 
    Call SelectFirstBlankCell(2) 
End Sub 

Sub SelectFirstBlankCell(Optional sourceCol as Integer = 6) 
Dim rowCount As Integer, currentRow As Integer 
Dim currentRowValue As String 

rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 

'for every row, find the first blank cell and select it 
For currentRow = 3 To rowCount 
    currentRowValue = Cells(currentRow, sourceCol).Value 
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then 
     Cells(currentRow, sourceCol).Select 
     Exit For 
    End If 
Next 
End Sub 
+0

감사 매크로를 만들 수 있습니다 ..... 또 한가지는, 매개 변수로 sourceCol과 코드를 사용하여 매크로는 할 수 없습니다 내 매크로 목록에 없기 때문에 실행하십시오. 왜 이렇게이다? – user2530250

+0

OK 매크로는 매개 변수를 사용할 수 없으며 매크로 목록에 표시됩니다. 답변을 반영하여 답변을 업데이트하겠습니다. – serakfalcon

관련 문제