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
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>
Re: How to select last row in excel sheet
vb Code:
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