Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Jasmine Grant   on Aug 26 In MS Office Category.

  
Question Answered By: Verner Fischer   on Aug 26

LastCell doesn't always update itself properly. You could try adding

Worksheets("EnterCowData").UsedRange

just before the LastCell line, to make Excel recalculate what the last cell
is. If that doesn't work, here is a handy function which will find the last cell
with data on a worksheet:

Function FindLastCell(Wksht As Worksheet) As String
'Returns address of last cell used (highest row  & col) on specified sheet
Dim LastRow As Long
Dim LastCol As Integer
On Error GoTo FLCerr1
With Wksht
LastRow = 0
LastCol = 0
LastRow& = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
LastCol% = _
.Cells.Find("*", after:=.Cells(1), _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
End With
FindLastCell$ = Cells(LastRow&, LastCol%).AddressLocal
Exit Function
FLCerr1:
FindLastCell$ = "ERROR"
End Function

You would call it like this:

CmyLastRow = FindLastCell(Sheets("EnterCowData"))

Please note, FindLastCell returns ERROR if the sheet is empty.

Share: 

 
 
Didn't find what you were looking for? Find more on combo box, deleting a row Or get search suggestion and latest updates.


Tagged: