2013-02-19 67 views
12

这是一个我很困惑的查询。因为我已经找了这么多次,但是我总是找到与查找最后使用或第一个非空单元有关的代码。 尝试在下面的代码。从列1开始选择第一个空单元格。(不使用偏移量)

Sub LastCellInColumn() 

Range("A65536").End(xlup).Select 

End Sub 

即使

:DIFF代码已经由单词 “甚至”

iRow = Worksheets("Sheet1").Cells(Rows.Count,1).End(XlUp).Row 

即使

Sub LastCellBeforeBlankInColumn() 

Range("A1").End(xldown).Select 

End Sub 

即使

查找列中的最后使用的细胞分离

查找最后一个单元格,前一个空白的行:

Sub LastCellBeforeBlankInRow() 

Range("A1").End(xlToRight).Select 

End Sub 

即使

查找一排的最后使用的细胞:

Sub LastCellInRow() 

Range("IV1").End(xlToLeft).Select 

End Sub 

即使

Worksheets("Sheet1").Range("A1").End(xlDown).Row + 1 

即使

LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1 
Sheets("SheetName").Range("A" & LastRow).Paste 

即使

Dim FirstBlankCell as Range 
Set FirstBlankCell=Range("A" & rows.Count).end(xlup).offset(1,0) 
FirstBlankCell.Activate 

'Find the last used row in a Column: column A in this example 
Dim LastRow As Long 
Dim NextRow As Long 
With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row 
End With 
NextRow = LastRow + 1 
+0

你见过吗? http://stackoverflow.com/questions/11169445/error-finding-last-used-cell-in-vba – 2013-02-19 12:53:31

+0

是不是同样的事情。我想选择或激活第一个空单元格。 例如:如果有值直到单元格F5,那么我想激活单元格F6并且不使用偏移量,而这样做 – Nishant 2013-02-19 13:00:07

+0

如果您知道最后一行,那么只需使用Range(“F”&LastRow)。激活'虽然我不是太赞成了'.Activate' – 2013-02-19 13:05:58

回答

9

如果你正在试图做的是选择一个给定列第一空白单元格全部,你可以试试这个:

代码:

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 

选择之前 - 第一个空白单元格选择:

enter image description here

选择后:

enter image description here

+1

嘿谢谢萨姆这就是我正在尝试做的。尽管我看了悉达思的建议,但也发挥了作用,并且已经使用了它。但是你的答案在未来对我有用。非常感谢 – Nishant 2013-02-20 07:58:02

+1

有没有办法做到这一点作为一个公式,而不是VBA? – Raj 2016-07-06 19:54:56

7

山姆的代码是好的,但我认为这需要一些修正,

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 

感谢

+2

而且你可能想使用Long而不是Integer,因为行可以走得很远(或向上?) – 2014-07-05 23:23:22

10

如果任何一个绊倒小号在此,我只是...

查找一列中第一个空白单元格(我使用的列d,但不想包括D1)

NextFree = Range("D2:D" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row 
Range("D" & NextFree).Select 

NextFree只是一个名字,如果你愿意,你可以使用香肠。

+0

前两个解决方案没有工作,这一个做到了 – Kyoujin 2017-12-01 14:32:48

0
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 

如果任何列包含多个空单元格持续那么这个代码将不能正常工作

4

如果你正在试图做的是选择一个给定列的第一个空白单元格的所有,你可以给这是一个尝试:

Range("A1").End(xlDown).Offset(1, 0).Select 
+0

迄今为止最简单的答案。 – Aldentec 2017-10-02 22:39:59

1

我适于一点大家的代码,在功能使得它,使它更快(阵列)中,加入参数:

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 
3

如果你正在寻找一个衬垫(不包括名称和注释)试试这个

Dim iRow As Long 
Dim ws As Worksheet 
Set ws = Worksheets("Name") 

    'find first empty cell in column F (coming up from the bottom) and return row number 
iRow = ws.Range("F:F").Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1 
相关问题