PC Review


Reply
Thread Tools Rate Thread

Cell not selecting

 
 
Gotroots
Guest
Posts: n/a
 
      17th Dec 2009
The last blank cell containing the value of "B9" does not rng.Select. Much
appreciate if someone could advise me as to why.

If IsEmpty(Range("b9")) Then
MsgBox "No record found in B9.", vbInformation
Else
lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1

If lngLastRow <= 10 Then
Range("B10").Value = Range("B9").Value
Else
Cells(lngLastRow, "B").Value = Range("B9").Value
If rng Is Nothing Then
Else
rng.Select
End If
End If
End If

End Sub

 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      17th Dec 2009
Hi,

Rng isn't selecting because it is never set to anything so it will fail.
What do you want Rng to be?

Mike

"Gotroots" wrote:

> The last blank cell containing the value of "B9" does not rng.Select. Much
> appreciate if someone could advise me as to why.
>
> If IsEmpty(Range("b9")) Then
> MsgBox "No record found in B9.", vbInformation
> Else
> lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
>
> If lngLastRow <= 10 Then
> Range("B10").Value = Range("B9").Value
> Else
> Cells(lngLastRow, "B").Value = Range("B9").Value
> If rng Is Nothing Then
> Else
> rng.Select
> End If
> End If
> End If
>
> End Sub
>

 
Reply With Quote
 
Ryan H
Guest
Posts: n/a
 
      17th Dec 2009
Your problem is you haven't assigned anything to rng so rng will always be
Nothing until you do assign it something. Do you have some code missing from
your post? Are you wanting to select the last cell, if not, which cell are
you wanting to select? Try the code below.

Note: I would recommend you put Option Explicit at the top of your module
which will force declaration of all variables and ensure you have not spelled
anything incorrectly.


Option Explicit

Sub RangeSelect()

Dim lngLastRow As Long
Dim rng As Range

If IsEmpty(Range("B9")) Then
MsgBox "No record found in B9.", vbInformation
Else
lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1

If lngLastRow <= 10 Then
Range("B10").Value = Range("B9").Value
Else
Cells(lngLastRow, "B").Value = Range("B9").Value
If Not rng Is Nothing Then
rng.Select
End If
End If
End If

End Sub
--
Cheers,
Ryan


"Gotroots" wrote:

> The last blank cell containing the value of "B9" does not rng.Select. Much
> appreciate if someone could advise me as to why.
>
> If IsEmpty(Range("b9")) Then
> MsgBox "No record found in B9.", vbInformation
> Else
> lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
>
> If lngLastRow <= 10 Then
> Range("B10").Value = Range("B9").Value
> Else
> Cells(lngLastRow, "B").Value = Range("B9").Value
> If rng Is Nothing Then
> Else
> rng.Select
> End If
> End If
> End If
>
> End Sub
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      17th Dec 2009
You should really stay with one thread... it would have been better if you
posted this question back in your original thread where you got some of this
code from.

--
Rick (MVP - Excel)


"Gotroots" <(E-Mail Removed)> wrote in message
news:813BD982-DF18-4F4B-B516-(E-Mail Removed)...
> The last blank cell containing the value of "B9" does not rng.Select. Much
> appreciate if someone could advise me as to why.
>
> If IsEmpty(Range("b9")) Then
> MsgBox "No record found in B9.", vbInformation
> Else
> lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
>
> If lngLastRow <= 10 Then
> Range("B10").Value = Range("B9").Value
> Else
> Cells(lngLastRow, "B").Value = Range("B9").Value
> If rng Is Nothing Then
> Else
> rng.Select
> End If
> End If
> End If
>
> End Sub
>


 
Reply With Quote
 
Gotroots
Guest
Posts: n/a
 
      17th Dec 2009
Hello,

I want rng to be the cell the value of “B9” is pasted into.

For example:
“Test Data” is in “B9” and when the code is run “Test Data” is inserted into
“B23” the first blank cell in “B10:B”

When you say “it is never set to anything” how should this be done.

Gotroots

"Mike H" wrote:

