PC Review


Reply
Thread Tools Rate Thread

Assigning Cell data to VB Variables

 
 
mike@infinitesign.com
Guest
Posts: n/a
 
      28th Mar 2007
REPOSTED FOR CLARIFICATION ( MS Office Excel 2003 )
temp = Sheets("Sheet1").Cells(Y, X).Value2
temp = Cells(Y, X).Value2
temp = Sheets("Sheet1").Range(Cells(Y, X), Cells(Y, X)).Value2

All 3 statements produce a 1004 runtime error. Use "Sheets" or
"Worksheets" makes no difference either.

Except for temp all variables are non-zero integers, and temp is
not declared (for now) so that Excel VB can determine the type as
needed.

None of the "Cells" referred to are "CurCell": not Active nor Selected
( that I know of )
I simply want to "point" to them i.e. to read them.
I believe even the Offset Method (?) changes the selected cell.

I don't want to employ the standard Range("A1") object since the A1 is
a literal string,
and I need more programatic control hence the Cells method.

Thanks, Mike
PS I've seen online doc with the same statements indicated that they
should work.

 
Reply With Quote
 
 
 
 
Trevor Shuttleworth
Guest
Posts: n/a
 
      28th Mar 2007
works for me:

Sub test()
x = 1
y = 1
Cells(y, x) = 30
temp = Sheets("Sheet1").Cells(y, x).Value2
temp = Cells(y, x).Value2
temp = Sheets("Sheet1").Range(Cells(y, x), Cells(y, x)).Value2
End Sub

What are the values of x and y ? What value do you have in the cell
(Cells(y,x)) ? Why are you using Value2 rather than just Value or
defaulting ?

Regards

Trevor


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> REPOSTED FOR CLARIFICATION ( MS Office Excel 2003 )
> temp = Sheets("Sheet1").Cells(Y, X).Value2
> temp = Cells(Y, X).Value2
> temp = Sheets("Sheet1").Range(Cells(Y, X), Cells(Y, X)).Value2
>
> All 3 statements produce a 1004 runtime error. Use "Sheets" or
> "Worksheets" makes no difference either.
>
> Except for temp all variables are non-zero integers, and temp is
> not declared (for now) so that Excel VB can determine the type as
> needed.
>
> None of the "Cells" referred to are "CurCell": not Active nor Selected
> ( that I know of )
> I simply want to "point" to them i.e. to read them.
> I believe even the Offset Method (?) changes the selected cell.
>
> I don't want to employ the standard Range("A1") object since the A1 is
> a literal string,
> and I need more programatic control hence the Cells method.
>
> Thanks, Mike
> PS I've seen online doc with the same statements indicated that they
> should work.
>



 
Reply With Quote
 
mike@infinitesign.com
Guest
Posts: n/a
 
      28th Mar 2007
Trevor,

Thanks - that's good news. All x,y data should be non-zero integer
( For ...Next etc.). The Cell data is also integer.
I used Value2 out of desperation as it does not support dates etc, and
I was only dealing with integers.

I'll try your code, and I'll probably find I have a problem elsewhere.
( believe it or not this is my first time on a ...dare I use the old
term ....bulletin board )

On Mar 28, 4:53 pm, "Trevor Shuttleworth" <Tre...@Shucks.demon.co.uk>
wrote:
> works for me:
>
> Sub test()
> x = 1
> y = 1
> Cells(y, x) = 30
> temp = Sheets("Sheet1").Cells(y, x).Value2
> temp = Cells(y, x).Value2
> temp = Sheets("Sheet1").Range(Cells(y, x), Cells(y, x)).Value2
> End Sub
>
> What are the values of x and y ? What value do you have in the cell
> (Cells(y,x)) ? Why are you using Value2 rather than just Value or
> defaulting ?
>
> Regards
>
> Trevor
>
> <m...@infinitesign.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > REPOSTED FOR CLARIFICATION ( MS Office Excel 2003 )
> > temp = Sheets("Sheet1").Cells(Y, X).Value2
> > temp = Cells(Y, X).Value2
> > temp = Sheets("Sheet1").Range(Cells(Y, X), Cells(Y, X)).Value2

>
> > All 3 statements produce a 1004 runtime error. Use "Sheets" or
> > "Worksheets" makes no difference either.

>
> > Except for temp all variables are non-zero integers, and temp is
> > not declared (for now) so that Excel VB can determine the type as
> > needed.

>
> > None of the "Cells" referred to are "CurCell": not Active nor Selected
> > ( that I know of )
> > I simply want to "point" to them i.e. to read them.
> > I believe even the Offset Method (?) changes the selected cell.

>
> > I don't want to employ the standard Range("A1") object since the A1 is
> > a literal string,
> > and I need more programatic control hence the Cells method.

>
> > Thanks, Mike
> > PS I've seen online doc with the same statements indicated that they
> > should work.- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      29th Mar 2007
Mike,
Using "Sheet1", X and Y are valid, methods 1 & 2 will work.
Method 3 may work depending on the location of the code/Active sheet, as
Cells may not be on the "Sheet1".
But this will work, note the qualifying "." before Cells:
With Sheets("Sheet1")
temp = .Range(.Cells(Y, X), .Cells(Y, X)).Value2
End With

Maybe this will help:

'On Sheet 1
Private Sub CommandButton1_Click()

Worksheets(1).Cells(1, 1).Value = "Sheet 1"
Worksheets(2).Cells(1, 1).Value = "Sheet 2"

MsgBox Cells(1, 1).Value
MsgBox GetValue()

Worksheets(2).Select

MsgBox Cells(1, 1).Value
MsgBox GetValue()
MsgBox ActiveSheet.Cells(1, 1).Value

End Sub

'---------------------
'On a module
Public Function GetValue() As String
GetValue = Cells(1, 1).Value
End Function

Having said that it is seldom necessary to .Select object before using them.
Personally, I always fully qualify the objects, unless the aim is to always
work on the active sheet or sheet with the code.

NickHK

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> REPOSTED FOR CLARIFICATION ( MS Office Excel 2003 )
> temp = Sheets("Sheet1").Cells(Y, X).Value2
> temp = Cells(Y, X).Value2
> temp = Sheets("Sheet1").Range(Cells(Y, X), Cells(Y, X)).Value2
>
> All 3 statements produce a 1004 runtime error. Use "Sheets" or
> "Worksheets" makes no difference either.
>
> Except for temp all variables are non-zero integers, and temp is
> not declared (for now) so that Excel VB can determine the type as
> needed.
>
> None of the "Cells" referred to are "CurCell": not Active nor Selected
> ( that I know of )
> I simply want to "point" to them i.e. to read them.
> I believe even the Offset Method (?) changes the selected cell.
>
> I don't want to employ the standard Range("A1") object since the A1 is
> a literal string,
> and I need more programatic control hence the Cells method.
>
> Thanks, Mike
> PS I've seen online doc with the same statements indicated that they
> should work.
>



 
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
Assigning cell values to variables Tim Microsoft Excel Misc 3 11th Jun 2009 04:30 PM
Copy cell data and assigning to variables Fan924 Microsoft Excel Programming 2 14th Oct 2007 03:45 AM
MS Office Excel 2003 assigning cell values to variables mike@infinitesign.com Microsoft Excel Programming 1 28th Mar 2007 05:32 PM
Assigning cell address components to variables Conan Kelly Microsoft Excel Programming 1 15th Jun 2005 06:05 AM
assigning cell values to variables Ruune Microsoft Excel Worksheet Functions 1 3rd Dec 2003 02:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:58 PM.