根据前四列确定空行

时间:2015-11-10 13:24:16

标签: excel vba

我在Excel中使用UserForm将内容从文本框1移动到工作表2上的第一个空行。下面的命令工作正常,但我想只考虑前三列是空的,而不是所有列的空行(其他列有一些信息)。

我该如何调整?

Private Sub CommandButton1_Click()
Dim emptyRow As Long

'Make Sheet2 active
With Sheets("Sheet2")

    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1

    'Transfer information

    .Cells(emptyRow, 1).Value = TextBox1.Value

5 个答案:

答案 0 :(得分:2)

从下往上看。

emptyRow = application.max(.cells(rows.count, "A").end(xlup).row, _ 
                           .cells(rows.count, "B").end(xlup).row, _
                           .cells(rows.count, "C").end(xlup).row) + 1

答案 1 :(得分:1)

试试这个:

Private Sub CommandButton1_Click()
    Dim x&, i&, emptyRow&
    emptyRow = 0
    With Sheets("Sheet2")
        For x = 1 To 3
            i = .Cells(Rows.Count, x).End(xlUp).Row
            If emptyRow < i + 1 Then emptyRow = i + 1
        Next x
        .Cells(emptyRow, 1).Value = TextBox1.Value
    End With
End Sub

<强> 试验:

enter image description here

答案 2 :(得分:0)

Private Sub CommandButton1_Click()
Dim emptyRow As Long, x as Long

'Make Sheet2 active
With Sheets("Sheet2")

    'Determine emptyRow
    x = 0

    Do 

         x = x +1

         emptyRow = WorksheetFunction.CountA(.Range("A" & x & ":C" & x))

    Loop Until emptyRow = 0

    'Transfer information
    .Cells(x, 1).Value = TextBox1.Value

答案 3 :(得分:0)

测试每行的列值,直到找到空白列。

你也可以用一个发现来做,但我不知道我的头脑是多么偏高。

Private Sub CommandButton1_Click()
    Dim ws As Excel.Worksheet
    Set ws = ActiveWorkbook.Sheets("Sheet2")
    Dim emptyRow As Long

    Dim lrow As Long
    lrow = 1

    ws.Activate
    'Loop through the rows
    Do While lrow <= ws.UsedRange.Rows.count

        'Test for an empty row
        If ws.Range("A" & lrow).Value = "" And ws.Range("B" & lrow).Value = "" And ws.Range("C" & lrow).Value = "" Then
            emptyRow = lrow
            Exit Do
        End If

    lrow = lrow + 1
    Loop

    ws.Range("A" & emptyRow).Value = TextBox1.Value

End Sub

答案 4 :(得分:0)

试试这个:

Private Sub CommandButton1_Click()
Dim emptyRow As Long
Dim row1  As Long, row2 As Long, row3 As Long

'Make Sheet2 active
With Sheets("Sheet2")

'Determine emptyRow
row1 = .Cells(.Rows.Count,1).End(XlUp).Row + 1
row2 = .Cells(.Rows.Count,2).End(XlUp).Row + 1
row3 = .Cells(.Rows.Count,3).End(XlUp).Row + 1

If row1 = row2 And row1 = row3 Then

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

Else

    If row1 >= row2 And row1 >= row3 Then

    emptyRow = row1

    Elseife row2 >= row3 Then

    emptyRow = row2

    Else

    emptyRow = row3

    End If

End If

'Transfer information

.Cells(emptyRow, 1).Value = TextBox1.Value

那么这样做是否会检查A,B和C列中的最后一行是否相同,如果没有,则在A,B或C列的“最大”最后一行设置emptyRow 。 我希望,这就是你在寻找的地方。否则请忽略这篇文章。