> Hi,
>
> Rng isn't selecting because it is never set to anything so it will fail.
> What do you want Rng to be?
>
> Mike
>
> "Gotroots" wrote:
>
> > The last blank cell containing the value of "B9" does not rng.Select. Much
> > appreciate if someone could advise me as to why.
> >
> > If IsEmpty(Range("b9")) Then
> > MsgBox "No record found in B9.", vbInformation
> > Else
> > lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
> >
> > If lngLastRow <= 10 Then
> > Range("B10").Value = Range("B9").Value
> > Else
> > Cells(lngLastRow, "B").Value = Range("B9").Value
> > If rng Is Nothing Then
> > Else
> > rng.Select
> > End If
> > End If
> > End If
> >
> > End Sub
> >

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      17th Dec 2009
"rng" is not a pre-defined entity in Excel's VBA (the way Selection or
ActiveCell is, for example), so VB assumes it is a variable. Variables need
to be assigned values. Since you are attempting to Select it, VB assumes it
to be a object and objects have a value of Nothing until they are set to
reference some actual object (such as a Range, Worksheet, etc.). You set an
object variable to a reference using the Set keyword, such as like this...

Set rng = Range("B9")

However, for what I see your code doing, I think you can bypass using a
"rng" variable and just select the range you just found...

Cells(lngLastRow, "B").Select

Put this line right after the line in your originally posted code where you
assign the contents of B9 to it.

--
Rick (MVP - Excel)


"Gotroots" <(E-Mail Removed)> wrote in message
news:8BFA1154-7F77-4D6F-A811-(E-Mail Removed)...
> Hello,
>
> I want rng to be the cell the value of “B9” is pasted into.
>
> For example:
> “Test Data” is in “B9” and when the code is run “Test Data” is inserted
> into
> “B23” the first blank cell in “B10:B”
>
> When you say “it is never set to anything” how should this be done.
>
> Gotroots
>
> "Mike H" wrote:
>
>> Hi,
>>
>> Rng isn't selecting because it is never set to anything so it will fail.
>> What do you want Rng to be?
>>
>> Mike
>>
>> "Gotroots" wrote:
>>
>> > The last blank cell containing the value of "B9" does not rng.Select.
>> > Much
>> > appreciate if someone could advise me as to why.
>> >
>> > If IsEmpty(Range("b9")) Then
>> > MsgBox "No record found in B9.", vbInformation
>> > Else
>> > lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
>> >
>> > If lngLastRow <= 10 Then
>> > Range("B10").Value = Range("B9").Value
>> > Else
>> > Cells(lngLastRow, "B").Value = Range("B9").Value
>> > If rng Is Nothing Then
>> > Else
>> > rng.Select
>> > End If
>> > End If
>> > End If
>> >
>> > End Sub
>> >


 
Reply With Quote
 
Gotroots
Guest
Posts: n/a
 
      17th Dec 2009
I ran your code and the last cell did not select.
On my last post I gave an example of what I hoped the code should do, that
is "B23" would be selected after the code had run.



"Ryan H" wrote:

> Your problem is you haven't assigned anything to rng so rng will always be
> Nothing until you do assign it something. Do you have some code missing from
> your post? Are you wanting to select the last cell, if not, which cell are
> you wanting to select? Try the code below.
>
> Note: I would recommend you put Option Explicit at the top of your module
> which will force declaration of all variables and ensure you have not spelled
> anything incorrectly.
>
>
> Option Explicit
>
> Sub RangeSelect()
>
> Dim lngLastRow As Long
> Dim rng As Range
>
> If IsEmpty(Range("B9")) Then
> MsgBox "No record found in B9.", vbInformation
> Else
> lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
>
> If lngLastRow <= 10 Then
> Range("B10").Value = Range("B9").Value
> Else
> Cells(lngLastRow, "B").Value = Range("B9").Value
> If Not rng Is Nothing Then
> rng.Select
> End If
> End If
> End If
>
> End Sub
> --
> Cheers,
> Ryan
>
>
> "Gotroots" wrote:
>
> > The last blank cell containing the value of "B9" does not rng.Select. Much
> > appreciate if someone could advise me as to why.
> >
> > If IsEmpty(Range("b9")) Then
> > MsgBox "No record found in B9.", vbInformation
> > Else
> > lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
> >
> > If lngLastRow <= 10 Then
> > Range("B10").Value = Range("B9").Value
> > Else
> > Cells(lngLastRow, "B").Value = Range("B9").Value
> > If rng Is Nothing Then
> > Else
> > rng.Select
> > End If
> > End If
> > End If
> >
> > End Sub
> >

 
Reply With Quote
 
gotroots
Guest
Posts: n/a
 
      17th Dec 2009
Hi

Thank you for the clarity, I have to rush off so will test your suggestion
out tomorrow. Many thanks.

"Rick Rothstein" wrote:

