VBA XLUP

Excel VBA XLUP

Una cosa che devi tenere a mente mentre scrivi codice VBA è ciò che fai con il normale foglio di lavoro e puoi replicare la stessa cosa anche in VBA. Una di queste parole chiave nella codifica VBA è "XLUP", in questo articolo ti mostreremo qual è questa parola chiave nella codifica VBA e come usarla nella codifica.

Come utilizzare VBA XLUP nella codifica?

Di seguito sono riportati gli esempi di Excel VBA XLUP.

Puoi scaricare questo modello Excel VBA XLUP qui - Modello Excel VBA XLUP

Esempio n. 1: spostare le celle nella posizione delle celle eliminate

Ad esempio, guarda lo scenario dei dati sottostanti, in cui è necessario eliminare i dati delle celle colorate e più in alto i dati delle righe sottostanti ai dati sopra.

Un modo per eliminarlo nel foglio di lavoro è selezionare quelle celle in cui possiamo semplicemente eliminare l'intera riga stessa. Ma qui le situazioni sono un po 'complicate perché ho celle colorate nella Tabella 1 quando eliminiamo l'intera riga anche le righe della Tabella 2 vengono eliminate, ma non vogliamo che ciò accada, dobbiamo solo eliminare le righe colorate e le celle sotto dovrebbero spostarsi la posizione delle celle eliminate.

Per prima cosa, seleziona le celle colorate e premi Ctrl + Simbolo meno (-) per aprire l'opzione "Elimina".

Tasto di scelta rapida per aprire l'opzione "Elimina"

Nella finestra delle opzioni "cancella", abbiamo quattro opzioni, possiamo scegliere l'azione secondo le nostre esigenze. Dal momento che abbiamo bisogno di spostare le nostre celle verso l'alto per la posizione delle celle eliminate, scegli "Sposta cella su".

Avremo le righe della Tabella 2 invariate.

Questa azione in VBA richiede l'uso della proprietà "XLUP" per eseguire un insieme simile di azioni in VBA. Ora vieni alla finestra dell'editor VBA e inizia il nome della macro.

Codice:

 Sub XLUP_Example () End Sub 

Fornire prima il RANGE di celle da includere in questa operazione. In questa azione, le prime celle da eliminare e spostare verso l'alto sono le celle "A5: B5".

Codice:

 Sub XLUP_Example () Range ("A5: B5") End Sub 

Per questo intervallo di celle selezionare il metodo "Elimina".

Codice:

 Sub XLUP_Example () Intervallo ("A5: B5"). Elimina End Sub 

Come puoi vedere per il metodo "Elimina", abbiamo un argomento opzionale come [Shift], per questo argomento dobbiamo inserire l'argomento come "XLUP".

Codice:

 Sub XLUP_Example () Intervallo ("A5: B5"). Elimina spostamento: = xlUp End Sub 

Ora puoi eseguire questo codice manualmente o tramite il tasto di scelta rapida Excel F5, per vedere il risultato.

Come puoi vedere nella tabella 1, abbiamo la riga numero 6 spostata fino alla quinta riga e d'altra parte la riga tabella 2 (colorata) è inalterata, quindi utilizzando l'opzione "VBA XLUP" possiamo fare questa operazione.

Esempio # 2: trova l'ultima riga utilizzata utilizzando XLUP

Immagina una situazione in cui ti trovi nella cella A20 (guarda l'immagine sotto) e la tua ultima cella utilizzata è A14.

Ora se vuoi scegliere l'ultima cella utilizzata (A14). come farai usando un tasto di scelta rapida ???

Useremmo Ctrl + Freccia su per spostarci all'ultima cella utilizzata dalla posizione corrente.

Tasto di scelta rapida per passare all'ultima cella utilizzata 

Quindi, dalla cella corrente, Ctrl + freccia su ha selezionato l'ultima cella utilizzata. Allo stesso modo, nella codifica VBA usiamo END (XLUP) per eseguire lo stesso.

Ora torna alla finestra di codifica VBA.

In questa finestra, eseguiremo il compito di trovare l'ultima riga utilizzata nel foglio di lavoro. Crea una nuova sottoprocedura nella finestra VBA.

Codice:

 Sub XLUP_Example1 () End Sub 

Per memorizzare l'ultimo numero di riga utilizzato. definire la variabile come il tipo di dati VBA LONG.

Codice:

 Sub XLUP_Example1 () Dim Last_Row_Number As Long End Sub 

Ora per questa variabile, assegneremo l'ultimo numero di riga utilizzato.

Codice:

 Sub XLUP_Example1 () Dim Last_Row_Number As Long Last_Row_Number = End Sub 

Ora usa l'oggetto RANGE e apri questo oggetto.

Codice:

 Sub XLUP_Example1 () Dim Last_Row_Number As Long Last_Row_Number = Range (End Sub 

Ora menziona la cella attiva (A20) per l' oggetto RANGE .

Codice:

 Sub XLUP_Example1 () Dim Last_Row_Number As Long Range ("A14"). Seleziona Last_Row_Number = Range ("A20") End Sub 

Ora apri la proprietà END per la cella dell'intervallo fornito.

Codice:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End( End Sub 

As you can see above, we have to arrow key options like “xlDown”, “xlToLeft”, “xlToRight”, “xlUp”. Since we are moving up from the A14 cell choose the “VBA XLUP” option.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End(xlUp) End Sub 

After moving up from A14 cell we need to mention what we need to do since we need the last used row number I will use ROW property.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End(xlUp).Row End Sub 

Now for the message box assign the value of variable “Last_Row_Number”.

Code:

 Sub XLUP_Example1()  Dim Last_Row_Number As Long Range("A14").Select Last_Row_Number = Range("A20").End(xlUp).Row MsgBox Last_Row_Number End Sub 

Now you can run this code manually or through shortcut key F5, to see the result.

So message box showing the last used row number as 14, so our last data used row number is A14 cell.

In this case, since data is very small we started from A20 cell but when the data is large we cannot say which cell to take into consideration first, in such cases we need to employ a different technique.

We need to use CELLS property, below is the example of the same.

Code:

 Sub XLUP_Example2() Dim Last_Row_Number As Long Last_Row_Number = Cells(Rows.Count, 1).End(xlUp).Row MsgBox Last_Row_Number End Sub 

Now you can run this code manually or through shortcut key F5, to see the result.

Instead of a RANGE object, I have used CELLS property. Let me explain this in detail to you.

ROW.COUNT this will count how many rows are there in column 1. What this will do is it will take into consideration of the last cell in the worksheet instead of random cell address, in the above case we have used A14 as the random cell address.

Things to Remember about VBA XLUP

  • XLUP is the word used in VBA code to replicate the action of the “Up Arrow” key in excel.
  • VBA XLUP is used to move from active cells to the above cell or last used cell.
  • XLUP is generally used along with END property in VBA.