1

I was searching in many pages but couldn't find a way to find the first row in the range after the last used row and put the TextBox values from A"n" to Z"n" was possible, the only thing that I could find was to check only a single column. Hope someone would help.

 Private Sub CommandButton1_Click()
  
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet")
    Dim newRow As Long
    newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
   'newRow = Application.WorksheetFunction.CountA(ws.Range("A:Z")) + 1 this was an experiment    

    ws.Cells(newRow, 2).Value = TVehicle.Value
    ws.Cells(newRow, 3).Value = TPlace.Value
    ws.Cells(newRow, 4).Value = TRange.Value
    ws.Cells(newRow, 5).Value = TOD.Value
    ws.Cells(newRow, 6).Value = TIC.Value
    ws.Cells(newRow, 7).Value = TEC.Value
    ecc...   

End Sub
Black cat
  • 1,056
  • 1
  • 2
  • 11
MrAlex01
  • 17
  • 5
  • 2
    I'm not 100% clear on what you're asking, but you might find that this [SO question](https://stackoverflow.com/questions/3628057/how-to-find-out-if-an-entire-row-is-blank-in-excel-thorough-vba) helps? – JohnM Aug 01 '23 at 10:32
  • It looks like you're actually after the *last used* row, plus 1. Is that right? i.e. the next available empty row? – CLR Aug 01 '23 at 10:48
  • @CLR yes I'm trying to do that – MrAlex01 Aug 01 '23 at 12:05

4 Answers4

1

To get the first empty row (i.e. without contents), but examining only columns A-Z, you can use the following:

FirstEmptyRow = ActiveSheet.Range("A:Z").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1

This is on the assumption that columns A:Z have some data in them, it will error if the entire A:Z range is blank - so you should consider error trapping that.

CLR
  • 11,284
  • 1
  • 11
  • 29
0

Replace assignments with the actual ones.(Worksheet name and cell values)

Sub findemptyrow()
Set ws = Worksheets("Sheet6")
i = 1
Do Until WorksheetFunction.CountA(ws.Range("A" & i & ":Z" & i)) = 0
i = i + 1
Loop
Debug.Print "Empty line number is:", i

ws.Cells(i, "A") = "first value"
ws.Cells(i, "Z") = "last value"

End Sub
Black cat
  • 1,056
  • 1
  • 2
  • 11
0

Glue everything together.
If the length is zero, there is nothing. In formula form:

=LEN(CONCAT(A1:Z1))

This is just in formula-form, but I'm convinced it should not be too hard finding the corresponding VBA functions.

Dominique
  • 16,450
  • 15
  • 56
  • 112
0

Get First Available Row

Main

Private Sub CommandButton1_Click()
    
    Const DST_SHEET As String = "Sheet1"
    Const DST_FIRST_ROW As String = "A2:Z2"

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Sheets(DST_SHEET)
    Dim NewRow As Long: NewRow = GetFirstRow(ws, DST_FIRST_ROW)
    
    ws.Cells(NewRow, 2).Value = TVehicle.Value
    ws.Cells(NewRow, 3).Value = TPlace.Value
    ws.Cells(NewRow, 4).Value = TRange.Value
    ws.Cells(NewRow, 5).Value = TOD.Value
    ws.Cells(NewRow, 6).Value = TIC.Value
    ws.Cells(NewRow, 7).Value = TEC.Value

End Sub

Help

Function GetFirstRow( _
    ByVal ws As Worksheet, _
    ByVal FirstRowAddress As String) _
As Long
    
    Dim FirstRow As Long
    
    With ws.Range(FirstRowAddress)
        FirstRow = .Row
        Dim lCell As Range:
        Set lCell = .Resize(.Worksheet.Rows.Count - FirstRow + 1) _
            .Find("*", , xlFormulas, , xlByRows, xlPrevious)
        If Not lCell Is Nothing Then FirstRow = lCell.Row + 1
    End With

    GetFirstRow = FirstRow
    
End Function
VBasic2008
  • 44,888
  • 5
  • 17
  • 28