> "rng" is not a pre-defined entity in Excel's VBA (the way Selection or
> ActiveCell is, for example), so VB assumes it is a variable. Variables need
> to be assigned values. Since you are attempting to Select it, VB assumes it
> to be a object and objects have a value of Nothing until they are set to
> reference some actual object (such as a Range, Worksheet, etc.). You set an
> object variable to a reference using the Set keyword, such as like this...
>
> Set rng = Range("B9")
>
> However, for what I see your code doing, I think you can bypass using a
> "rng" variable and just select the range you just found...
>
> Cells(lngLastRow, "B").Select
>
> Put this line right after the line in your originally posted code where you
> assign the contents of B9 to it.
>
> --
> Rick (MVP - Excel)
>
>
> "Gotroots" <(E-Mail Removed)> wrote in message
> news:8BFA1154-7F77-4D6F-A811-(E-Mail Removed)...
> > Hello,
> >
> > I want rng to be the cell the value of “B9” is pasted into.
> >
> > For example:
> > “Test Data” is in “B9” and when the code is run “Test Data” is inserted
> > into
> > “B23” the first blank cell in “B10:B”
> >
> > When you say “it is never set to anything” how should this be done.
> >
> > Gotroots
> >
> > "Mike H" wrote:
> >
> >> Hi,
> >>
> >> Rng isn't selecting because it is never set to anything so it will fail.
> >> What do you want Rng to be?
> >>
> >> Mike
> >>
> >> "Gotroots" wrote:
> >>
> >> > The last blank cell containing the value of "B9" does not rng.Select.
> >> > Much
> >> > appreciate if someone could advise me as to why.
> >> >
> >> > If IsEmpty(Range("b9")) Then
> >> > MsgBox "No record found in B9.", vbInformation
> >> > Else
> >> > lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
> >> >
> >> > If lngLastRow <= 10 Then
> >> > Range("B10").Value = Range("B9").Value
> >> > Else
> >> > Cells(lngLastRow, "B").Value = Range("B9").Value
> >> > If rng Is Nothing Then
> >> > Else
> >> > rng.Select
> >> > End If
> >> > End If
> >> > End If
> >> >
> >> > End Sub
> >> >

>
> .
>

 
Reply With Quote
 
Gotroots
Guest
Posts: n/a
 
      18th Dec 2009
The last blank cell is now selecting only the cell remains without a value.
It should contain the value taken from "B9"

here is the code as it looks now:

If IsEmpty(Range("b9")) Then
MsgBox "No record found in B9.", vbInformation
Else
lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1

If lngLastRow <= 10 Then
Range("B10").Value = Range("B9").Value
Else
Cells(lngLastRow, "B").Select
If Not rng Is Nothing Then
rng.Select
End If
End If
End If

Gotroots


"Rick Rothstein" wrote:

> You should really stay with one thread... it would have been better if you
> posted this question back in your original thread where you got some of this
> code from.
>
> --
> Rick (MVP - Excel)
>
>
> "Gotroots" <(E-Mail Removed)> wrote in message
> news:813BD982-DF18-4F4B-B516-(E-Mail Removed)...
> > The last blank cell containing the value of "B9" does not rng.Select. Much
> > appreciate if someone could advise me as to why.
> >
> > If IsEmpty(Range("b9")) Then
> > MsgBox "No record found in B9.", vbInformation
> > Else
> > lngLastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
> >
> > If lngLastRow <= 10 Then
> > Range("B10").Value = Range("B9").Value
> > Else
> > Cells(lngLastRow, "B").Value = Range("B9").Value
> > If rng Is Nothing Then
> > Else
> > rng.Select
> > End If
> > End If
> > End If
> >
> > End Sub
> >

>
> .
>

 
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
Paste data of a cell to another cell by just selecting the source lamesakid5810 Microsoft Excel Programming 2 1st Apr 2009 08:35 AM
Excel 2007 single cell selecting muliple cell Submit2s Microsoft Excel Worksheet Functions 1 12th Feb 2009 04:52 PM
Selecting a cell entry based on cell validation selection Brutalius Microsoft Excel Worksheet Functions 2 17th Dec 2008 03:44 AM
Transfer cell values to another cell by selecting button. Gryndar Microsoft Excel Worksheet Functions 2 24th Nov 2008 02:21 AM
Re: Finding text in a cell, then selecting cells below until a blank cell is found. Don Guillett Microsoft Excel Misc 2 8th Jul 2003 11:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:52 AM.