PC Review


Reply
Thread Tools Rate Thread

Define a Range with Assembled String

 
 
Gandalph
Guest
Posts: n/a
 
      8th Oct 2008
Greetings from Norfolk (UK)

I am trying to generalise a specific piece of code I 'threw' together.
The first verison causes an error (as noted below) and the second runs OK.
On looking with Debug the content of "FirstCell" is the same , i.e. A1

Running Excel 2000 under Win XP

The following are the two versions of the set up and call in the main
procedure, followed by the relevant piece of code from the called procedure

SearchCol = "A"
Call DateRng(SearchCol)

*****************************

Sub DateRng(SearchCol As String)

Dim FirstCell As String * 4

FirstCell = SearchCol & GetRealFirstRow()
Range(FirstCell).Select
etc.
Generates
Run Time Error 1004
Method 'Range ' of object '_Worksheet' failed
******************************************
The following works OK

Call DateRng()

****************************

Sub DateRng()

Dim FirstCell As String * 4

FirstCell = "A" & GetRealFirstRow()
Range(FirstCell).Select

Your comments and advice would be appreciated.


 
Reply With Quote
 
 
 
 
Barb Reinhardt
Guest
Posts: n/a
 
      8th Oct 2008
I'd change

FirstCell = SearchCol & GetRealFirstRow()
Range(FirstCell).Select

to
Cells(GetRealFirstRow(),SearchCol).Select

If I were you, I'd declare the worksheet that you are acting on. I presume
it's the active worksheet . I'd do it this way

Dim aWS as Worksheet
Set aWS = ActiveSheet

and for the first one, I'd change
Cells(GetRealFirstRow(),SearchCol).Select

to
aWS.Cells(GetRealFirstRow(),SearchCol).Select


If you do it this way, you don't need to select the cells, but can act
directly on them. Selecting cells takes longer to execute than just acting
on the cells directly.
--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.



"Gandalph" wrote:

> Greetings from Norfolk (UK)
>
> I am trying to generalise a specific piece of code I 'threw' together.
> The first verison causes an error (as noted below) and the second runs OK.
> On looking with Debug the content of "FirstCell" is the same , i.e. A1
>
> Running Excel 2000 under Win XP
>
> The following are the two versions of the set up and call in the main
> procedure, followed by the relevant piece of code from the called procedure
>
> SearchCol = "A"
> Call DateRng(SearchCol)
>
> *****************************
>
> Sub DateRng(SearchCol As String)
>
> Dim FirstCell As String * 4
>
> FirstCell = SearchCol & GetRealFirstRow()
> Range(FirstCell).Select
> etc.
> Generates
> Run Time Error 1004
> Method 'Range ' of object '_Worksheet' failed
> ******************************************
> The following works OK
>
> Call DateRng()
>
> ****************************
>
> Sub DateRng()
>
> Dim FirstCell As String * 4
>
> FirstCell = "A" & GetRealFirstRow()
> Range(FirstCell).Select
>
> Your comments and advice would be appreciated.
>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      8th Oct 2008
I'd check the value in SearchCol and what's returned from the GetRealFirstRow
function.

Maybe you don't have a real address.

If that's not the problem, I'd make sure I selected the correct worksheet first
and then selected the range.

Worksheets("somesheetnamehere").select
Worksheets("somesheetnamehere").Range(FirstCell).Select


Gandalph wrote:
>
> Greetings from Norfolk (UK)
>
> I am trying to generalise a specific piece of code I 'threw' together.
> The first verison causes an error (as noted below) and the second runs OK.
> On looking with Debug the content of "FirstCell" is the same , i.e. A1
>
> Running Excel 2000 under Win XP
>
> The following are the two versions of the set up and call in the main
> procedure, followed by the relevant piece of code from the called procedure
>
> SearchCol = "A"
> Call DateRng(SearchCol)
>
> *****************************
>
> Sub DateRng(SearchCol As String)
>
> Dim FirstCell As String * 4
>
> FirstCell = SearchCol & GetRealFirstRow()
> Range(FirstCell).Select
> etc.
> Generates
> Run Time Error 1004
> Method 'Range ' of object '_Worksheet' failed
> ******************************************
> The following works OK
>
> Call DateRng()
>
> ****************************
>
> Sub DateRng()
>
> Dim FirstCell As String * 4
>
> FirstCell = "A" & GetRealFirstRow()
> Range(FirstCell).Select
>
> Your comments and advice would be appreciated.


--

Dave Peterson
 
Reply With Quote
 
Gandalph
Guest
Posts: n/a
 
      8th Oct 2008


"Barb Reinhardt" wrote:

