Combine row and columns as variables to cell address

mikemowry

New Member
Joined
Dec 28, 2016
Messages
8
Hi, brand new to the forum. Not a newbie, but somewhat experienced with simpler code.

Here's my problem. I want to use the last cell with data as part of the range in an ActiveSheet.Sort.SortFields.Add Key:=Range( ??? statement.

I got this far and stored row and column to variables, but can't get a cell address to place in the sort statement.

Dim lastRow As Long
Dim lastColumn As Integer
Dim lastCell As Range

If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Rows.
lastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row

'Search for any entry, by searching backwards by Columns.
lastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

MsgBox Cells(lastRow, lastColumn).Address

Set lastCell = Cells(lastColumn, lastRow)

The message box displays $U$711905 and lastRow is set to 711905 and lastColumn is set to 21, but lastCell = Nothing

Thanks for any help you can offer.
 
So, just as I thought, the code was doing what is supposed to, it just wasn't the results you were expecting.

I don't understand why when I Ctrl End I go to $V$711908.
I have seen that happen when ranges may not be populated, but have been formatted down that far.
Or data was deleted using Clear Contents instead of deleting rows.


Edit: Just saw your last reply. Yes, saving often resets that to work properly. Glad you got it working now.
 
Last edited:
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Sorry if I wasn't clear, but it's still not returning the cell address. The only results I get are lastCell - Nothing or lastCell = 0.

Still trying to figure out how to return the cell address so I can use it in a range statement.
 
Upvote 0
I added this code at the end

Range("A2:" & lastColumn & lastRow).Select

I get a Runtime 1004 error.

Method 'Range' of object '_Global' failed
 
Upvote 0
Sorry if I wasn't clear, but it's still not returning the cell address. The only results I get are lastCell - Nothing or lastCell = 0.
Look at my post #8 again.

"lastCell" will NOT return an address, since it is a range variable and not a string. It It returns the value that is in the lastCell.
If you want the address, you would need to use "lastCell.Address".

I added this code at the end

Range("A2:" & lastColumn & lastRow).Select

I get a Runtime 1004 error.

Method 'Range' of object '_Global' failed
It would be:
Code:
Range(Cells(2, "A"), Cells(lastRow, lastColumn)).Select
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

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