Why do I get different results of Lastcell?

Oberon70

Board Regular
Joined
Jan 21, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the below code, which for some reason gives me the result of $A$1 instead of $AK$28, which is the last cell for the current statement I am testing with.

VBA Code:
Sub CopyTbleToTmpSht()

Dim LastCell As String
Dim oarray As Variant


With wsData
    LastCell = wsData.Range(FindLast(3)).Address
End With

oarray = wsData.Range("A1:" & LastCell)

wsTmpData.Range("A1:" & LastCell) = oarray

Debug.Print LastCell

'wsData.Range("A1:" & LastCell).Select


End Sub

It will work if I change the code to the below, but was wondering why I have to activate the sheet for it to work? Is there a way when I call the function it is pointing to the worksheet I want?

VBA Code:
Sub CopyTbleToTmpSht()

Dim LastCell As String
Dim oarray As Variant


With wsData
    .Activate
    LastCell = wsData.Range(FindLast(3)).Address
End With

oarray = wsData.Range("A1:" & LastCell)

wsTmpData.Range("A1:" & LastCell) = oarray

Debug.Print LastCell

'wsData.Range("A1:" & LastCell).Select


End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
FindLast is not a VBA function. You must have code for that somewhere, and the answer to your question is probably in that code. Can you post it here? I am guessing it looks at ActiveSheet for the cell it wants. I am sorry to say that is an undesirable coding technique, for the very reason you are experiencing.

By the way, the original version of your code does not take advantage of the With statement.
VBA Code:
With wsData
    LastCell = wsData.Range(FindLast(3)).Address
End With
I would not recommend a With here because there is only one reference to wsData, but if you want to do it you would do this:
VBA Code:
With wsData
    LastCell = .Range(FindLast(3)).Address
End With
 
Upvote 0
thanks I will remove the with, below is the Function Findlast, which was provided to me.

VBA Code:
Enum XLFindLast
    xlFindLastRow = 1
    xlFindLastColumn
    xlFindlastCell
End Enum

Function FindLast(lRowColCell As Long, _
                    Optional sSheet As String, _
                    Optional sRange As String)
'Find the last row, column, or cell using the Range.Find method
'lRowColCell: 1=Row, 2=Col, 3=Cell

Dim lRow As Long
Dim lCol As Long
Dim wsFind As Worksheet
Dim rFind As Range

    'Default to ActiveSheet if none specified
    On Error GoTo ErrExit
    
    If sSheet = "" Then
        Set wsFind = ActiveSheet
    Else
        Set wsFind = Worksheets(sSheet)
    End If

    'Default to all cells if range no specified
    If sRange = "" Then
        Set rFind = wsFind.Cells
    Else
        Set rFind = wsFind.Range(sRange)
    End If
    
    On Error GoTo 0

    Select Case lRowColCell
    
        Case 1 'Find last row
            On Error Resume Next
            FindLast = rFind.Find(What:="*", _
                            After:=rFind.Cells(1), _
                            LookAt:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
            On Error GoTo 0

        Case 2 'Find last column
            On Error Resume Next
            FindLast = rFind.Find(What:="*", _
                            After:=rFind.Cells(1), _
                            LookAt:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
            On Error GoTo 0

        Case 3 'Find last cell by finding last row & col
            On Error Resume Next
            lRow = rFind.Find(What:="*", _
                           After:=rFind.Cells(1), _
                           LookAt:=xlPart, _
                           LookIn:=xlFormulas, _
                           SearchOrder:=xlByRows, _
                           SearchDirection:=xlPrevious, _
                           MatchCase:=False).Row
            On Error GoTo 0

            On Error Resume Next
            lCol = rFind.Find(What:="*", _
                            After:=rFind.Cells(1), _
                            LookAt:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
            On Error GoTo 0

            On Error Resume Next
            FindLast = wsFind.Cells(lRow, lCol).Address(False, False)
            'If lRow or lCol = 0 then entire sheet is blank, return "A1"
            If Err.Number > 0 Then
                FindLast = rFind.Cells(1).Address(False, False)
                Err.Clear
            End If
            On Error GoTo 0

    End Select
    
    Exit Function
    
ErrExit:

    MsgBox "Error setting the worksheet or range."

End Function
 
Upvote 0
As I suspected, if you do not provide a sheet name, LastCell assumes you want to reference ActiveSheet. Change your line of code as follows:

Rich (BB code):
    LastCell = wsData.Range(FindLast(3, wsData.Name)).Address
 
Upvote 0
Perhaps you could provide what wsData equals.

That is a lot of code just get a result.
 
Upvote 0
Are you just looking for the last used cell in the sheet?
 
Upvote 0
The following is a test you can run which I think will give you the result you are looking for with much shorter code:
VBA Code:
Sub Test()
'
    Dim ColValue    As Range
    Dim RowValue    As Range
    Dim wsData      As Worksheet
'
    Set wsData = Sheets("Sheet1")                                         ' <--- set this to the proper sheet name
    Set ColValue = wsData.Cells.Find("*", , xlValues, , xlByColumns, xlPrevious)
    Set RowValue = wsData.Cells.Find("*", , xlValues, , xlByRows, xlPrevious)
'
Debug.Print "ColValue.Address = " & ColValue.Address
Debug.Print "RowValue.Address = " & RowValue.Address
'
MsgBox "ColValue.Address = " & ColValue.Address & vbCrLf & "RowValue.Address = " & RowValue.Address
End Sub

One of those two results should give you the result you are looking for, I am guessing it is the second result that you want.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top