> I'd change
>
> FirstCell = SearchCol & GetRealFirstRow()
> Range(FirstCell).Select
>
> to
> Cells(GetRealFirstRow(),SearchCol).Select
>
> If I were you, I'd declare the worksheet that you are acting on. I presume
> it's the active worksheet . I'd do it this way
>
> Dim aWS as Worksheet
> Set aWS = ActiveSheet
>
> and for the first one, I'd change
> Cells(GetRealFirstRow(),SearchCol).Select
>
> to
> aWS.Cells(GetRealFirstRow(),SearchCol).Select
>
>
> If you do it this way, you don't need to select the cells, but can act
> directly on them. Selecting cells takes longer to execute than just acting
> on the cells directly.
> --
> HTH,
> Barb Reinhardt
>
> If this post was helpful to you, please click YES below.
>
>
>
> "Gandalph" wrote:
>
> > Greetings from Norfolk (UK)
> >
> > I am trying to generalise a specific piece of code I 'threw' together.
> > The first verison causes an error (as noted below) and the second runs OK.
> > On looking with Debug the content of "FirstCell" is the same , i.e. A1
> >
> > Running Excel 2000 under Win XP
> >
> > The following are the two versions of the set up and call in the main
> > procedure, followed by the relevant piece of code from the called procedure
> >
> > SearchCol = "A"
> > Call DateRng(SearchCol)
> >
> > *****************************
> >
> > Sub DateRng(SearchCol As String)
> >
> > Dim FirstCell As String * 4
> >
> > FirstCell = SearchCol & GetRealFirstRow()
> > Range(FirstCell).Select
> > etc.
> > Generates
> > Run Time Error 1004
> > Method 'Range ' of object '_Worksheet' failed
> > ******************************************
> > The following works OK
> >
> > Call DateRng()
> >
> > ****************************
> >
> > Sub DateRng()
> >
> > Dim FirstCell As String * 4
> >
> > FirstCell = "A" & GetRealFirstRow()
> > Range(FirstCell).Select
> >
> > Your comments and advice would be appreciated.
> >
> >

Thank you for your suggestions, I will look at them in detail, but, it does
not explain why the first version
(FirstCell = SearchCol & GetRealFirstRow()
Range(FirstCell).Select
fails, and the second version
FirstCell = "A" & GetRealFirstRow()
Range(FirstCell).Select
Works OK.
Is it that strings 'assembled' by the programme are not acceptable to
Range(*).Select

I am not being difficult, but wicsh to know so that I can use this step in
future.


 
Reply With Quote
 
Gandalph
Guest
Posts: n/a
 
      8th Oct 2008
Thank you for your reply, but you appear to have missed the principal point
of the question.
The first listed code runs upto the statement
Range(FirstCell).Select
On actioning this statement the error is generated.

The second code fragment runs correctly (to me) and generates no error, and
continues through the sub routine to the end.

The code 'changes' were made using the comment (') to select either one or
the other, no other changes being made.

I wsh to know WHY for future reference etc.


"Dave Peterson" wrote:

> I'd check the value in SearchCol and what's returned from the GetRealFirstRow
> function.
>
> Maybe you don't have a real address.
>
> If that's not the problem, I'd make sure I selected the correct worksheet first
> and then selected the range.
>
> Worksheets("somesheetnamehere").select
> Worksheets("somesheetnamehere").Range(FirstCell).Select
>
>
> Gandalph wrote:
> >
> > Greetings from Norfolk (UK)
> >
> > I am trying to generalise a specific piece of code I 'threw' together.
> > The first verison causes an error (as noted below) and the second runs OK.
> > On looking with Debug the content of "FirstCell" is the same , i.e. A1
> >
> > Running Excel 2000 under Win XP
> >
> > The following are the two versions of the set up and call in the main
> > procedure, followed by the relevant piece of code from the called procedure
> >
> > SearchCol = "A"
> > Call DateRng(SearchCol)
> >
> > *****************************
> >
> > Sub DateRng(SearchCol As String)
> >
> > Dim FirstCell As String * 4
> >
> > FirstCell = SearchCol & GetRealFirstRow()
> > Range(FirstCell).Select
> > etc.
> > Generates
> > Run Time Error 1004
> > Method 'Range ' of object '_Worksheet' failed
> > ******************************************
> > The following works OK
> >
> > Call DateRng()
> >
> > ****************************
> >
> > Sub DateRng()
> >
> > Dim FirstCell As String * 4
> >
> > FirstCell = "A" & GetRealFirstRow()
> > Range(FirstCell).Select
> >
> > Your comments and advice would be appreciated.

>
> --
>
> Dave Peterson
>

 
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
define a range that equals the first row of another range Richard Microsoft Excel Programming 5 1st Dec 2010 05:53 AM
Define a Range based on a Range object Terry Microsoft Excel Programming 4 25th Oct 2010 08:44 PM
Define String shapper Microsoft C# .NET 4 4th Sep 2008 01:13 AM
i can't define a string - help Jack Microsoft VC .NET 3 11th Dec 2006 12:00 AM
Define a range based on another named range =?Utf-8?B?QmFzaWw=?= Microsoft Excel Worksheet Functions 2 21st Feb 2005 01:47 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:59 PM.