MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2004, 04:54 PM   #1
march madness
 
Join Date: Mar 2004
Posts: 34
Default accessing values in cells

If I want to access the values in cells when the cells are in different worksheets, how do I do that? i.e., there are 10 worksheets, 10 cells and 10 values in those cells. I want to create an array of those values. How do I do it? Note: I will be creating a new array of the same cells' values often, and the values will be changing. Each time I create the array, the array with the new values will overwrite the old array, and it's old values, so I'd like to automate this as much as possible.

For the sake of the example, let's say the values are in cell A2 in each worksheet, and each worksheet has it's default name, i.e. "Sheet1."

Any suggestions?
march madness is offline   Reply With Quote
Old Apr 12th, 2004, 07:25 AM   #2
HalfAce
MrExcel MVP
 
Join Date: Apr 2003
Location: Alaska
Posts: 7,332
Default Re: accessing values in cells

Not sure if you still need it or not, but assuming you want to show the values of A2 from all 10 sheets in A1 of sheet 1....
Code:
Sub Test()
[A1] = Sheets("Sheet1").[A2] & ", " & Sheets("Sheet2").[A2] & ", " & Sheets("Sheet3").[A2] & ", " & Sheets("Sheet4").[A2] & ", " & Sheets("Sheet5").[A2] & ", " _
& Sheets("Sheet6").[A2] & ", " & Sheets("Sheet7").[A2] & ", " & Sheets("Sheet8").[A2] & ", " & Sheets("Sheet9").[A2] & ", " & Sheets("Sheet10").[A2]
End Sub
Is this what you are (were?) looking for?
Dan
__________________
XP & '03
Vista & '07
HalfAce is offline   Reply With Quote
Old Apr 12th, 2004, 08:08 AM   #3
Tom Urtis
MrExcel MVP
 
Tom Urtis's Avatar
 
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
Default Re: accessing values in cells

Something like this would update and place those 10 values in range B1:B10 of the active sheet.

Sub SheetArray()
Dim i As Integer
For i = 1 To 10
Cells(i, 2).Value = Sheets("Sheet" & i).Range("A2").Value
Next i
End Sub
__________________
Tom Urtis
Tom Urtis is offline   Reply With Quote
Old Apr 13th, 2004, 01:18 AM   #4
march madness
 
Join Date: Mar 2004
Posts: 34
Default Re: accessing values in cells

Quote:
Originally Posted by Tom Urtis
Something like this would update and place those 10 values in range B1:B10 of the active sheet.

Sub SheetArray()
Dim i As Integer
For i = 1 To 10
Cells(i, 2).Value = Sheets("Sheet" & i).Range("A2").Value
Next i
End Sub
That's what I asked for, thanks.

Is there any chance you know how I can associate those values with the name of the sheet the values originate from? Say the values are 10,20,30,40,50,60,70,80,90,100, and the sheet names are: Sheet1,Sheet2,Sheet3,Sheet4,Sheet5,Sheet6,Sheet7,Sheet8,Sheet9,Sheet10, respectively.
What I'd like to do is have in column B the names of the sheets, and in column C the associative values. How might I do that?
march madness is offline   Reply With Quote
Old Apr 13th, 2004, 01:26 AM   #5
DRJ
MrExcel MVP
 
DRJ's Avatar
 
Join Date: Feb 2002
Location: California
Posts: 3,857
Default

Like this?

Sub SheetArray()
Dim i As Integer
For i = 1 To 10
Cells(i, 2).Value = Sheets("Sheet" & i).Range("A2").Value
Cells(i, 3).Value = "Sheet" & Sheets("Sheet" & i).Range("A2").Value / 10
Next i
End Sub
__________________
Excel VBA Training and Certification (Lesson 1 is free)
<hr>

<hr>-Jacob
DRJ is offline   Reply With Quote
Old Apr 13th, 2004, 01:34 AM   #6
Tom Urtis
MrExcel MVP
 
Tom Urtis's Avatar
 
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
Default Re: accessing values in cells

Quote:
Originally Posted by march madness
Is there any chance you know how I can associate those values with the name of the sheet the values originate from? What I'd like to do is have in column B the names of the sheets, and in column C the associative values.
The below macro would accomplish that for you:

Sub SheetArray_TakeTwo()
Dim i As Integer
For i = 1 To 10
With Sheets("Sheet" & i)
Cells(i, 2).Value = .Name
Cells(i, 3).Value = .Range("A2").Value
End With
Next i
End Sub
__________________
Tom Urtis
Tom Urtis is offline   Reply With Quote
Old Apr 13th, 2004, 01:53 AM   #7
march madness
 
Join Date: Mar 2004
Posts: 34
Default Re: accessing values in cells

Quote:
Originally Posted by Tom Urtis
Quote:
Originally Posted by march madness
Is there any chance you know how I can associate those values with the name of the sheet the values originate from? What I'd like to do is have in column B the names of the sheets, and in column C the associative values.
The below macro would accomplish that for you:

Sub SheetArray_TakeTwo()
Dim i As Integer
For i = 1 To 10
With Sheets("Sheet" & i)
Cells(i, 2).Value = .Name
Cells(i, 3).Value = .Range("A2").Value
End With
Next i
End Sub
Thanks much.
Two last things, if you don't mind:
1)How might I include in that same macro an ascending sort for both columns, so that if Sheet5's value is 40 and Sheet4's value is 50, Sheet4 will come first?
2)Make this macro work in another worksheet, say the sheet called, Results?

Thanks much.
march madness is offline   Reply With Quote
Old Apr 13th, 2004, 02:29 AM   #8
Tom Urtis
MrExcel MVP
 
Tom Urtis's Avatar
 
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
Default Re: accessing values in cells

Sub SheetArray_TakeThree()
Dim i As Integer, z As Worksheet
Set z = Worksheets("Results")
For i = 1 To 10
With Sheets("Sheet" & i)
z.Cells(i, 2).Value = .Name
z.Cells(i, 3).Value = .Range("A2").Value
End With
Next i
z.Range("B1:C10").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
End Sub
__________________
Tom Urtis
Tom Urtis is offline   Reply With Quote
Old Apr 13th, 2004, 05:01 AM   #9
march madness
 
Join Date: Mar 2004
Posts: 34
Default Re: accessing values in cells

Quote:
Originally Posted by Tom Urtis
Sub SheetArray_TakeThree()
Dim i As Integer, z As Worksheet
Set z = Worksheets("Results")
For i = 1 To 10
With Sheets("Sheet" & i)
z.Cells(i, 2).Value = .Name
z.Cells(i, 3).Value = .Range("A2").Value
End With
Next i
z.Range("B1:C10").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
End Sub
That's what I'm talking about! Thanks.
march madness is offline   Reply With Quote
Old Apr 13th, 2004, 05:09 AM   #10
march madness
 
Join Date: Mar 2004
Posts: 34
Default

Quote:
Originally Posted by DRJ
Like this?

Sub SheetArray()
Dim i As Integer
For i = 1 To 10
Cells(i, 2).Value = Sheets("Sheet" & i).Range("A2").Value
Cells(i, 3).Value = "Sheet" & Sheets("Sheet" & i).Range("A2").Value / 10
Next i
End Sub
Not that I was trying to ignore your help, this one just reported a syntax error, and Tom came back with one that worked. Thanks for the input though. BTW, what's the syntax error here?
march madness is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On

Forum Jump


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


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.