0

I would like to create a VBA subroutine that searches a table for the first column header named "Phonetic Name". Then finds the absolute last cell in the table, on the bottom right corner, and store a variable as the cell coordinate one row above the last cell. The subroutine would then select all cells between the first cell "Phonetic Name" and the "LastCell" variable.

Dim LastCol As Integer

TL = ActiveSheet.Range("A:A").Find("Phonetic Name", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True).Row

Set LastRow = Cells.Find("*", [a1], , , xlByRows, xlPrevious)

With ActiveSheet
    LastCol = .Cells(TL, .Columns.Count).End(xlToLeft).Column
End With

Set LastCell = ActiveSheet.Cells(LastRow.Row - 1, LastCol)
'I would like to do something like the following... 
ActiveSheet.Range("TL:LastCell").Select
Selection.Copy

How can I re-write this logic in a way that is VBA friendly?

2
  • What's wrong with that code ? Commented Aug 24, 2012 at 0:21
  • 1
    I think ActiveSheet.Range(TL,LastCell).Copy is what you're looking for (but remove the .Row from the end of the .Find() line) Commented Aug 24, 2012 at 0:48

1 Answer 1

2
Dim LastCol As Integer  
Dim TL as Range

Set TL = ActiveSheet.Range("A:A").Find("Phonetic Name", _
                 LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)  

If Not TL Is Nothing Then

    Set LastRow = Cells.Find("*", [a1], , , xlByRows, xlPrevious)  
    With ActiveSheet     
        LastCol = .Cells(TL.Row, .Columns.Count).End(xlToLeft).Column 
    End With  
    Set LastCell = ActiveSheet.Cells(LastRow.Row - 1, LastCol) 
    ActiveSheet.Range(TL,LastCell).Copy

End If
Sign up to request clarification or add additional context in comments.

1 Comment

Change TL to TL.Row, i.e., LastCol = .Cells(TL, Columns.Count).End(xlToLeft).Column to LastCol = .Cells(TL.Row, Columns.Count).End(xlToLeft).Column Also, consider expanding your With statement to include all ActiveSheet references, and declaring all your variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.