根据前四列确定空行

我在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 

尝试这个:

 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 

testing:

在这里输入图像说明

从下往上看

 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 
 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 

testing每行的这些列的值,直到find空白的列。

你也许可以用一个发现做,但我不知道如何离开我的头顶。

 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 

尝试这个:

 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中的最后一行是否相同,如果不相同,emptyRow会被设置在A,B或C列的“最大”最后一行上。我希望,这是你在哪里寻找。 否则忽略这个职位。