Results 1 to 3 of 3

Thread: How to select last row in excel sheet

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    How to select last row in excel sheet

    Hai ,

    I want to select the last row in the excel sheet for enetering the next data.
    kindly help me


    Code:
    Private Sub sadet_Click()
    Dim AppXls As Excel.Application
    Dim mastexl As Excel.Application
    Dim ObjWb, maswb As Excel.Workbook
    Dim objws1, masws As Excel.Worksheet
    Set mastex1 = New Excel.Application
    Dim i, a As Integer
    Set maswb = mastex1.Workbooks.Open(App.Path & "\master.xlsx")
     Set masws = maswb.Worksheets("sheet1")
     i = 1
    While masws.Cells(i, 1) = ""
    i = i + 1
    Wend
    a = i + 1
    With masws
     .Cells(a, 1) = a
     End With
     maswb.Close (SaveChanges = True)
     mastex1.Quit
    
    Set AppXls = CreateObject("Excel.Application")
    Set ObjWb = AppXls.Workbooks.Add
    Dim ass As String
    
    ass = "" & a + 1 & ""
    
     Set objws1 = ObjWb.Worksheets.Add
        objws1.Name = "Registration details"
        objws1.Cells(1, 1) = "hello"
        Set objws1 = Nothing
        ObjWb.SaveAs (App.Path & "\" & ass & "")
      ObjWb.Close (SaveChanges = True)
      AppXls.Quit
    
    End Sub
    i am getting always 3rd row in the sheet and also nothing is getting updated there

    regards

    praveen

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to select last row in excel sheet

    This finds the first empty row (actually the first blank cell in column 'A') and updates it.

    Code:
       '~~> Starting Row
        i = 2
        '~~> Find the first empty Row
        Do Until Len(Trim(Range("A" & i))) = 0
            i = i + 1
        Loop
        '~~> Add new value in Col A
        Range("A" & i).Value = <new value>

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to select last row in excel sheet

    vb Code:
    1. nextrow = masws.cells(masws.rows.count, 1).end(xlup).row + 1
    change to correct worksheet and you may need to declare the xl constant xlup = -4162

    also you do not need to create 2 instances of excel (mastex1 and AppXls) as you can open multiple workbooks in the same instance
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width