programing

행 1부터 시작하는 열 F의 첫 번째 빈 셀을 선택합니다(오프셋 사용 안 함).

jooyons 2023. 4. 15. 08:45
반응형

행 1부터 시작하는 열 F의 첫 번째 빈 셀을 선택합니다(오프셋 사용 안 함).

이것은 내가 정말 헷갈리는 질문 중 하나이다.왜냐하면 저는 이것을 몇 번이나 찾았지만, 저는 항상 마지막으로 사용한 셀이나 비어 있지 않은 셀을 찾는 것과 관련된 코드를 발견합니다.아래 코드에서 시도되었습니다. diff 코드가 "짝수"라는 단어로 구분되었습니다.

iRow = Worksheets("Sheet1").Cells(Rows.Count,1).End(XlUp).Row 

심지어.

Sub LastCellBeforeBlankInColumn()

Range("A1").End(xldown).Select

End Sub

심지어.

열에서 마지막으로 사용한 셀 찾기:

Sub LastCellInColumn()

Range("A65536").End(xlup).Select

End Sub

심지어.

행의 공백 앞에 있는 마지막 셀 찾기:

Sub LastCellBeforeBlankInRow()

Range("A1").End(xlToRight).Select

End Sub

심지어.

행에서 마지막으로 사용한 셀 찾기:

Sub LastCellInRow()

Range("IV1").End(xlToLeft).Select

End Sub

심지어.

Worksheets("Sheet1").Range("A1").End(xlDown).Row + 1

심지어.

LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("SheetName").Range("A" & LastRow).Paste

심지어.

Dim FirstBlankCell as Range
Set FirstBlankCell=Range("A" & rows.Count).end(xlup).offset(1,0)
FirstBlankCell.Activate

'Find the last used row in a Column: column A in this example
Dim LastRow As Long
Dim NextRow As Long
With ActiveSheet
    LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
End With
NextRow = LastRow + 1

지정된 열의 첫 번째 셀만 선택하려는 경우 다음을 시도할 수 있습니다.

코드:

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 = 1 To rowCount
        currentRowValue = Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Cells(currentRow, sourceCol).Select
        End If
    Next
End Sub

선택 전 - 선택할 첫 번째 빈 셀:

여기에 이미지 설명 입력

선택 후:

여기에 이미지 설명 입력

혹시나 누군가 이걸 우연히 발견하게 될까봐...

열에서 첫 번째 빈 셀 찾기(D 열을 사용하지만 D1을 포함하지 않음)

