Explanation of Code in VBA

seasmith

New Member
Joined
Jul 6, 2011
Messages
44
Range("A65536").End(xlUp)(1, 2) = txtTitle.Text 'Prints the text in the Title column'


This code goes to a text box on a form and it prints whatever I type in the text box directly into the first blank row in the second column. I found this code on this site, but I would like to understand it better as to what it is actually doing especially what the .End(xlUp) part is? If you have any knowledge about this it would be much appreciated. Thank you ahead of time
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You "go" to the last cell in column A, which was row 65536 in Excel 2003 and some version before. Now it's a lot more.

The End(xlUp) is the equivalent of Ctrl-Arrow up to find the next cell in the same column that is not empty.

The (1,2) then offsets to the correct cell in column B, one row below.
 
Upvote 0
The end(xlUp) is simulating pressing CTRL + UP on your keyboard.
So it's as if you went to A65536, then pressed CTRL + UP
It finds the last occupied cell in column A.

Then the (1,2) after that is almost like an offset..
More like index.

From whatever cell the end(xlup) found (say it was A25)
It then Indexes from there 1 row and 2 columns (INCLUDING A25).
So 1 row from A25 (including A25) is A25
2 Columns from A25(including A25) is B25

so it ends up with B25


Hope that helps.
 
Upvote 0
Thanks! That was a great explanation. Just curious, is this a common way used to find the next empty row?
 
Upvote 0
Thanks! That was a great explanation. Just curious, is this a common way used to find the next empty row?

Yes, it's very common.
But it does NOT find the next empty row...
It actually finds the LAST NON EMPTY row...
There's a big difference.

Standby for more..
 
Upvote 0
If it finds the last non empty row why is it putting whatever I type in the text box in the row after this which is the next empty row? Does it have something to do with the offset?
 
Upvote 0
Range("A65536").End(xlUp)(1, 2)

The red part finds the last non empty cell in column A
the blue part then goes from that cell 1 down and 2 right (INCLUDING the previous cell)
 
Upvote 0
Just one last note...

Instead of Range("A65536")
It's wise to use Range("A" & Rows.Count)

Because Different versions of Excel have different amount of Rows available.
XL2003 has 65536
XL2007 and above have 1 Million +

The purpose of the 65536 was to look at the end of the sheet, then go up.
This may fail in other versions if they are using more than that many rows.
Rows.Count will always = the amount of rows available, regardless of XL version.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top