Finding the last column

Excel VBA for Beginners 8- Must know Excel Logic
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.81
List Price:  €129.94
You save:  €37.13
£79.68
List Price:  £111.56
You save:  £31.87
CA$136.70
List Price:  CA$191.38
You save:  CA$54.68
A$151.47
List Price:  A$212.07
You save:  A$60.59
S$134.88
List Price:  S$188.84
You save:  S$53.96
HK$781.18
List Price:  HK$1,093.68
You save:  HK$312.50
CHF 90.49
List Price:  CHF 126.69
You save:  CHF 36.20
NOK kr1,087.35
List Price:  NOK kr1,522.33
You save:  NOK kr434.98
DKK kr692.92
List Price:  DKK kr970.11
You save:  DKK kr277.19
NZ$166.33
List Price:  NZ$232.87
You save:  NZ$66.53
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,943.35
List Price:  ৳15,321.13
You save:  ৳4,377.77
₹8,337.18
List Price:  ₹11,672.39
You save:  ₹3,335.20
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,738.77
List Price:  ₨38,835.40
You save:  ₨11,096.62
฿3,675.50
List Price:  ฿5,145.85
You save:  ฿1,470.34
₺3,235.04
List Price:  ₺4,529.19
You save:  ₺1,294.14
B$509.60
List Price:  B$713.47
You save:  B$203.86
R1,850.72
List Price:  R2,591.08
You save:  R740.36
Лв181.51
List Price:  Лв254.13
You save:  Лв72.61
₩135,590.93
List Price:  ₩189,832.73
You save:  ₩54,241.80
₪370.74
List Price:  ₪519.05
You save:  ₪148.31
₱5,705.78
List Price:  ₱7,988.32
You save:  ₱2,282.54
¥15,303.96
List Price:  ¥21,426.16
You save:  ¥6,122.20
MX$1,697.07
List Price:  MX$2,375.96
You save:  MX$678.89
QR363.69
List Price:  QR509.19
You save:  QR145.49
P1,358.38
List Price:  P1,901.79
You save:  P543.40
KSh13,211.65
List Price:  KSh18,496.84
You save:  KSh5,285.18
E£4,788.95
List Price:  E£6,704.73
You save:  E£1,915.77
ብር5,723.72
List Price:  ብር8,013.44
You save:  ብር2,289.71
Kz83,591.64
List Price:  Kz117,031.64
You save:  Kz33,440
CLP$94,219
List Price:  CLP$131,910.38
You save:  CLP$37,691.37
CN¥723.64
List Price:  CN¥1,013.13
You save:  CN¥289.48
RD$5,794.19
List Price:  RD$8,112.10
You save:  RD$2,317.90
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.06
List Price:  Q1,085.12
You save:  Q310.05
GY$20,860.22
List Price:  GY$29,205.14
You save:  GY$8,344.92
ISK kr13,949.49
List Price:  ISK kr19,529.85
You save:  ISK kr5,580.35
DH1,005.73
List Price:  DH1,408.07
You save:  DH402.33
L1,766.28
List Price:  L2,472.87
You save:  L706.58
ден5,712.52
List Price:  ден7,997.75
You save:  ден2,285.23
MOP$802.57
List Price:  MOP$1,123.63
You save:  MOP$321.06
N$1,845.78
List Price:  N$2,584.16
You save:  N$738.38
C$3,670.67
List Price:  C$5,139.09
You save:  C$1,468.41
रु13,313.56
List Price:  रु18,639.52
You save:  रु5,325.95
S/372.22
List Price:  S/521.13
You save:  S/148.90
K385.44
List Price:  K539.63
You save:  K154.19
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,689.66
List Price:  ZK3,765.63
You save:  ZK1,075.97
L462.03
List Price:  L646.86
You save:  L184.83
Kč2,326.92
List Price:  Kč3,257.79
You save:  Kč930.86
Ft36,152.68
List Price:  Ft50,615.20
You save:  Ft14,462.51
SEK kr1,078.64
List Price:  SEK kr1,510.15
You save:  SEK kr431.50
ARS$87,815.44
List Price:  ARS$122,945.14
You save:  ARS$35,129.69
Bs690.51
List Price:  Bs966.74
You save:  Bs276.23
COP$388,367.89
List Price:  COP$543,730.59
You save:  COP$155,362.69
₡50,962.55
List Price:  ₡71,349.61
You save:  ₡20,387.06
L2,463.20
List Price:  L3,448.58
You save:  L985.38
₲746,475.93
List Price:  ₲1,045,096.16
You save:  ₲298,620.23
$U3,821.56
List Price:  $U5,350.33
You save:  $U1,528.77
zł401.98
List Price:  zł562.79
You save:  zł160.80
Already have an account? Log In

Transcript

In this video, we're going to speak about how you can find the last row on your data set using Excel VBA. So finding the last row on your data set using Excel VBA is very important because if you're dealing with a data set like this using VBA code, and let's say you're copying good data, you need to make sure that your code can determine what the last row for the data is, regardless of how many rows the data has. So if for example, the next time you run the macro on this data set, instead of having 18 rows, it would have 30 rows, for example, you need to make sure that you have code that can determine each time how many rows there are on your data set so as to only select the rows that contain the data set, and you wouldn't select extra rows that are actually blank.

