VBA XLUP | Kako koristiti VBA XLUP u programu Excel? (s primjerima)

Excel VBA XLUP

Prilikom pisanja VBA koda morate imati na umu ono što radite s uobičajenim radnim listom, a istu stvar možete replicirati i u VBA. Jedna od takvih ključnih riječi u VBA kodiranju je "XLUP", u ovom ćemo vam članku pokazati što je ta ključna riječ u VBA kodiranju i kako je koristiti u kodiranju.

Kako koristiti VBA XLUP u kodiranju?

Slijede primjeri excela VBA XLUP.

Ovaj VBA XLUP Excel predložak možete preuzeti ovdje - VBA XLUP Excel Predložak

Primjer # 1 - Premještanje ćelija u izbrisani položaj ćelija

Na primjer, pogledajte scenarij podataka u nastavku, gdje trebate izbrisati podatke o obojenim stanicama i više podataka iz redaka u nastavku do gornjih podataka.

Jedan od načina brisanja ovog na radnom listu je odabir onih ćelija u kojima možemo jednostavno izbrisati cijeli redak. Ali ovdje su situacije malo nezgodne jer u tablici 1 imam obojene stanice kad brišemo cijeli redak, čak se i retci tablice 2 također brišu, ali ne želimo da se to dogodi, trebamo samo izbrisati obojene retke, a stanice ispod trebale bi se pomicati gore položaj izbrisanih stanica.

Prvo odaberite obojene stanice i pritisnite Ctrl + Simbol minus (-) da biste otvorili opciju "Delete".

Tipka prečaca za otvaranje opcije "Izbriši"

U prozoru opcija „brisanje“ imamo četiri mogućnosti, možemo odabrati radnju prema našem zahtjevu. Budući da moramo pomaknuti ćelije prema gore za položaj izbrisanih ćelija, odaberite "Pomakni ćeliju prema gore".

Imat ćemo nepromijenjene retke tablice 2.

Ova radnja u VBA zahtijeva upotrebu svojstva "XLUP" za izvođenje sličnog skupa radnji u VBA. Sada dođite do prozora VBA uređivača i pokrenite svoje ime makronaredbe.

Kodirati:

 Sub XLUP_Example () Kraj Sub 

Prvo opskrbite ćeliju RANGE koja će biti uključena u ovu operaciju. U ovoj akciji prve stanice koje se brišu i pomiču prema gore su stanice „A5: B5“.

Kodirati:

 Sub XLUP_Example () Raspon ("A5: B5") Kraj Sub 

Za ovaj raspon ćelija odaberite metodu "Izbriši".

Kodirati:

 Sub XLUP_Example () Raspon ("A5: B5"). Izbriši Kraj Sub 

Kao što vidite za metodu "Delete", imamo jedan neobavezni argument kao [Shift], za taj argument moramo unijeti argument kao "XLUP".

Kodirati:

 Sub XLUP_Example () Raspon ("A5: B5"). Izbriši shift: = xlUp Kraj Sub 

Sada možete pokrenuti ovaj kod ručno ili pomoću prečaca excel tipke F5 da biste vidjeli rezultat.

Kao što možete vidjeti u tablici 1, imamo red broj 6 pomaknut do 5. reda, a s druge strane tablica, red 2 (obojen) je nepromijenjen, pa pomoću opcije "VBA XLUP" možemo obaviti ovu operaciju.

Primjer # 2 - Pronađite zadnji korišteni redak pomoću XLUP-a

Zamislite situaciju u kojoj ste u A20th ćeliji (pogledajte donju sliku), a vaša posljednja korištena ćelija je A14.

Sada ako želite odabrati posljednju korištenu ćeliju (A14). kako ćete to učiniti pomoću tipke prečaca ???

Koristili bismo tipku Ctrl + tipka sa strelicom za gore za pomicanje do posljednje korištene ćelije s trenutnog položaja.

Tipka prečaca za prelazak u posljednju korištenu ćeliju 

Dakle, iz trenutne ćelije, Ctrl + strelica prema gore odabrale su posljednju korištenu ćeliju. Slično tome, u VBA kodiranju koristimo END (XLUP) da bismo izveli isto.

Sada se vratite na prozor VBA kodiranja.

In this window, we will perform the task of finding the last used row in the worksheet. Create a new subprocedure in the VBA window.

Code:

 Sub XLUP_Example1() End Sub 

To store the last used row number. define the variable as the VBA LONG data type.

Code:

 Sub XLUP_Example1() Dim Last_Row_Number As Long End Sub 

Now for this variable, we will assign the last used row number.

Code:

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

Now use RANGE object and open this object.

Code:

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

Now mention the active cell (A20) for RANGE object.

Code:

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

Now open END property for supplied range cell.

Code:

 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.