NextFree = Range("D2:D" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
Range("D" & NextFree).Select

Next Free는 그냥 이름일 뿐이고, 원한다면 소시지를 사용할 수 있습니다.

지정된 열의 첫 번째 빈 셀만 선택하려는 경우 다음을 시도할 수 있습니다.

Range("A1").End(xlDown).Offset(1, 0).Select

선택한 열을 기준으로 사용할 경우 다음 작업이 수행됩니다.

Selection.End(xlDown).Offset(1, 0).Select

샘의 코드도 좋지만 수정이 필요한 것 같아요.

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 = 1 To rowCount
        currentRowValue = Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Cells(currentRow, sourceCol).Select
            Exit For 'This is missing...
        End If
    Next
End Sub

감사해요.

1개의 라이너(지정 및 코멘트 제외)를 찾고 있는 경우는, 이것을 사용해 주세요.

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Name")

    'find first empty cell in column F (coming up from the bottom) and return row number
iRow = ws.Range("F:F").Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

모든 사람의 코드를 약간 수정하여 함수로 만들고 더 빠르게(배열) 파라미터를 추가했습니다.

Public Function FirstBlankCell(Optional Sh As Worksheet, Optional SourceCol As Long = 1, Optional ByVal StartRow& = 1, Optional ByVal SelectCell As Boolean = False) As Long
Dim RowCount As Long, CurrentRow As Long
Dim CurrentRowValue As String
Dim Data()
If Sh Is Nothing Then Set Sh = ActiveSheet

With Sh

    rowCount = .Cells(.Rows.Count, SourceCol).End(xlUp).Row
    Data = .Range(.Cells(1, SourceCol), .Cells(rowCount, SourceCol)).Value2

    For currentRow = StartRow To RowCount
        If Data(currentRow, SourceCol) = vbNullString Then
            If SelectCell Then .Cells(currentRow, SourceCol).Select
            'if selection is out of screen, intead of .select , use : application.goto reference:=.cells(...), scroll:= true
            FirstBlankCell = currentRow
            Exit For
        End If
    Next

End With ' Sh

Erase Data
Set Sh = Nothing
End Function

이것은 매우 빠르고 깨끗한 방법입니다.또한 위의 답변 중 아무것도 빈 열에 대해 작동하지 않는 빈 열을 지원합니다.

: 사법 usage :SelectFirstBlankCell("F")

Public Sub SelectFirstBlankCell(col As String)

    Dim i As Integer

    For i = 1 To 10000
        If Range(col & CStr(i)).Value = "" Then
            Exit For
        End If
    Next i

    Range(col & CStr(i)).Select
End Sub
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 = 1 To rowCount
        currentRowValue = Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Cells(currentRow, sourceCol).Select
        End If
    Next
End Sub

열에 여러 개의 빈 셀이 연속적으로 포함되어 있는 경우 이 코드는 제대로 작동하지 않습니다.

비슷한 작업을 수행하려다 이 실을 발견했습니다.결국, 나는

Range("F:F").SpecialCells(xlBlanks).Areas(1)(1).Select

지정된 범위와 워크시트의 사용된 범위의 교차점에 공백 셀이 있는 한 올바르게 작동합니다.

영역 속성은 범위에서 절대 첫 번째 공백을 찾는 데 필요하지 않지만 연속되지 않은 공백을 찾는 데 유용합니다.

엔...Do Until더 깨끗하고 - 루루더 - - - - - - - - - - - - - - - - - - - - - - - - - - - 。

Public Sub SelectFirstBlankCell(col As String)
    Dim Column_Index as Integer
    Dim Row_Counter as 

    Column_Index = Range(col & 1).Column
    Row_Counter = 1

    Do Until IsEmpty(Cells(Row_Counter, 1))
        Row_Counter = Row_Counter + 1
    Loop

    Cells(Row_Counter, Column_Index).Select

빈 셀 앞에 있는 모든 빈 셀을 무시하고 첫 번째 열의 끝에서 마지막 빈 셀을 선택하는 다른 방법도 있습니다.컬럼은 그 번호와 함께 주소를 지정해야 합니다(예:Col "A" = 1.

With ThisWorkbook.Sheets("sheet1")

        .Cells(.Cells(.Cells.Rows.Count, 1).End(xlUp).Row + 1, 1).select

End With

다음 코드는 위와 동일하지만 더 잘 이해할 수 있습니다.

i = ThisWorkbook.Sheets("sheet1").Cells.Rows.Count

j = ThisWorkbook.Sheets("sheet1").Cells(i, 1).End(xlUp).Row

ThisWorkbook.Sheets("sheet1").Cells(j + 1, 1) = textbox1.value

NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1

선택한 셀을 기준으로 열에 있는 첫 번째 빈 셀을 선택하기 위해 이 한 줄만 썼습니다.선택한 셀의 첫 번째 열에서만 작동합니다.필요에 따라 변경

Selection.End(xlDown).Range("A2").Select

.Find에는 많은 옵션이 있으며 범위 내에 빈 셀이 없는 경우 아무것도 반환되지 않습니다.

With Range("F:F")
     .Find("", .Rows(.Rows.Count), xlFormulas, , xlByRows, xlNext).Select
End With

.Find는 기본적으로 범위 내의 첫 번째 셀 뒤에 검색을 시작하므로 보통 행 2에서 시작됩니다.After 인수가 범위의 마지막 셀인 경우 행 1에서 시작하는 첫 번째 빈 셀이 검색됩니다.그건 검색이 끝나기 때문이에요.

이 수식은 빌트인을 사용합니다.ISBLANK(range)열의 첫 번째 빈 행 번호와 일치시키는 함수:=MATCH(TRUE,ISBLANK(F:F),0)

물론 평소처럼 범위를 좁힐 수 있습니다.

이 시점까지 공백이 아닌 엔트리의 수를 카운트하려면 , 1을 뺍니다.=MATCH(TRUE,ISBLANK(F:F),0)-1CSV 또는 유사한 Import로 수신되는 트랜잭션 수 등 동적으로 변화하는 테이블 내의 엔트리 수를 셀 때 유용합니다.헤더 행의 수를 빼거나 범위를 다음과 같이 변경하여 헤더 행을 제외할 수 있습니다.(F1:Fxxxx)어디에xxxx예상되는 최대 범위보다 큰 수치입니다.

언급URL : https://stackoverflow.com/questions/14957994/select-first-empty-cell-in-column-f-starting-from-row-1-without-using-offset

반응형