+ Reply to Thread
Results 1 to 21 of 21

Get the last used of Row

Hybrid View

ccs1981 Get the last used of Row 06-18-2013, 08:34 AM
Mordred Re: Get the last used of Row 06-18-2013, 08:49 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 09:07 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 09:09 AM
Mordred Re: Get the last used of Row 06-18-2013, 08:54 AM
Mordred Re: Get the last used of Row 06-18-2013, 09:14 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 09:18 AM
Mordred Re: Get the last used of Row 06-18-2013, 09:20 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 09:22 AM
Mordred Re: Get the last used of Row 06-18-2013, 09:26 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 09:37 AM
Mordred Re: Get the last used of Row 06-18-2013, 09:44 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 09:57 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 10:00 AM
Mordred Re: Get the last used of Row 06-18-2013, 10:03 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 10:04 AM
Mordred Re: Get the last used of Row 06-18-2013, 10:07 AM
ccs1981 Re: Get the last used of Row 06-18-2013, 10:11 AM
patel45 Re: Get the last used of Row 06-18-2013, 10:26 AM
HaHoBe Re: Get the last used of Row 06-18-2013, 11:57 AM
ccs1981 Re: Get the last used of Row 06-19-2013, 03:53 AM
  1. #1
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Get the last used of Row

    hi all,

    good days.


    how to get the last used of row for the range of data (A:W).
    if i had draw border to row 300.


    'LastRow = Cells(Rows.Count, 1).End(xlUp).Row (this 1 is only can count one column only)
    ''LastRow = ActiveSheet.UsedRange.Rows.Count (this 1 get the lastrow = 300 because i draw the border even there is blank)


    i want to count the last used of row for the range of data which exclude the border i draw to 300

    hope can get help from your all.

    thank you~

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    Perhaps the following will do?
    Sub FindCompleteRange()
    
        Dim ws2 As Worksheet
        Dim uRng As Range
        Dim lRow As Long, lCol As Long
        
        Set ws2 = Worksheets("Sheet2")
        lRow = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
        lCol = ws2.Cells(lRow, ws2.Columns.Count).End(xlToLeft).Column
        Set uRng = ws2.Range(ws2.Cells(1, 1), ws2.Cells(lRow, lCol))
        Debug.Print uRng.Address
        
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    thank you,

    but still cannot get the correct last used row.

  4. #4
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    if i only fill in some of the cell until row 25, then it show the last row as 25.
    even i had draw the border to row 300

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    Sorry, I may have mis-understood this.
    i want to count the last used of row for the range of data which exclude the border i draw to 300
    So if Row 300 hundred has a border and there's a row 301 without a border, you want the variable lRow to be 301?

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    The code I provided you will set the range that is used. If you just want the last row number then
    lRow = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
    Did you change the
    Set ws2 = Worksheets("Sheet2")
    to reference the sheet in your workbook that you want to reference? For instance if you want to reference sheet 1 then
    Dim ws1 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    The code I gave you was referencing sheet 2.

  7. #7
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    yes, i change to sheet 1..
    the code you give me cannot count the correct last used row.

    i only want to know the last used row.

  8. #8
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    Perhaps you should upload a mock workbook.
    the code you give me cannot count the correct last used row.
    It can and does but obviously there are issues in your workbook that don't agree with the code.

  9. #9
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    i am using office 2010. it just a normal workbook.
    please help~

    thank you.
    Last edited by ccs1981; 06-18-2013 at 09:26 AM.

  10. #10
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  11. #11
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    
    
     
        Dim ws2 As Worksheet
        Dim LastRow As Long
    
        
        Set ws2 = Worksheets("Sheet1")
        LastRow = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row
      
    
    If WorksheetFunction.CountBlank(Range("A2:B" & LastRow)) >= 1 Or WorksheetFunction.CountBlank(Range("D2:D" & LastRow)) >= 1 Or WorksheetFunction.CountBlank(Range("F2:F" & LastRow)) >= 1 Then
            MsgBox "Save disabled because at least one required field is left blank in BOARDTYPE, BOARD, CHASSIS, PIC, STATUS"
        Cancel = True
    End If
    
    
    
    End Sub
    actually i want the last used row to check some of the required column.
    Last edited by ccs1981; 06-18-2013 at 09:55 AM.

  12. #12
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    I don't understand why you are not providing a workbook. I don't particularly like guessing all day when it comes to people's requirements. First you want rows now you want columns. Please, upload a mock workbook with a before and after scenario. If you do not, I will not bother any more.

  13. #13
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    sorry, i typo error.
    i want the last used row instead of last used column.

  14. #14
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    i upload here my workbook.


    My coding at ThisWorkbook.
    Last edited by ccs1981; 06-19-2013 at 03:54 AM.

  15. #15
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    Thank you. Which sheet do you want this to run on?

  16. #16
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    the script is running when i want to close the workbook to ensure some of the column is required key in.
    it does not allow blank when try to save and close the workbook.

  17. #17
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Get the last used of Row

    So, sheet 1, where there is only 1 row used? LastRow is working perfectly in that scenario.

  18. #18
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    yes, you can simply insert any data into the cell.

  19. #19
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Get the last used of Row

    Sub LastRowInRange()
    LR = ActiveSheet.UsedRange.Rows.Count
    Set rng = Range("A1:W" & LR)
    Lastcol = rng.Columns.Count
    LastRow = 1
    For c = 1 To Lastcol
      If Cells(Rows.Count, c).End(xlUp).Row > LastRow Then LastRow = Cells(Rows.Count, c).End(xlUp).Row
    Next
    MsgBox LastRow
    
    End Sub
    If solved remember to mark Thread as solved

  20. #20
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Get the last used of Row

    Hi, patel45,

    "great" stuff: LR gets the highest row number by default (starting from row 1 as stated), and your loop is obsolete.

    @ccs1981:
    if your range starts with row 2 while there is only the heading available you will alwas get a notification. Maybe change your code to read
    If WorksheetFunction.CountBlank(Range("A1:B" & LastRow)) >= 1 Or WorksheetFunction.CountBlank(Range("D1:D" & LastRow)) >= 1 Or WorksheetFunction.CountBlank(Range("F1:F" & LastRow)) >= 1 Then
            MsgBox "Save disabled because at least one required field is left blank in BOARDTYPE, BOARD, CHASSIS, PIC, STATUS"
        Cancel = True
    End If
    Ciao,
    Holger
    Last edited by HaHoBe; 06-18-2013 at 12:05 PM. Reason: change of code added
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  21. #21
    Registered User
    Join Date
    09-16-2008
    Location
    malaysia
    Posts
    89

    Re: Get the last used of Row

    thank you so much.

    it solved. Really appreciate it. ^^

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1