Advertisement
AlanElston

Last Row

Jan 7th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.35 KB | None | 0 0
  1. Sub FindlastRowInColumn_HayBill() ' https://www.excelforum.com/hello-introduce-yourself/1214555-an-old-geezer-coming-over-from-the-access-forum.html
  2. ' Worksheets info for first worksheet
  3. Dim Ws1 As Worksheet            '_-Dim: Prepares "Pointer" to a "Blue Print" (or Form, Questionaire not yet filled in, a template etc.)"Pigeon Hole" in Memory, sufficient in construction to house a piece of Paper with code text giving the relevant information for the particular Variable Type. VBA is sent to it when it passes it. In a Routine it may be given a particular “Value”, or (“Values” for Objects).  There instructions say then how to do that and handle(store) that(those). At Dim the created Paper is like a Blue Print that has some empty spaces not yet filled in. A String is a a bit tricky. The Blue Print code line Paper in the Pigeon Hole will allow to note the string Length and an Initial start memory Location. This Location well have to change frequently as strings of different length are assigned. Instructiions will tell how to do this. Theoretically a specilal value vbNullString is set to aid in quich checks.. But..http://www.mrexcel.com/forum/excel-questions/361246-vbnullstring-2.html#post4411
  4.  Set Ws1 = ThisWorkbook.Worksheets.Item(1)  '_- Set: Fill or partially Fill: Setting to a Class will involve the use of an extra New at this code line. I will then have an Object referred to as an instance of a Class. At this point I include information on my Pointer Pigeon hole for a distinct distinguishable usage of an Object of the Class. For the case of something such as a Workbook this instancing has already been done, and in addition some values are filled in specific memory locations which are also held as part of the information in the Pigeon Hole Pointer. We will have a different Pointer for each instance. In most excel versions we already have a few instances of Worksheets. Such instances Objects can be further used., - For this a Dim to the class will be necessary, but the New must be omitted at Set. I can assign as many variables that I wish to the same existing instance
  5. Dim Clm As Long    '                                  ' Long is very simple to handle, - final memory "size" type is known (123.456 and 000.001 have same "size" computer memory ) , and so a Address suggestion can be given for the next line when the variable is filled in.  '( Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster. )
  6.  Let Clm = 1 ' CHANGE FOR DIFFERENT COLUMN
  7. ' Fill Place Holder for the last row as a whole number and msgbox its value out
  8. Dim Lr As Long: Let Lr = Ws1.Cells.Item(Ws1.Rows.Count, Clm).End(xlUp).Row ' ' The Range Object ( cell ) that is the last cell in the column of interest (CHOOSE a column typically that will always have a last Entry in any Data) ,( Row Number given by .Count Property applied to ( any Worksheet would do, so leaving unqualified is OK here, ) Spreadsheet Range Columns Property)    has the Property .End ( argument "Looking back up" ) appled to it. This Returns a new Range ( cell ) object which is that of the first Range ( cell ) with something in it "looking back up" in the XL spreadsheet from that last Cell. Then the .Row Property is applied to return a long number equal to the Column number of that cell:     Rows.Count is the very last row number in your sheet. It is different for earlier versions of Excel.  The End(xlUp) is the same as pressing a Ctrl+UpArrow key combination. The final ".Row" returns the row where the cursor stops after moving up.
  9.  MsgBox Prompt:="Last used row in column " & Clm & " is " & Lr & ""
  10. End Sub
  11.  
  12. '    The _ .Cells _ property returns all the cells as a Range object of the object to which it is applied. In this case it returns all the cells in a worksheet ( here the first worksheet ) , as a VBA Range object.
  13. '    In simple terms, in this case, the Worksheet can then be considered as a single massive rectangular Area of cells, with top left referred to typically as cell A1
  14. '
  15. '    The VBA Range object, is probably the most important thing for you to read up on and / or revise.
  16. '    Any Range object organises its cells into item numbers counting from left to right and then next row down. ( In that screen shot shown in yellow I have coincidentally shown a part ( 3 columns of 9 total columns and 9 total rows ) of another range object. That range object shown in Yellow starts at B2)
  17. '
  18. '    .Cells _ applied to a Worksheet returns the range object of the entire cells starting at A1.
  19. '    We can refer to the Items of any range object in a few ways, - Simplified in these three ways:_
  20. '    _ (Item number counting left to right then down) __ So A1 would be (1) in our case, and B2 for the range object shown in yellow
  21. '    _ (row number, column number ) __ with (1, 1) being top left of the entire spreadsheet Area returned by .Cells as in our case. ( For that range shown in yellow B2 is item (1, 1) )
  22. '    _ (Row number, column letter ) __ Top left for that returned by .Cells applied to a worksheet will be (1, “A”) as in our case. ( For that range shown in yellow B2 is item (1, “A”)
  23. '
  24. '    In that code line we use (row number , column number)
  25. '    We are referring to, pseudo code
  26. '    AllWorksheetCellsRangeObjectItem(1048576, 1) __1 for XL 2007+
  27. '    or
  28. '    AllWorksheetCellsRangeObjectItem(65536, 1) __1 for XL 2007+
  29. '
  30. '    Those big numbers come form _ Ws1.Rows.Count
  31. '    .Rows Property of a worksheet returns a thingy ( object ) containing stuff ( Methods, Properties ) about all rows of the thing it is applied to ( The worksheet in our case). One of its Property thingys is .Count which gives you the total number of rows. So Rows.Count will give you 1048576 for XL2007+ and 65536 for XL 2003
  32. '
  33. '    So _ Ws1.Cells.Item(Ws1.Rows.Count, 1) _ is returning us the cell ( as a range object: a range object can be one or more cells ) of the last cell in column A in terms of (row number , column number)
  34. '
  35. '    .End _ is a Property of a range object ( sometimes regarded as a Method by some people ) which is a sort of VBA equivalent of Hitting the _ Ctrl Key + an Arrow Key __ It is sometime considered as a Method as it takes a parameter argument in the ( ) . The parameter argument _ xlUp _ is equivalent to Hitting the Up Arrow Key. So this command “takes us to” or “returns” the range object which is the next cell it finds with something in it, having started from the last cell in the worksheet.
  36. '
  37. '    Once we have the range object of the last cell down the column with something in it, then we can use the .Row Property of a range object to return the row of the top left cell of that range object Area, ( which for the case of a single cell will be its only row )
  38. '
  39. '
  40. '
  41. '    http://www.excelfox.com/forum/showthread.php/2138-Understanding-VBA-Range-Object-Properties-and-referring-to-ranges-and-spreadsheet-cells
  42. '    https://www.excelforum.com/development-testing-forum/1154829-collection-stuff-of-codes-for-other-threads-no-reply-needed-11.html#post4551080
  43. '    https://powerspreadsheets.com/excel-vba-range-object/
  44. '    http://www.excelforum.com/tips-and-tutorials/1172578-understanding-vba-range-object-properties-and-referring-to-ranges-and-spreadsheet-cells.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement