PC Review


Reply
Thread Tools Rate Thread

Better way to select Cell in Range

 
 
Hal
Guest
Posts: n/a
 
      13th Nov 2009
I have the range K2:V2. I want to start at K2 and check if the cell is empty.
If so select it, if not, check L2, M2 . . . V2 until the first empty cell in
this range is found, and then select it. The code below will work but I would
like to have something more professional.

Signed, Novice at work


If Range("K2").Value = "" Then
Range("K2").Select
Elseif Range("L2").Value = "" Then
Range("L2").Select
 
Reply With Quote
 
 
 
 
Barb Reinhardt
Guest
Posts: n/a
 
      13th Nov 2009
Dim myRange as Excel.Range
Dim r as Excel.Range

set myRange = Range("K2:V2")

for each r in myRange
if r.value = "" then
r.select
end if
next r

That's how you do it, but selection really slows down execution.
--
HTH,

Barb Reinhardt



"Hal" wrote:

> I have the range K2:V2. I want to start at K2 and check if the cell is empty.
> If so select it, if not, check L2, M2 . . . V2 until the first empty cell in
> this range is found, and then select it. The code below will work but I would
> like to have something more professional.
>
> Signed, Novice at work
>
>
> If Range("K2").Value = "" Then
> Range("K2").Select
> Elseif Range("L2").Value = "" Then
> Range("L2").Select
> .
> .
> .
> Else
> Range("V2").Select

 
Reply With Quote
 
Gary''s Student
Guest
Posts: n/a
 
      13th Nov 2009
Sub Macro1()
Range("K2").End(xlToRight).Offset(0, 1).Select
End Sub

--
Gary''s Student - gsnu200908
 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      13th Nov 2009
Hi,

Try this but note I didn't trap for no empty cells in the range which would
throw an error

Dim rng As Range
Set rng = Range("K2:V2")
rng.Cells.SpecialCells(xlCellTypeBlanks).Cells(1).Select

Mike

"Hal" wrote:

> I have the range K2:V2. I want to start at K2 and check if the cell is empty.
> If so select it, if not, check L2, M2 . . . V2 until the first empty cell in
> this range is found, and then select it. The code below will work but I would
> like to have something more professional.
>
> Signed, Novice at work
>
>
> If Range("K2").Value = "" Then
> Range("K2").Select
> Elseif Range("L2").Value = "" Then
> Range("L2").Select
> .
> .
> .
> Else
> Range("V2").Select

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      13th Nov 2009
Maybe this?

Sub SelectBlank()
On Error Resume Next
Range("K2:V2").SpecialCells(xlCellTypeBlanks)(1).Select
End Sub

--
Rick (MVP - Excel)


"Hal" <(E-Mail Removed)> wrote in message
news:5A3F8D8C-7A9A-4DCC-812C-(E-Mail Removed)...
>I have the range K2:V2. I want to start at K2 and check if the cell is
>empty.
> If so select it, if not, check L2, M2 . . . V2 until the first empty cell
> in
> this range is found, and then select it. The code below will work but I
> would
> like to have something more professional.
>
> Signed, Novice at work
>
>
> If Range("K2").Value = "" Then
> Range("K2").Select
> Elseif Range("L2").Value = "" Then
> Range("L2").Select
> .
> .
> .
> Else
> Range("V2").Select


 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      13th Nov 2009
Includes a trap for no empty cells

Dim rng As Range
On Error GoTo Getmeout
Set rng = Range("K2:V2")
rng.Cells.SpecialCells(xlCellTypeBlanks).Cells(1).Select
Exit Sub
Getmeout:
MsgBox "No empty cells in range"

Mike

"Mike H" wrote:

> Hi,
>
> Try this but note I didn't trap for no empty cells in the range which would
> throw an error
>
> Dim rng As Range
> Set rng = Range("K2:V2")
> rng.Cells.SpecialCells(xlCellTypeBlanks).Cells(1).Select
>
> Mike
>
> "Hal" wrote:
>
> > I have the range K2:V2. I want to start at K2 and check if the cell is empty.
> > If so select it, if not, check L2, M2 . . . V2 until the first empty cell in
> > this range is found, and then select it. The code below will work but I would
> > like to have something more professional.
> >
> > Signed, Novice at work
> >
> >
> > If Range("K2").Value = "" Then
> > Range("K2").Select
> > Elseif Range("L2").Value = "" Then
> > Range("L2").Select
> > .
> > .
> > .
> > Else
> > Range("V2").Select

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      13th Nov 2009
GS,

