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.

 

Comments

WilliamJew (not verified)

Sat, 09/10/2022 - 17:17

<a href=https://reijelmonsdincachab.tk/post/Procreate-For-Audiences-In-Us-Acade… For Audiences In Us Academic Settings</a>
<a href=https://syfilde.tk/post/Ausbildung-Machen-Mit-Geschichten>Ausbildung Machen Mit Geschichten</a>
<a href=https://starmarweld.tk/post/5-Preguntas-Inc-Modas-Sobre-Mi-Madre>5 Preguntas Inc Modas Sobre Mi Madre</a>

<a href=https://heritage-digitaltransitions.com/commercial-reproduction-and-dig… de portugues para qualquer pessoa</a> <a href=https://www.zimbrafr.org/forum/topic/162406-billig-abilify-in-der-onlin…; <a href=https://www.zimbrafr.org/forum/topic/131851-clomiphene-serophene-50mg-o… et recits de ma famille</a> <a href=http://www.basinstreetrecords.biz/new-orleans-jazz-heritage-festival/#c… exercises to write introductory paragraphs</a> <a href=https://evosoluzioni.com/cropped-20200310_153543-1-jpg/#comment-630582>…; <a href=http://j.ctfda.com/viewthread.php?tid=1139726&pid=3224947&page=1&extra=… esperienza di inglese 22</a> <a href=https://diariosdelchaco.com/adelantan-la-audiencia-publica-para-debatir… scrivere un articolo di laurea online</a> <a href=https://itein.com.mx/blog-itein/2018/03/14/big-data-y-las-criptodivisas…; <a href=https://www.zimbrafr.org/forum/topic/127645-gry-za-pieniadze-i-casino-p…; <a href=https://steliam.com/product/wireless-keyboard-mouse/comment-page-3862/#…; <a href=https://webdevsupply.com/i-quit-in-the-midst-of-a-pandemic-and-went-to-… site</a> <a href=https://www.aafrc.org/best-cat-bed/#comment-259298>Modelo de ficcao</a> <a href=https://tricoloristra.ru/news/ustanovka-sputnikovykh-antenn/#comment227…; <a href=http://182.74.213.166/PROJECTS/usha-international-19083594-drupal/en/ar…: Re:</a> <a href=https://bengalinewspaper.info/showthread.php?tid=155806&pid=371590#pid3…; <a href=https://nysapower.com/product/solar-gardening-lights/#comment-8721>this site</a> <a href=https://hlebspb.ru/gb/msg-2.html&name1=WilliamSpace&email1=pdaniels%254… de noureev enfin traduite</a> <a href=http://americanmacro.com/uncategorized/hello-world/#comment-14548>Goethe zertifikat b1 schreiben mit harald lesch</a> <a href=https://www.agrapole.eu/events/formation-haccp-qualite/#comment-531784>… this website</a> <a href=https://tesladv.ru/products/rele-vremeni-rsz-522ff/#comment_131535>Tipps fur kinder</a> <a href=https://dixitp.com/saintout-la-unidad-siempre-es-el-camino/comment-page… indicateurs de l'ecriture</a> a60ad37

WilliamJew (not verified)

Sat, 09/10/2022 - 17:36

<a href=https://dysfunkbifibto.gq/post/Termina-Tus-Ensayos-En-Canvas-Utp>Termina Tus Ensayos En Canvas Utp</a>
<a href=https://penmaro.ml/post/Reime-F-R-Enormes-Selbstvertrauen>Reime F R Enormes Selbstvertrauen</a>
<a href=https://oxincolgastti.ga/post/Din-Mica-De-Tecido-2>Din Mica De Tecido 2</a>

