Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA Highlighting/selecting an unknown range of cells

Hiya Everyone!

I maybe going about this in a very convoluted way.......

Im trying to highlight/select a variable range of cells....So far I
have this code which highlights one column depending on the length of
the column (data filled). but I need to select it in variable number
of rows

Sub SelectColumn()

Dim startCell As Range, cell1 As Range, cell2 As Range
Dim rowNr&, colNr&

Set startCell = ActiveCell
rowNr = startCell.Row: colNr = startCell.Column

If IsEmpty(startCell) Then Exit Sub

For rowNr = startCell.Column To 1 Step -1
If IsEmpty(Cells(rowNr, colNr).Value) Then
Set cell1 = Cells(rowNr + 1, colNr)
Exit For
End If
Next rowNr

If cell1 Is Nothing Then Set cell1 = Cells(1, colNr)

For rowNr = startCell.Row To 500
If IsEmpty(Cells(rowNr, colNr).Value) Then
Set cell2 = Cells(rowNr - 1, colNr)
Exit For
End If
Next rowNr
If cell2 Is Nothing Then Set cell2 = Cells(500, colNr)

Range(cell1, cell2).Select

End Sub

I think it is probably staring me in the face...or there is a really
quick way of doing this...Can anyone help??

Thanks!


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Excel VBA Highlighting/selecting an unknown range of cells

dani,

I think the sub below does the same thing, only quicker.

Depending on how your data is set up, a single line macro might work:

Sub SelectColumn3()
Intersect(ActiveCell.CurrentRegion, ActiveCell.EntireColumn).Select
End Sub

HTH,
Bernie
MS Excel MVP

Sub SelectColumn2()
Dim Cell1 As Range
Dim Cell2 As Range
If IsEmpty(ActiveCell) Then Exit Sub
Set Cell1 = ActiveCell
Set Cell2 = ActiveCell
If Not IsEmpty(ActiveCell(0)) Then Set Cell1 = ActiveCell.End(xlUp)
If Not IsEmpty(ActiveCell(2)) Then Set Cell2 = ActiveCell.End(xlDown)
Range(Cell1, Cell2).Select
End Sub


"daniB " wrote in message
...
Hiya Everyone!

I maybe going about this in a very convoluted way.......

Im trying to highlight/select a variable range of cells....So far I
have this code which highlights one column depending on the length

of
the column (data filled). but I need to select it in variable

number
of rows

Sub SelectColumn()

Dim startCell As Range, cell1 As Range, cell2 As Range
Dim rowNr&, colNr&

Set startCell = ActiveCell
rowNr = startCell.Row: colNr = startCell.Column

If IsEmpty(startCell) Then Exit Sub

For rowNr = startCell.Column To 1 Step -1
If IsEmpty(Cells(rowNr, colNr).Value) Then
Set cell1 = Cells(rowNr + 1, colNr)
Exit For
End If
Next rowNr

If cell1 Is Nothing Then Set cell1 = Cells(1, colNr)

For rowNr = startCell.Row To 500
If IsEmpty(Cells(rowNr, colNr).Value) Then
Set cell2 = Cells(rowNr - 1, colNr)
Exit For
End If
Next rowNr
If cell2 Is Nothing Then Set cell2 = Cells(500, colNr)

Range(cell1, cell2).Select

End Sub

I think it is probably staring me in the face...or there is a really
quick way of doing this...Can anyone help??

Thanks!


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA Highlighting/selecting an unknown range of cells

Thank you thats done it great

--
Message posted from http://www.ExcelForum.com

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
highlighting or selecting cells joec[_2_] Setting up and Configuration of Excel 1 August 19th 09 10:11 PM
Total cells when range is unknown FirstVette52 Excel Worksheet Functions 4 December 31st 08 06:41 PM
Selecting shapes with unknown names JBCI Excel Discussion (Misc queries) 0 March 10th 08 05:45 PM
Selecting/Highlighting active cells wnfisba Excel Discussion (Misc queries) 1 September 27th 06 04:24 PM
Range selecting cells Alec H Excel Discussion (Misc queries) 2 March 14th 06 01:36 PM


All times are GMT +1. The time now is 04:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"