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

Thomascax (not verified)

Sun, 09/20/2020 - 05:40

Спасибо за совет

<a href=https://android-yte.ru/category/telefonyi/>Водостойкий планшет</a>

<a href=https://bellavistahotel.ru/category/solnechnaya-energetika-paneli/>свет… на солнечных батареях</a>

<a href=https://autorp.ru/category/fizika/>НОВОСТИ АВТОПРОМА</a>

<a href=https://tcareva-apteka.ru>МАШИННОЕ ОБУЧЕНИЕ И ИИ</a>

My spouse and i have been very cheerful that Louis managed to conclude his inquiry via the ideas he came across out of the site. It's not at all simplistic to simply happen to be handing out guidance the rest could have been making money from. Therefore we acknowledge we need the website owner to give thanks to for this. These explanations you have made, the straightforward web site navigation, the relationships your site make it possible to promote - it's got all superb, and it's letting our son and our family consider that this idea is exciting, and that is quite essential. Many thanks for everything!
[url=http://www.jordan4.us.com]jordan shoes[/url]

My spouse and i were more than happy that Chris managed to finish off his investigation through your precious recommendations he grabbed in your blog. It is now and again perplexing to just possibly be handing out things which usually most people may have been trying to sell. Therefore we acknowledge we've got the writer to thank because of that. The type of illustrations you have made, the easy blog menu, the relationships you will make it easier to promote - it's got mostly fantastic, and it is aiding our son in addition to the family feel that this matter is pleasurable, which is certainly exceptionally fundamental. Many thanks for all!
[url=http://www.off-whiteshoes.us.com]off white nike[/url]

I intended to write you that little bit of note to say thanks a lot once again just for the incredible information you've provided on this page. It was so pretty open-handed with you to allow without restraint just what a lot of folks would've offered as an e book to generate some cash for themselves, chiefly seeing that you could have done it if you wanted. Those tricks in addition acted to become easy way to realize that someone else have the identical desire much like my personal own to understand many more when considering this condition. I'm certain there are some more fun instances ahead for many who looked over your site.
[url=http://www.chromehearts.com.co]chrome hearts[/url]

My wife and i have been quite glad that Raymond managed to deal with his inquiry out of the precious recommendations he gained when using the web site. It is now and again perplexing to just possibly be handing out guides which often the others could have been trying to sell. And we also understand we have got you to give thanks to for that. Most of the illustrations you made, the easy blog menu, the relationships your site make it easier to foster - it is mostly extraordinary, and it is leading our son in addition to us know that this article is brilliant, and that's tremendously vital. Thanks for all!
[url=http://www.monclercoat.us.com]moncler[/url]

I simply desired to thank you very much once again. I'm not certain the things that I could possibly have used without these pointers contributed by you over this problem. It previously was a very hard setting in my circumstances, however , viewing this well-written way you managed it made me to jump for gladness. Now i'm grateful for the guidance and then wish you find out what a powerful job you are putting in educating many others via your web blog. I am certain you haven't come across any of us.
[url=http://www.airjordansstore.com]air jordan[/url]

I happen to be commenting to let you know of the awesome encounter my cousin's girl obtained browsing your site. She even learned a lot of pieces, which included how it is like to possess a great teaching style to get the rest effortlessly know just exactly selected grueling topics. You really did more than visitors' desires. Thank you for providing the powerful, trusted, edifying and as well as cool guidance on this topic to Emily.
[url=http://www.supremeclothing.us.org]supreme[/url]

I simply wanted to thank you so much again. I do not know the things that I would've carried out without these opinions provided by you on such a subject matter. Entirely was the traumatic dilemma in my view, nevertheless taking a look at a specialised avenue you handled the issue made me to leap with fulfillment. Now i'm happy for the information as well as pray you are aware of a great job you were getting into training people by way of your webpage. I know that you have never come across any of us.
[url=http://www.jordansforcheap.us.com]cheap jordans[/url]

Needed to post you the very little remark to finally say thank you the moment again with your magnificent views you have shared at this time. It has been so surprisingly generous of you to give unhampered what exactly many people might have offered for an electronic book in order to make some cash for themselves, certainly since you might well have done it if you ever considered necessary. These good ideas in addition served to become good way to recognize that some people have a similar interest just like mine to figure out very much more when considering this condition. I am sure there are a lot more pleasurable periods ahead for individuals that find out your blog post.
[url=http://www.giannisantetokounmposhoes.us]giannis shoes[/url]

I simply wanted to appreciate you yet again. I do not know the things I would have followed without the type of solutions shared by you directly on that question. It had been the difficult case in my position, however , finding out the very specialised manner you treated that made me to cry for contentment. Now i am grateful for the work and as well , sincerely hope you really know what a great job that you're getting into instructing the rest by way of a web site. I know that you've never encountered any of us.
[url=http://www.jordan11retro.us.com]jordan 11[/url]

I simply needed to thank you so much once again. I'm not certain what I would've achieved in the absence of the entire strategies shown by you on my situation. It actually was a real terrifying condition in my position, nevertheless discovering a specialized approach you managed it made me to weep for contentment. Extremely happy for your work and even pray you recognize what an amazing job your are providing training people using a blog. I'm certain you haven't encountered any of us.
[url=http://www.michaeljordanshoes.us.com]michael jordan shoes[/url]

<a href="http://www.communitize.net/nike-eagles-17-harold-carmichael-whitepink-w… eagles 17 harold carmichael whitepink womens stitched nfl limited rush fashion jersey</a> <a href="http://www.fotowerft.net/womens-nike-sb-stefan-janoski-max-orange-black… nike sb stefan janoski max orange black billig</a> <a href="http://www.brassmedia.net/nike-shox-navina-cheap-white-and-red-shoes-sc… shox navina cheap white and red shoes</a> <a href="http://www.musiserv.net/men-nike-carolina-panthers-88-greg-olsen-limite… nike carolina panthers 88 greg olsen limited blue alternate nfl jersey sale</a> <a href="http://www.gastropol.net/2014-air-jordan-5-mens-suede-leather-gray-bill… air jordan 5 mens suede leather gray billig</a> <a href="http://www.stringwise.net/adidas-originals-superstar-ii-grey-runningcc"… originals superstar ii grey</a>
[url=http://www.sanjosedj.net/]sanjosedj[/url]

I wanted to compose you the very little observation to help give thanks the moment again for the magnificent principles you have featured on this site. It was really particularly open-handed of people like you to supply without restraint just what many individuals could possibly have offered as an e-book to get some profit for themselves, especially considering the fact that you might well have tried it if you desired. These advice in addition worked like the easy way to realize that most people have similar desire like my personal own to find out more with reference to this problem. I am certain there are millions of more enjoyable opportunities up front for many who examine your blog post.
[url=http://www.supremeclothingsstore.com]supreme clothing[/url]

I am writing to let you be aware of what a amazing experience my princess found going through yuor web blog. She learned so many things, with the inclusion of what it's like to have a very effective coaching spirit to get folks quite simply learn certain advanced issues. You undoubtedly exceeded readers' expected results. I appreciate you for supplying these warm and helpful, trustworthy, edifying and easy thoughts on that topic to Mary.
[url=http://www.yeezyboost380.us.com]yeezy boost 380[/url]

Thanks a lot for giving everyone remarkably breathtaking opportunity to read articles and blog posts from this site. It's usually so brilliant and also packed with fun for me and my office peers to visit your blog more than three times in a week to study the newest tips you will have. Not to mention, I'm also actually fulfilled with all the outstanding strategies you serve. Some 4 facts in this posting are easily the most beneficial we have all had.
[url=http://www.golden-goose.us]golden goose sneakers[/url]

I must convey my love for your generosity giving support to those who need guidance on that concern. Your personal dedication to passing the message all around appears to be extremely important and has continuously made girls much like me to reach their targets. Your entire insightful information means a great deal a person like me and extremely more to my peers. Many thanks; from all of us.
[url=http://www.stephcurryshoes.com]curry shoes[/url]

My spouse and i have been glad Louis managed to conclude his basic research with the ideas he had from your web pages. It is now and again perplexing just to be giving away key points that the rest could have been trying to sell. And we also take into account we need the blog owner to give thanks to because of that. The illustrations you have made, the simple blog menu, the relationships you can assist to instill - it's most unbelievable, and it's really assisting our son in addition to us feel that this theme is pleasurable, which is certainly exceptionally important. Many thanks for all!
[url=http://www.hermesbirkinbags.us.com]hermes birkin[/url]

I really wanted to compose a simple message so as to say thanks to you for all the awesome suggestions you are giving out at this site. My time intensive internet investigation has finally been rewarded with useful facts and strategies to go over with my friends. I would assume that we visitors actually are unquestionably blessed to live in a really good website with very many wonderful professionals with helpful points. I feel extremely grateful to have discovered the weblog and look forward to tons of more exciting minutes reading here. Thanks once more for all the details.
[url=http://www.hermesbirkinbags.us.com]hermes birkin[/url]

I must voice my admiration for your generosity giving support to visitors who must have assistance with this particular concern. Your real commitment to passing the message all through became astonishingly beneficial and has surely allowed some individuals much like me to achieve their pursuits. The warm and helpful suggestions entails a whole lot to me and far more to my mates. Thank you; from everyone of us.
[url=http://www.supremesoutlet.us.com]supreme[/url]

I must show thanks to this writer just for bailing me out of this type of incident. Just after looking throughout the online world and coming across basics that were not beneficial, I believed my life was gone. Living without the presence of answers to the difficulties you've resolved as a result of your entire article content is a serious case, and those which might have badly damaged my entire career if I had not come across your site. Your actual ability and kindness in taking care of the whole lot was helpful. I am not sure what I would have done if I hadn't discovered such a step like this. I am able to now relish my future. Thanks so much for the professional and result oriented help. I will not hesitate to propose your web page to anyone who wants and needs guidelines about this matter.
[url=http://www.kobebasketballshoes.net]kobe basketball shoes[/url]

I am only commenting to let you know of the magnificent encounter my girl experienced reading your site. She realized such a lot of details, which included what it is like to possess a wonderful giving character to get other folks smoothly know specific hard to do topics. You truly surpassed people's desires. Many thanks for supplying such essential, trustworthy, informative and in addition unique guidance on the topic to Ethel.
[url=http://www.kevindurant-shoes.us.com]kevin durant shoes[/url]

Thank you so much for providing individuals with an exceptionally brilliant opportunity to discover important secrets from this blog. It is often very pleasurable and as well , full of a great time for me personally and my office fellow workers to search the blog not less than thrice every week to learn the fresh guides you have. Of course, I'm just at all times motivated with all the unique knowledge served by you. Some 1 areas in this posting are absolutely the most impressive I've had.
[url=http://www.jordansforcheap.us.com]cheap jordans[/url]

I and my pals happened to be digesting the best information and facts on the blog and the sudden developed a horrible suspicion I had not expressed respect to the web blog owner for those secrets. All the guys appeared to be as a result excited to read through them and already have surely been making the most of those things. Thanks for actually being very kind and then for pick out certain helpful information most people are really needing to be aware of. My very own honest regret for not expressing appreciation to earlier.
[url=http://www.kawhileonardshoes.us]kawhi leonard shoes[/url]

I have to express my love for your kindness for people who really want help on this particular concept. Your real commitment to passing the solution up and down had become especially beneficial and have all the time allowed guys and women just like me to achieve their desired goals. Your personal valuable suggestions indicates this much a person like me and even more to my peers. Thank you; from all of us.
[url=http://www.curry7.us]curry 7 shoes[/url]

Thanks for your whole hard work on this website. My daughter really loves going through investigation and it is simple to grasp why. A lot of people hear all regarding the powerful form you deliver both interesting and useful tricks on your website and boost response from the others about this concept then our own child is undoubtedly discovering a lot of things. Enjoy the rest of the year. You're the one carrying out a superb job.
[url=http://www.supremeshoodie.com]supreme hoodie[/url]

I precisely had to thank you so much yet again. I am not sure what I could possibly have achieved in the absence of those hints documented by you about that concern. It was actually a real frightening circumstance in my position, nevertheless witnessing a new well-written mode you treated the issue took me to leap over gladness. I'm just happy for this help and wish you recognize what an amazing job you happen to be accomplishing teaching people by way of your blog post. Most probably you haven't met any of us.
[url=http://www.yeezy-sneakers.us]yeezy[/url]

I wanted to compose you the very small remark to give many thanks the moment again considering the superb secrets you've discussed at this time. It has been so particularly open-handed of people like you to supply easily what exactly most of us would've offered for sale for an electronic book to help make some dough for their own end, especially since you could possibly have done it if you desired. Those tactics additionally acted like the fantastic way to be certain that other people online have the same interest really like my personal own to grasp way more in regard to this condition. I am certain there are lots of more pleasant sessions ahead for folks who look over your website.
[url=http://www.jordan1shoes.us]jordan 1[/url]

I precisely had to say thanks once more. I am not sure the things I would have made to happen in the absence of these advice contributed by you concerning such a concern. It became a horrifying scenario in my circumstances, nevertheless coming across your expert form you processed it forced me to weep over joy. I'm happier for this assistance and believe you find out what a powerful job you happen to be doing teaching many others with the aid of your web site. I am certain you've never met any of us.
[url=http://www.hermes-handbags.us.com]hermes[/url]

I would like to express my passion for your generosity for persons that absolutely need help on this important content. Your special dedication to getting the solution all through ended up being definitely functional and has usually allowed folks just like me to arrive at their goals. The informative instruction means so much to me and especially to my mates. Thank you; from everyone of us.
[url=http://www.bapehoodie.us]bape hoodie[/url]

Add new comment

Plain text

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