Okay, so this is something that is very important to know why you're going to use it on nearly every time time you write a macro that deals with a dataset. Okay, so finding your last row actually involves three steps. The first step is actually starting at the last row in Excel. So actually, we can get to the last row in Excel by pressing Ctrl and down. But this does not happen on the code on the macro, the macro just starts at the last row. Okay, so this is the starting point of your macro.

And then excel vba actually jumps up until it hits the last row on your data set. And you can jump up actually by pressing Ctrl and up and this is actually what the code does. So it starts at the last row and the code you can think of it as pressing CTRL and up till it hits the last row on your dataset, which here in our case is going to be row number 18. And then the third step is asking what row on my on? Okay, so this is actually the third step. Step One Pro on my own.

And of course, these three steps actually happen in just one line of code. Okay, but this is how you can understand it, just go to the last row, jump up until you hit the last row on my data set, and then ask the question, which row on my on? Okay, so I've opened the Visual Basic Editor here. And basically, in order to determine the last row on your data set, you need to create a variable because this variable is going to contain the row number for the last row. And the row number is going to be basically an integer, right? It's just a number, and it cannot be a decimal.

So I'm assigning to it the integer data type. And also the single data type can work because the single data type can work with both integers and decimals as well. And if your data set contains more than 32,000 rows, then this integer data type will not be enough. Right. So you need to go higher go single, go double whatever. So the last row, actually, you're going to determine it using the statement, cells rows dot count, and then comma one, and then dot end, Excel up dot row.

Okay, so that's analyze that statement and see what it does. Basically, I'm using the cells collection object to go to a cell. This cells address, the row number is going to be rows count, which is actually the number of rows in my Excel workbook. And we're writing it as rows dot count, instead of writing 1 million something because first of all, it's easier to write it this way, because you don't have to memorize the number. And the second thing is that if you're using Excel 2003, Excel 2003 only had 65,000 rows. So this ensures that you select the last row on your worksheet regardless The number of rows because you're actually using the count property for the rows collection object to determine the number of rows.

And then this is the column number for the cell that we're going to start from. And the column number here is one. And it could be anything because this will depend on your data set. In our case here, I'm actually going to assume that the name column is the one that always has values because the column that you base your last row on should be the column that always has values. Like for example, the employee ID, and employee couldn't not have a name on the record or the date of birth, but he can always have an ID if that's how your company works. For example, here, there will always be a name, for example, in this data set, but there could be a row where there's no date or there is no item sold or so.

But this data set can always contain the name so you need to make sure to base your row counting on the column. That you suspect that he will always have data, okay, or that you think that it will always have data in it. So this is the first step, I'm starting on the last row in Excel. And then dot and excel up. This is basically pressing Ctrl N up. Okay, so this is the second step that I'm doing.

So the first step is starting in the last cell in Excel. Second step here is pressing Ctrl N up. And when I do that, I'm hitting the last row on my data set. So the third step is asking for the row number with the dot row property. So cells, rows dot count, comma, one dot end Excel up. So I'm on the last row in Excel, pressing CTRL and up, and then I'm hitting a cell and then I'm asking for the row property, which will give me the road number for that particular cell, which in our case, should be 18.

So if we step through this code step by step, using f8 on our keyboard, when we execute this line of code, you can see our LR is equal to 18. And that means the our last row is row number 18, which is correct if let's say we've added any rows to this dataset. So now we've got one extra row and we're run this code again. So let's run it again step by step as well. You can see our the LR is has become 19. So that means that when you put this line of code in your code, this will dynamically determine what the last row on your data set is.

So if your data set expands, so number of rows increases or shrinks, number of rows decreases. You will always make sure that your macro will be able to determine how many rows your data set has. That is actually a very Important thing to learn in VBA. And you need to actually memorize this statement, just as you memorize your name. Okay, this is a very important statement that you're going to use a lot. There is another way that you can determine what the last row is, although it's not recommended, but I'll just explain it for you.

Basically, you can start at any cell in your data set. And if you press Ctrl, and down, let's see what happens when you press Ctrl and down, you actually end up on the last non blank cell, okay, it's not going to be essentially the song the last row because if there is a blank cell in the middle, you're going to end up on it. So this is why I'm not recommending it. For example, if we insert a row here, and this row is blank, and if I press Ctrl and down, you can see here I've stopped actually the last non blank row but there is a row here that is blank. And this is not my last row. So this is why this way is not recommended.

Because if you press Ctrl and down and write the code this way, and this way is actually cells one comma one, or it could be any cell in your data set dot end Excel down, and then the Excel means press Ctrl and down, and then ask for the row number. If there's a blank cell, you're not going to reach your last row. And you could end up with having the last row to be incorrect. This is why you're not going to see the second way that we've explained here being used often and you're actually going to see the first way of doing it being used more often. So rows dot count, comma one dot and excel up dot row. So that's it, guys for finding the last row in Excel VBA.

It's a very important thing to understand. Thank you very much watching this video and I'll see you on the next one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.