Getting last row in dataset as a wrapped function

Submitted by MisterBeck on Thu, 05/07/2020 - 12:17

Oftentimes you need to get the last row of a dataset so can do some manipulation on it. And while `Cells(Rows.Count, "A").End(xlUp).Row` is just one line, it's hard to remember. And yes, you can look it up each time but if you find yourself doing that multiple times a day, you can simply encapsulate that line in a function.

Function GetLastRow() as Long

    Dim lastRow As Long
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    GetLastRow = lastRow

End Function

Then it's a simple operation to call it up.

Dim lastRow as Long
lastRow = GetLastRow

Let's extend it a bit to handle some edge cases. You dataset may be in another worksheet and you furthest data point may not be in column "A." We will add some optional parameters to handle these values. The complete function is as follows:

Function GetLastRow(Optional ws As Worksheet, Optional col As Variant) As Long

    If ws Is Nothing Then Set ws = ActiveSheet
    If IsMissing(col) Then col = "A"

    Dim lastRow As Long
    lastRow = ws.Cells(Rows.Count, col).End(xlUp).Row
    GetLastRow = lastRow

End Function

If you don't set the params, they default to active sheet and column "A." The col param, being a Variant, can take a number or a string. The following function calls are all valid.

x = GetLastRow(, 1)
x = GetLastRow(, "A")
x = GetLastRow(ActiveSheet)
x = GetLastRow

I have found this function extremely useful and I keep it in my personal VBA library. My personal library will be the subject of coming posts, and I will post the complete library on GitHub, but for now we are going back to Snek. Stay tuned.



WilliamJew (not verified)

Sat, 09/10/2022 - 17:17

<a href=… For Audiences In Us Academic Settings</a>
<a href=>Ausbildung Machen Mit Geschichten</a>
<a href=>5 Preguntas Inc Modas Sobre Mi Madre</a>

<a href=… de portugues para qualquer pessoa</a> <a href=…; <a href=… et recits de ma famille</a> <a href=… exercises to write introductory paragraphs</a> <a href=>…; <a href=… esperienza di inglese 22</a> <a href=… scrivere un articolo di laurea online</a> <a href=…; <a href=…; <a href=…; <a href=… site</a> <a href=>Modelo de ficcao</a> <a href=…; <a href=…: Re:</a> <a href=…; <a href=>this site</a> <a href=… de noureev enfin traduite</a> <a href=>Goethe zertifikat b1 schreiben mit harald lesch</a> <a href=>… this website</a> <a href=>Tipps fur kinder</a> <a href=… indicateurs de l'ecriture</a> a60ad37

WilliamJew (not verified)

Sat, 09/10/2022 - 17:36

<a href=>Termina Tus Ensayos En Canvas Utp</a>
<a href=>Reime F R Enormes Selbstvertrauen</a>
<a href=>Din Mica De Tecido 2</a>

<a href=… this website</a>
<a href=>here</a&gt;
<a href=>here</a&gt;
<a href=… de quesos veganos online parte 1</a>
<a href=>this site</a>
<a href=>10 qualities to my family</a>
<a href=… mozart for kids</a>
<a href=>this site</a>
<a href=… this website</a>
<a href=… this website</a>
<a href=>Amostragem de poliana melhores momentos</a>
<a href=>La segunda guerra mundial en canva</a>
<a href=>here</a&gt;
<a href=… consigli per la d</a>

off white jordan 1 (not verified)

Sat, 09/10/2022 - 19:27

I have to express some thanks to this writer for bailing me out of such a predicament. As a result of exploring through the the net and seeing basics which were not beneficial, I believed my entire life was over. Existing devoid of the approaches to the issues you've resolved by means of your entire website is a crucial case, and the ones which could have in a negative way affected my career if I hadn't discovered the blog. Your skills and kindness in dealing with a lot of things was vital. I am not sure what I would've done if I had not encountered such a thing like this. I can also at this moment look ahead to my future. Thanks for your time so much for your high quality and sensible guide. I will not hesitate to refer your blog post to any person who desires counselling on this subject.
off white jordan 1

Eyegop (not verified)

Sat, 09/10/2022 - 21:01

kd shoes (not verified)

Sat, 09/10/2022 - 22:38

My husband and i felt very cheerful when Albert managed to complete his analysis through your precious recommendations he received from your very own blog. It's not at all simplistic to simply be giving freely concepts which some other people might have been making money from. We really remember we've got you to thank for this. All of the illustrations you made, the simple web site navigation, the relationships you can help promote - it's got all awesome, and it is aiding our son and the family consider that the issue is exciting, which is really essential. Thank you for all the pieces!
kd shoes

Jamespaf (not verified)

Sun, 09/11/2022 - 00:08

<a href="">amoxil 400 mg</a> <a href="">accutane pill</a> <a href="">cheapest tadalafil us</a> <a href="">trazodone 200 mg tablets</a> <a href="">generic cialis 10mg</a> <a href="">reputable online pharmacy no prescription</a> <a href="">best online pet pharmacy</a> <a href="">lisinopril without an rx</a>

WilliamJew (not verified)

Sun, 09/11/2022 - 00:33

<a href=>3 Simple Steps To Write First</a>
<a href=>Write An Essay On Video Calls</a>
<a href=>5 Dicas De Fotografia Para Reda O</a>

<a href=… this website</a>
<a href=>Parra… de lectura y redaccion i</a>
<a href=… de personnages pour un roman</a>
<a href=… dramma della tesi di inglese livello b1</a>
<a href=… cles de parole de connaissance</a>
<a href=>link</…;
<a href=>link</a&gt;
<a href=>here</a&gt;
<a href=…;
<a href=…;
<a href=>Organização de orçamento</a>
<a href=…;
<a href=>Re: Re:</a>
<a href=… valores para un comienzo intrigante</a>

Zakgop (not verified)

Sun, 09/11/2022 - 00:44

<a href="">how to purchase trazodone</a> <a href="">lasix without a rx</a> <a href="">where do you buy viagra</a> <a href="">seroquel 400</a> <a href="">no script viagra</a> <a href="">can you buy over the counter viagra in canada</a> <a href="">generic cialis 30 mg</a>

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.