VBA XLUP | Kako uporabiti VBA XLUP v Excelu? (s primeri)

Excel VBA XLUP

Med pisanjem kode VBA morate biti pozorni na to, kaj počnete z običajnim delovnim listom in lahko isto stvar ponovite tudi v VBA. Ena takih ključnih besed pri kodiranju VBA je »XLUP«, v tem članku vam bomo pokazali, kaj je ta ključna beseda v kodiranju VBA in kako jo uporabiti pri kodiranju.

Kako uporabiti VBA XLUP pri kodiranju?

Sledijo primeri excela VBA XLUP.

To predlogo VBA XLUP Excel lahko prenesete tukaj - Predloga VBA XLUP Excel

Primer # 1 - Premakni celice v izbrisani položaj celic

Oglejte si na primer scenarij spodnjih podatkov, kjer morate te barvne celice izbrisati in še več podatkov spodnjih vrstic do zgornjih podatkov.

Eden od načinov tega izbrisa na delovnem listu je izbrati tiste celice, v katerih lahko preprosto izbrišemo celotno vrstico. Toda tu so situacije malce zapletene, ker imam v tabeli 1 barvne celice, ko izbrišemo celo vrstico, tudi vrstice tabele 2 se tudi izbrišejo, vendar ne želimo, da bi se to zgodilo, temveč moramo samo izbrisati barvne vrstice, spodnje celice pa navzgor položaj izbrisanih celic.

Najprej izberite barvne celice in pritisnite Ctrl + Simbol minus (-), da odprete možnost »Delete«.

Bližnjična tipka za odpiranje možnosti »Delete«

V oknu z možnostmi "izbriši" imamo štiri možnosti, lahko izberemo dejanje v skladu z našo zahtevo. Ker moramo premakniti celice navzgor za položaj izbrisanih celic, izberite »Shift Cell Up«.

Imeli bomo nespremenjene vrstice v tabeli 2.

To dejanje v VBA zahteva uporabo lastnosti »XLUP« za izvedbo podobnega niza dejanj v VBA. Zdaj pridite do okna urejevalnika VBA in zaženite ime makra.

Koda:

 Sub XLUP_Primer () Konec Sub 

Najprej vnesite celico RANGE, ki bo vključena v to operacijo. V tem postopku so prve celice, ki jih je treba izbrisati in se premakniti navzgor, celice “A5: B5”.

Koda:

 Sub XLUP_Example () Obseg ("A5: B5") Konec Sub 

Za ta obseg celic izberite metodo »Delete«.

Koda:

 Sub XLUP_Example () Obseg ("A5: B5"). Izbriši Konec Sub 

Kot lahko vidite pri metodi “Delete”, imamo en izbirni argument kot [Shift], za ta argument moramo argument vnesti kot “XLUP”.

Koda:

 Sub XLUP_Example () Obseg ("A5: B5"). Izbriši premik: = xlUp End Sub 

Zdaj lahko to kodo zaženete ročno ali prek bližnjice excel s tipko F5, da si ogledate rezultat.

Kot lahko vidite v tabeli 1, imamo vrstico številka 6 pomaknjeno navzgor do 5. vrstice, na drugi strani pa je vrstica 2 (barvna) nespremenjena, zato lahko z uporabo možnosti »VBA XLUP« izvedemo to operacijo.

2. primer - Poiščite zadnjo uporabljeno vrstico z uporabo XLUP

Predstavljajte si situacijo, ko ste v celici A20. (Poglejte spodnjo sliko) in je vaša zadnja uporabljena celica A14.

Zdaj, če želite izbrati zadnjo uporabljeno celico (A14). kako boš z bližnjico ???

Za premik do zadnje uporabljene celice s trenutnega položaja bi uporabili tipko Ctrl + puščica navzgor .

Bližnjična tipka za premik v zadnjo uporabljeno celico 

Torej, iz trenutne celice je Ctrl + puščica gor izbrala zadnjo uporabljeno celico. Podobno pri kodiranju VBA uporabljamo END (XLUP) za izvedbo istega.

Zdaj se vrnite v okno za kodiranje VBA.

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.