Vba 从第1行开始,选择F列中的第一个空单元格。(不使用偏移)
这是一个我很困惑的问题。因为我已经找了很多次了,但我总是找到与查找最后使用的单元格或第一个非空单元格相关的代码。 在以下代码中尝试。差异代码之间用“偶数”一词隔开 甚至 甚至 在列中查找最后使用的单元格:Vba 从第1行开始,选择F列中的第一个空单元格。(不使用偏移),vba,excel,Vba,Excel,这是一个我很困惑的问题。因为我已经找了很多次了,但我总是找到与查找最后使用的单元格或第一个非空单元格相关的代码。 在以下代码中尝试。差异代码之间用“偶数”一词隔开 甚至 甚至 在列中查找最后使用的单元格: Sub LastCellInColumn() Range("A65536").End(xlup).Select End Sub 甚至 查找行中空白前的最后一个单元格: Sub LastCellBeforeBlankInRow() Range("A1").End(xlToRight).S
Sub LastCellInColumn()
Range("A65536").End(xlup).Select
End Sub
甚至
查找行中空白前的最后一个单元格:
Sub LastCellBeforeBlankInRow()
Range("A1").End(xlToRight).Select
End Sub
Sub LastCellInRow()
Range("IV1").End(xlToLeft).Select
End Sub
甚至
查找一行中最后使用的单元格:
Sub LastCellBeforeBlankInRow()
Range("A1").End(xlToRight).Select
End Sub
Sub LastCellInRow()
Range("IV1").End(xlToLeft).Select
End Sub
甚至
甚至
甚至
如果您只想选择给定列中的第一个空白单元格,您可以尝试一下:
Range("A1").End(xlDown).Offset(1, 0).Select
代码:
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 6 'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
End If
Next
End Sub
选择前-要选择的第一个空白单元格:
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 6 'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
End If
Next
End Sub
选择后:
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 6 'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
End If
Next
End Sub
Sam的代码很好,但我认为它需要一些更正
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 6 'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
Exit For 'This is missing...
End If
Next
End Sub
谢谢你,以防有人像我这样偶然发现 查找列中的第一个空白单元格(我使用的是D列,但不希望包含D1) NextFree只是一个名字,如果你想的话,你可以用香肠
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 6 'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
End If
Next
End Sub
如果任何列连续包含多个空单元格,则此代码将无法正常工作如果您只是选择给定列中的第一个空单元格,则可以尝试:
Range("A1").End(xlDown).Offset(1, 0).Select
如果相对于选定的列使用它,则此操作有效:
Selection.End(xlDown).Offset(1, 0).Select
我对每个人的代码都做了一些修改,将其放入函数中,使其更快(数组),并添加了参数:
Public Function FirstBlankCell(Optional Sh As Worksheet, Optional SourceCol As Long = 1, Optional ByVal StartRow& = 1, Optional ByVal SelectCell As Boolean = False) As Long
Dim RowCount As Long, CurrentRow As Long
Dim CurrentRowValue As String
Dim Data()
If Sh Is Nothing Then Set Sh = ActiveSheet
With Sh
rowCount = .Cells(.Rows.Count, SourceCol).End(xlUp).Row
Data = .Range(.Cells(1, SourceCol), .Cells(rowCount, SourceCol)).Value2
For currentRow = StartRow To RowCount
If Data(currentRow, SourceCol) = vbNullString Then
If SelectCell Then .Cells(currentRow, SourceCol).Select
'if selection is out of screen, intead of .select , use : application.goto reference:=.cells(...), scroll:= true
FirstBlankCell = currentRow
Exit For
End If
Next
End With ' Sh
Erase Data
Set Sh = Nothing
End Function
如果您正在寻找单行程序(不包括名称和注释),请尝试以下方法
这是一种非常快速和干净的方法。它还支持空列,因为上面的答案都不适用于空列 用法:
SelectFirstBlankCell(“F”)
我认为
Do-Until
-循环在这里更干净、更短、更合适:
Public Sub SelectFirstBlankCell(col As String)
Dim Column_Index as Integer
Dim Row_Counter as
Column_Index = Range(col & 1).Column
Row_Counter = 1
Do Until IsEmpty(Cells(Row_Counter, 1))
Row_Counter = Row_Counter + 1
Loop
Cells(Row_Counter, Column_Index).Select
还有一种方法忽略非空单元格之前的所有空单元格,并从第一列末尾选择最后一个空单元格。列应使用其编号进行寻址,例如列“A”=1 下一个代码与上面的代码完全相同,但可以更好地理解
i = ThisWorkbook.Sheets("sheet1").Cells.Rows.Count
j = ThisWorkbook.Sheets("sheet1").Cells(i, 1).End(xlUp).Row
ThisWorkbook.Sheets("sheet1").Cells(j + 1, 1) = textbox1.value
我在尝试执行类似任务时发现了此线程。最后,我用
范围(“F:F”)。特殊单元格(xlBlanks)。区域(1)(1)。选择
只要在工作表的指定范围和使用范围的交点处有一个空白单元格,该选项就可以正常工作
区域属性不需要查找范围中的第一个绝对空白,但对于查找后续的非连续空白非常有用。NextRow=Application.WorksheetFunction.CountA(range(“A:A”)+1
我刚刚编写了一行代码,以根据选定的单元格选择列中找到的第一个空白单元格。仅适用于选定单元格的第一列。根据需要修改
Selection.End(xlDown).Range("A2").Select
你看到这个了吗?这不是一回事吗。我想选择或激活第一个空单元格。示例:如果在单元格F5之前有值,那么我想激活单元格F6,并且在执行SOI时不使用偏移量。如果您知道最后一行,那么只需使用范围(“F”&LastRow)。激活,尽管我不太赞成。激活Siddharth我知道。。。但最后一行是使用过的单元格,有一些值。我想要它下面的单元格LastRow=.Range(“E”&.Rows.Count).End(xlUp).Row+1将给出该行;)嘿,谢谢,山姆,这就是我想做的。虽然我看了悉达思的建议,这也行得通,我已经用过了。但是你的ans将来对我很有用。感谢alotIs,有没有一种方法可以将其作为公式而不是VBA?如果上面只有一个单元格,则无法使其工作。例如,F1有一个值,而F2没有。为什么数字是6?我不明白。。。请解释一下。@SabiriS在我正在使用的F列的截图中<如果从左到右计数,代码>F
是第6列<代码>A=1
,B=2
F=6
。您可能希望使用Long而不是Integer,因为行可以向下(或向上?)很远。到目前为止,这是最简单的答案。如果列为空,则不太好,因为它只会选择最后一个绝对单元格。请停止使用。选择
~!这真是太过分了,对空列不起作用。只是好奇,NextCell
的目的是什么?删除了这个,在本例中不需要。这有什么“非常快速和干净”的呢?它在运行时不会移动活动单元格,这又慢又笨重,运行时的错误单击会在脚本期间切换列,移动活动单元时,也可能导致“无响应”。尝试一下,看看有什么不同。请不要只发布代码作为答案,还要解释代码的作用以及它是如何解决问题的。带有解释的答案通常更有帮助,质量更好,更容易吸引选票。
Public Sub SelectFirstBlankCell(col As String)
Dim Column_Index as Integer
Dim Row_Counter as
Column_Index = Range(col & 1).Column
Row_Counter = 1
Do Until IsEmpty(Cells(Row_Counter, 1))
Row_Counter = Row_Counter + 1
Loop
Cells(Row_Counter, Column_Index).Select
With ThisWorkbook.Sheets("sheet1")
.Cells(.Cells(.Cells.Rows.Count, 1).End(xlUp).Row + 1, 1).select
End With
i = ThisWorkbook.Sheets("sheet1").Cells.Rows.Count
j = ThisWorkbook.Sheets("sheet1").Cells(i, 1).End(xlUp).Row
ThisWorkbook.Sheets("sheet1").Cells(j + 1, 1) = textbox1.value
Selection.End(xlDown).Range("A2").Select