Finding first blank row, then writing to it

后端 未结 8 1816
小鲜肉
小鲜肉 2020-12-02 00:30

I need to find the first blank row in a workbook and write information to (row, 1) and (row, 2). I think I\'m currently pretty stuck...

Function WriteToMaste         


        
相关标签:
8条回答
  • 2020-12-02 00:57

    I would have done it like this. Short and sweet :)

    Sub test()
    Dim rngToSearch As Range
    Dim FirstBlankCell As Range
    Dim firstEmptyRow As Long
    
    Set rngToSearch = Sheet1.Range("A:A")
        'Check first cell isn't empty
        If IsEmpty(rngToSearch.Cells(1, 1)) Then
            firstEmptyRow = rngToSearch.Cells(1, 1).Row
        Else
            Set FirstBlankCell = rngToSearch.FindNext(After:=rngToSearch.Cells(1, 1))
            If Not FirstBlankCell Is Nothing Then
                firstEmptyRow = FirstBlankCell.Row
            Else
                'no empty cell in range searched
            End If
        End If
    End Sub
    

    Updated to check if first row is empty.

    Edit: Update to include check if entire row is empty

    Option Explicit
    
    Sub test()
    Dim rngToSearch As Range
    Dim firstblankrownumber As Long
    
        Set rngToSearch = Sheet1.Range("A1:C200")
        firstblankrownumber = FirstBlankRow(rngToSearch)
        Debug.Print firstblankrownumber
    
    End Sub
    
    Function FirstBlankRow(ByVal rngToSearch As Range, Optional activeCell As Range) As Long
    Dim FirstBlankCell As Range
    
        If activeCell Is Nothing Then Set activeCell = rngToSearch.Cells(1, 1)
        'Check first cell isn't empty
        If WorksheetFunction.CountA(rngToSearch.Cells(1, 1).EntireRow) = 0 Then
            FirstBlankRow = rngToSearch.Cells(1, 1).Row
        Else
    
            Set FirstBlankCell = rngToSearch.FindNext(After:=activeCell)
            If Not FirstBlankCell Is Nothing Then
    
                If WorksheetFunction.CountA(FirstBlankCell.EntireRow) = 0 Then
                    FirstBlankRow = FirstBlankCell.Row
                Else
                    Set activeCell = FirstBlankCell
                    FirstBlankRow = FirstBlankRow(rngToSearch, activeCell)
    
                End If
            Else
                'no empty cell in range searched
            End If
        End If
    End Function
    
    0 讨论(0)
  • 2020-12-02 01:03
    ActiveSheet.Range("A10000").End(xlup).offset(1,0).Select
    
    0 讨论(0)
提交回复
热议问题