excel VBA检查从A“N”到Z“N”的整行是否为空,并将文本框值

brvekthn  于 2023-08-08  发布在  其他
关注(0)|答案(4)|浏览(120)

我在许多页面中搜索,但找不到一种方法来检查是否整行单元格为空,并将TextBox值从A“n”到Z“n”是可能的,我唯一能找到的就是只检查一列。希望有人能帮忙。

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

字符串

myzjeezk

myzjeezk1#

把所有东西粘在一起。
如果长度为零,则没有任何内容。以公式形式:

=LEN(CONCAT(A1:Z1))

字符串
这只是公式形式,但我相信找到相应的VBA函数应该不会太难。

slmsl1lt

slmsl1lt2#

获取第一个可用行

主要

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

字符串

帮助

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

sr4lhrrt

sr4lhrrt3#

为了得到第一个空行(即不带内容),但只检查A-Z列,则可以使用以下命令:

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

字符串
这是假设列A:Z中有 * 一些 * 数据,如果整个A:Z范围是空的,它将出错-所以你应该考虑错误捕获。

x6yk4ghg

x6yk4ghg4#

用实际分配替换分配。(工作表名称和单元格值)

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

字符串

相关问题