Select range on a worksheet

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

Hi all.

I do a lot of downloads with CSV files that I pull into Excel and then
have to format, so I wrote a macro that does 99% of the formatting I
need to do, but I'm having the darndest time getting a macro to select
the whole range that has values in it starting at Cell A1 and going to
the last cell... which might be F500, Q1250, or whatever, depending on
the worksheet. All I want to do is select the whole range and then
put borders on the cells.

Any help?
 
Give this a try...

Sub SelectRange() 'Run me
Range("A1", LastCell()).Select
End Sub

Public Function LastCell(Optional ByVal wks As Worksheet) As Range
Dim lngLastRow As Long
Dim intLastColumn As Integer

If wks Is Nothing Then Set wks = ActiveSheet
On Error Resume Next
lngLastRow = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
intLastColumn = wks.Cells.Find(What:="*", _
After:=wks.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
If lngLastRow = 0 Then
lngLastRow = 1
intLastColumn = 1
End If
Set LastCell = wks.Cells(lngLastRow, intLastColumn)

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top