I would avoid this method because it fails if all cells are empty and
selects a cell outside the range if all cells are populated.

Mike

"Gary''s Student" wrote:

> Sub Macro1()
> Range("K2").End(xlToRight).Offset(0, 1).Select
> End Sub
>
> --
> Gary''s Student - gsnu200908

 
Reply With Quote
 
muddan madhu
Guest
Posts: n/a
 
      13th Nov 2009
Sub test2()

Range("K2").Select
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1).Select
Loop

End Sub


On Nov 14, 12:43*am, Hal <H...@discussions.microsoft.com> wrote:
> I have the range K2:V2. I want to start at K2 and check if the cell is empty.
> If so select it, if not, check L2, M2 . . . V2 until the first empty cellin
> this range is found, and then select it. The code below will work but I would
> like to have something more professional.
>
> Signed, Novice at work
>
> * * If Range("K2").Value = "" Then
> * * *Range("K2").Select
> * * Elseif Range("L2").Value = "" Then
> * * *Range("L2").Select
> * * .
> * * .
> * * .
> * * Else
> * * *Range("V2").Select


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      13th Nov 2009
Here is a patch for Gary''s Student's method...

Sub Macro1()
On Error Resume Next
Intersect(Range("K2").End(xlToRight).Offset(0, 1), Columns("K:V")).Select
End Sub

--
Rick (MVP - Excel)


"Mike H" <(E-Mail Removed)> wrote in message
news:88F22084-CAE4-4CC7-B01E-(E-Mail Removed)...
> GS,
>
> I would avoid this method because it fails if all cells are empty and
> selects a cell outside the range if all cells are populated.
>
> Mike
>
> "Gary''s Student" wrote:
>
>> Sub Macro1()
>> Range("K2").End(xlToRight).Offset(0, 1).Select
>> End Sub
>>
>> --
>> Gary''s Student - gsnu200908


 
Reply With Quote
 
John_John
Guest
Posts: n/a
 
      13th Nov 2009
Hi all!

On Error Resume Next
Range("K2:V2").SpecialCells(4)(1).Select
'or
Range("K2:V2").Find("").Select
If Err Then MsgBox "No cells were found.", vbExclamation

Ο χρήστης "Mike H" *γγραψε:

> Includes a trap for no empty cells
>
> Dim rng As Range
> On Error GoTo Getmeout
> Set rng = Range("K2:V2")
> rng.Cells.SpecialCells(xlCellTypeBlanks).Cells(1).Select
> Exit Sub
> Getmeout:
> MsgBox "No empty cells in range"
>
> Mike
>
> "Mike H" wrote:
>
> > Hi,
> >
> > Try this but note I didn't trap for no empty cells in the range which would
> > throw an error
> >
> > Dim rng As Range
> > Set rng = Range("K2:V2")
> > rng.Cells.SpecialCells(xlCellTypeBlanks).Cells(1).Select
> >
> > Mike
> >
> > "Hal" wrote:
> >
> > > I have the range K2:V2. I want to start at K2 and check if the cell is empty.
> > > If so select it, if not, check L2, M2 . . . V2 until the first empty cell in
> > > this range is found, and then select it. The code below will work but I would
> > > like to have something more professional.
> > >
> > > Signed, Novice at work
> > >
> > >
> > > If Range("K2").Value = "" Then
> > > Range("K2").Select
> > > Elseif Range("L2").Value = "" Then
> > > Range("L2").Select
> > > .
> > > .
> > > .
> > > Else
> > > Range("V2").Select

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot select a cell or range REM Microsoft Excel Discussion 3 11th Nov 2008 05:16 AM
macro to select range from active cell range name string aelbob Microsoft Excel Programming 2 14th Jul 2008 09:19 PM
HELP W/ VBA: SELECT RANGE, ALLCAPS, CELL COLOR, RETURN TO BLANK CELL/PATTERN CELL extremejobtvshow@gmail.com Microsoft Excel Programming 5 28th Jun 2008 07:49 PM
NEED VBA TO SELECT A CELL; NOTE THE CELL VALUE;COPYADJ CELL;FIND CELL VALUE IN A RANGE AND SO ON CAPTGNVR Microsoft Excel Programming 2 8th Jul 2007 04:18 PM
How do I select multiple selections using the range(cell(),Cell()) =?Utf-8?B?aGF2b2NkcmFnb24=?= Microsoft Excel Programming 1 8th Nov 2006 05:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:20 AM.