2013-02-19 65 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 
相關問題