2013-02-19 16 views
12

Questa è una query con la quale sono davvero confuso. Ho cercato Coz così tante volte ma trovo sempre i codici relativi alla ricerca dell'ultima cella utilizzata o prima non vuota. Provati ai seguenti codici. codici diff sono stati separati dalla parola "anche"Selezionare la prima cella vuota nella colonna F a partire dalla riga 1. (senza utilizzare l'offset)

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

anche

Sub LastCellBeforeBlankInColumn() 

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

End Sub 

anche

Trova l'ultima cella utilizzata in una colonna:

Sub LastCellInColumn() 

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

End Sub 

anche

Trova l'ultima cella, prima di un vuoto in una riga:

Sub LastCellBeforeBlankInRow() 

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

End Sub 

anche

Trova l'ultima cella utilizzata in una riga:

Sub LastCellInRow() 

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

End Sub 

anche

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

anche

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

anche

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

Hai visto questo? http://stackoverflow.com/questions/11169445/error-finding-last-used-cell-in-vba –

+0

Non è la stessa cosa. Voglio selezionare o attivare la prima cella vuota. esempio: Se ci sono valori fino alla cella F5, allora voglio attivare la cella F6 e non usare Offset mentre lo faccio – Nishant

+0

Se conosci l'ultima riga, usa semplicemente 'Range (" F "e LastRow). Attiva' Anche se non lo sono troppo a favore di ". Attiva" –

risposta

9

Se tutto quello che stai cercando di fare è selezionare la prima cella vuota in una data colonna, si può dare a questo una prova:

Codice:

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 

Prima della selezione: prima cella vuota per selezionare:

enter image description here

dopo la selezione:

enter image description here

+1

Hey Grazie Sam Questo è quello che stavo cercando di fare. Anche se ho guardato il suggerimento di Siddharth e anche quello ha funzionato e l'ho già usato. Ma i tuoi ans sarebbero utili in futuro per me. grazie mille – Nishant

+1

C'è un modo per farlo come una formula piuttosto che in VBA? – Raj

7

Codice di Sam è buona, ma credo che sia bisogno di qualche correzione,

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 

Grazie

+2

e potresti voler usare Long invece di Integer, perché le righe possono andare molto in basso (o in alto?) –

10

Nel caso in cui qualsiasi uno inciampa s su questo come ho solo ...

Trova prima cella vuota di una colonna (sto usando la colonna D, ma non volevo includere D1)

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

NextFree è solo un nome, potresti usare le salsicce se lo volevi.

+0

le prime due soluzioni non funzionavano, questo ha fatto – Kyoujin

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 

Se una colonna contiene più di una cella vuota continuamente allora questo codice non funzionerà correttamente

4

Se tutto quello che stai cercando di fare è selezionare la prima cella vuota in una data colonna, si può dare questo una prova:

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

di gran lunga la risposta più semplice. – Aldentec

1

ho adattato un po 'il codice di tutti, fatto in una funzione, ha reso più veloce (array), e parametri aggiunti:

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

Se siete alla ricerca di un uno di linea (non comprese le denominazioni e commenti) provare questo

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