<a href=https://gswill.com/product/vapormax-low-top-nike-sneakers-wine-navy-blu… this website</a>
<a href=https://brandonarthurroth.com/gwbush#comment-74936>here</a&gt;
<a href=http://blog.spicyrocket.com/public/posts/158>here</a&gt;
<a href=https://amster-brand.ru/blog/testovaya-zapis-dlya-novostej#comment_1796… de quesos veganos online parte 1</a>
<a href=http://stewartwho.com/travel/te-quiero-ecuador/#comment-2205317>this site</a>
<a href=https://winbeefarms.com/product/cuckoo-maran/#comment-24398>10 qualities to my family</a>
<a href=http://mocchaumoc.com/du-lich-moc-chau-nhat-dinh-phai-doc-bai-viet-nay/… mozart for kids</a>
<a href=http://zollian.com/product/brown-basic-top/#comment-148377>this site</a>
<a href=http://forumx.fearnode.net/general-help/4653305/oral-bi-irrigator?page=… this website</a>
<a href=http://codercoder.cn/index.php/2020/05/mysql-note19-mysql-proxy-tool-pr… this website</a>
<a href=http://gisthai.org/webboard/Question.asp?GID=149&no=9>Amostragem de poliana melhores momentos</a>
<a href=http://lifeisguz.com/hello-world#comment-9231>La segunda guerra mundial en canva</a>
<a href=https://www.aafrc.org/best-dog-boots/#comment-264689>here</a&gt;
<a href=http://xyztec-korea.com/bbs/zboard.php?id=board&page=2&page_num=20&sele… consigli per la d</a>
c0f988b

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 http://www.off-whiteshoes.us.com

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 http://www.kd14shoes.com

Jamespaf (not verified)

Sun, 09/11/2022 - 00:08

<a href="http://amoxicillina.online/">amoxil 400 mg</a> <a href="http://isotretinoin.xyz/">accutane pill</a> <a href="http://cheapcialistabletprescription.monster/">cheapest tadalafil us</a> <a href="http://trazodone.win/">trazodone 200 mg tablets</a> <a href="http://cialis20mgtabs.quest/">generic cialis 10mg</a> <a href="http://onlinepharmacy.date/">reputable online pharmacy no prescription</a> <a href="http://pharmacyonline.online/">best online pet pharmacy</a> <a href="http://lisinopril.bid/">lisinopril without an rx</a>

WilliamJew (not verified)

Sun, 09/11/2022 - 00:33

<a href=https://reijelmonsdincachab.tk/post/3-Simple-Steps-To-Write-First>3 Simple Steps To Write First</a>
<a href=https://soundvenkadado.tk/post/Write-An-Essay-On-Video-Calls>Write An Essay On Video Calls</a>
<a href=https://crimizdo.ml/post/5-Dicas-De-Fotografia-Para-Reda-O>5 Dicas De Fotografia Para Reda O</a>

<a href=https://www.vituliaexperience.com/2019/03/03/mungibeddu/#comment-163004… this website</a>
<a href=https://thecookbookqueen.com/grilled-greek-chicken/#comment-76695>Parra… de lectura y redaccion i</a>
<a href=https://www.pcstacks.com/remove-write-protection-on-micro-sd-card/#comm… de personnages pour un roman</a>
<a href=https://sachychodov.cz/cs/clanky/podzimni-turnaj?page=2728#comment-6800… dramma della tesi di inglese livello b1</a>
<a href=https://www.pcstacks.com/disable-volume-overlay-in-windows/#comment-179… cles de parole de connaissance</a>
<a href=https://thatnutritiousglow.info/navratri-diet-plan/#comment-2285>link</…;
<a href=https://cfah.org/cbd-for-weight-loss/#comment-1205000>link</a&gt;
<a href=https://cacfig.org/hello-world/#comment-6973>here</a&gt;
<a href=https://www.zimbrafr.org/forum/topic/7545-recuperer-ses-donnees-lorsque…;
<a href=https://www.zimbrafr.org/forum/topic/15748-aliskarola-not/page__st__600…;
<a href=https://celebritykink.com/non-nude-selena-gomez/#comment-15602>Organização de orçamento</a>
<a href=https://businessgeek.uk/investing-in-whisky-making-money-from-liquid-go…;
<a href=https://www.ecotunisie.com/service-geosynthetics/#comment-12901>Re: Re:</a>
<a href=https://mercedes-world.com/news/mercedes-w196r-lego-ideas/comment-page-… valores para un comienzo intrigante</a>
dc4ed8a

Zakgop (not verified)

Sun, 09/11/2022 - 00:44

<a href="https://trazodone.download/">how to purchase trazodone</a> <a href="https://furosemide.works/">lasix without a rx</a> <a href="https://viagrapriceonline.monster/">where do you buy viagra</a> <a href="https://seroquel.email/">seroquel 400</a> <a href="https://bestviagradrugwithnorx.monster/">no script viagra</a> <a href="https://onlineviagra150mgcost.quest/">can you buy over the counter viagra in canada</a> <a href="https://cialisbestpharmacy.monster/">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.