![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Join Date: Mar 2004
Posts: 34
|
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? |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Apr 2003
Location: Alaska
Posts: 7,332
|
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
Dan
__________________
XP & '03 Vista & '07 |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
|
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 |
|
|
|
|
|
#4 | |
|
Join Date: Mar 2004
Posts: 34
|
Quote:
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? |
|
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: California
Posts: 3,857
|
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 |
|
|
|
|
|
#6 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
|
Quote:
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 |
|
|
|
|
|
|
#7 | ||
|
Join Date: Mar 2004
Posts: 34
|
Quote:
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. |
||
|
|
|
|
|
#8 |
|
MrExcel MVP
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
|
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 |
|
|
|
|
|
#9 | |
|
Join Date: Mar 2004
Posts: 34
|
Quote:
|
|
|
|
|
|
|
#10 | |
|
Join Date: Mar 2004
Posts: 34
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|