Last used row in a column containing a cell.

sts023

Board Regular
Joined
Sep 1, 2008
Messages
106
Hi again....

I've a horrible feeling I'm getting way too complex in a solution to a problem, and was wondering if I'm missing something blindingly obvious.

On a given Worksheet, I have a cell address (e.g. "B24", with no dollar signs)
I need a way to find the last used row in the column in which the target cell is.

Here's the code (which works) but I'm sure there's a better way.
Any ideas?
Code:
Public Sub SampleCode()
Dim lngLastRow                  As Long
Dim lngStartRow                 As Long
Dim strCol                      As String
Dim strStartCell                As String
Dim strWks                      As String
Dim wks                         As Excel.Worksheet
'*
'** Get addressability to the Worksheet and Cell.
'*
  strWks = "Sheet1"
  strStartCell = "B24"
  Set wks = Worksheets(strWks)
  wks.Select
  wks.Range(strStartCell).Select
'*
'** Because I only know how to get the last
'** row in a column using
'** e.g. Range("Z12345").End(xlUp), I
'** need to change the column NUMBER to
'** a column LETTER.
'*
  strCol = funColAsLetter(Selection.Column)
'*
'** Get ROW of start CELL.
'
  lngStartRow = Selection.Row
'*
'** Get ROW of last used CELL in the
'** column containing the target CELL.
'*
  lngLastRow = Range(strCol & Rows.Count).End(xlUp).Row
End Sub      'funGetDateInCol


Public Function funColAsLetter(intCol As Integer) As String
Dim intA1                       As Integer
Dim intA2                       As Integer
Dim intRemainder                As Integer
  Select Case intCol
    Case 0
      funColAsLetter = ""
    Case 1 To 26
      funColAsLetter = Chr(intCol + 64)
    Case Else
      intA1 = Int(intCol / 27)
      intA2 = intCol - (intA1 * 26)
      funColAsLetter = Chr(intA1 + 64) & _
                         Chr(intA2 + 64)
  End Select
End Function 'funColAsLetter

I
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Given you know this:
lngLastRow = Range(strCol & Rows.Count).End(xlUp).Row

you can use
Answer = Cells(lngLastRow,2).address(0,0)
and if lngLastRow is 34, then Answer is "B34" (no quotes).
 
Upvote 0
You don't need to convert column Number to Letter.
You can use the column Number just as well..

The Cells property does pretty much everything the Range property does.
But you can use column NUMBER in Cells
Cells(Row#, ColumnNumber OR Letter)

So we can do
Cells(5,7) : this is the same as Range("G5")
Cells(5,"G") : this is also the same as Range("G5")


So something like this.

strStartCell = "B24"
Set MyRange = Range(strStartCell)
MyCol = MyRange.Column
LastRow = Cells(Rows.Count, MyCol).End(xlup).Row
 
Upvote 0
Thanks Jonmo1 (and all others)
I don't know if its heartrending or heartwarming to see an elegant three line solution to something I've spent a whole day trying to figure out!
Oh well, onwards and upwards.....
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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