Link to home
Start Free TrialLog in
Avatar of SameerMirza
SameerMirzaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Excel vba code understand

Hi,
I am trying to understand the code below,
iRow = ws.Cells(Rows.Count, 1). end(xlUp).Offset(1,0).Row

does it mean somthing like
worksheet.Cells(total rows, first cell).Offset(next line, same cell).Row

Q1. why we use .Row at the end
Q2. How would we find last empty cell - col


Kind regards,

Avatar of SameerMirza
SameerMirza
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

sorry If I sound to annoying but I just want to understand it once very clearly because lines like these are used quiet often
ASKER CERTIFIED SOLUTION
Avatar of Saqib Husain
Saqib Husain
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Orite.. - now I know and I can simple write the same statement for last occupied cell if I have to - JUST PERFACT

I have another question though,
I am following an example - please have a look at the code below
Its basically to enter values values in excel sheet - database - using a command button

Know I know that we can use 'Exit Function' to exit function
But I am trying to implement the error handling here but not sure
For example it throws the 'alert' when 'partid' is not given but would still write the row - which it shouldn't

It would great if you get me started on it or just quickly edit the code to be able to handle the 'nulls' etc

Thanks

Private Sub cmdAdd_Click()

Dim iRom As Long
Dim ws As Worksheet
Set ws = Worksheets("PartsData")
'find first empty row in the database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

'check for a part number
If Trim(Me.txtPart.Value) = "" Then
Me.txtPart.SetFocus
MsgBox "Please enter part number"
Exit()
End If

'assign the values
ws.Cells(iRow, 1).Value = Me.txtPart.Value
ws.Cells(iRow, 2).Value = Me.txtLocation
ws.Cells(iRow, 3).Value = Me.txtDate
ws.Cells(iRow, 4).Value = Me.txtQuantity

'clear the data
Me.txtPart.Value = ""
Me.txtLocation = ""
Me.txtDate = ""
Me.txtQuantity = ""



End Sub

Open in new window

how do we convert string to number?
Change line 15 from  "End if" to "Else"

and add "End if" on line 22


To convert string to number the conventional function is
a = val(string)

but quite often this is very conveniently done by

a = --string

or

a = string * 0
thanks