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 Apr 13th, 2004, 05:18 AM   #1
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
Darnit, I thought it would still work with the names of the worksheets being arbitrary, like Bob, Joe, Leslie, and so on. Can you change it to accomodate names, instead of just numbers? And, can you exclude the sheet called "Winners"

Thanks for the continual assistance.
march madness is offline   Reply With Quote
Old Apr 13th, 2004, 06:11 AM   #2
DRJ
MrExcel MVP
 
DRJ's Avatar
 
Join Date: Feb 2002
Location: California
Posts: 3,857
Default

I'm not sure what the error is. The code compiles fine for me with no syntax errors.

To exclude the sheet Winners use

For i = 1 To 10
If Sheets("Sheet" & 1).Name <> "Winners" Then
With Sheets("Sheet" & i)
z.Cells(i, 2).Value = .Name
z.Cells(i, 3).Value = .Range("A2").Value
End With
Else
End If
Next i
__________________
Excel VBA Training and Certification (Lesson 1 is free)
<hr>

<hr>-Jacob
DRJ is offline   Reply With Quote
Old Apr 13th, 2004, 08:12 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

Quote:
Originally Posted by march madness
Darnit, I thought it would still work with the names of the worksheets being arbitrary, like Bob, Joe, Leslie, and so on. Can you change it to accomodate names, instead of just numbers? And, can you exclude the sheet called "Winners"
This will be my last post on this thread.

You've been asking these questions in small bits instead of just asking for what you want. This is getting messy because you are not specifying what should be done if "Winners" is among the first 10 sheets or not. The following two macros will cover either scenario there. But what if "Results" is among the first 10 sheets, do you want to include that too? And what if you don't have 11 sheets if "Winners" is among the 10 and you don't want to include it?

DRJ's code failed, although he said he could not see why, because:

(1) It relied on Sheet names, not indexes, the rules of which you changed after the fact.
(2) It placed the A2 value of each sheet in column B, not the sheet name like you asked.
(3) For some reason only he knows why, he coded his macro to place the word "Sheet" and then the A2 value divided by 10, in column C, instead of just the A2 value like you asked. That is why his code would show a bizarre sheet name - looking value like "Sheet1.4" in the wrong column if the number 14 were in cell A2 of one of those sheets.

You might also need to end up taking values off sheets based on their VBA object code names, instead of by Index number or sheet tab name.

At this point I do not know what you will end up needing, but here is a set of two macros that do what you want per your latest request, given two possible scenarios described above.

'First 10 sheets, by Index number, excluding "Winners"
Sub SheetArray_TakeFourA()
Dim i As Integer, z As Worksheet
Set z = Worksheets("Results")
For i = 1 To 10
With Sheets(i)
If .Name <> "Winners" Then
z.Cells(i, 2).Value = .Name
z.Cells(i, 3).Value = .Range("A2").Value
End If
End With
Next i
z.Range("B1:C10").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
End Sub

'First 10 sheets, by Index number, that are not "Winners"
Sub SheetArray_TakeFourB()
Dim i As Integer, z As Worksheet
Set z = Worksheets("Results")
z.Range("B1:C11").ClearContents
For i = 1 To 10
With Sheets(i)
If .Name <> "Winners" Then
z.Cells(i, 2).Value = .Name
z.Cells(i, 3).Value = .Range("A2").Value
End If
End With
Next i
z.Range("B1:C10").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
If Application.CountA(z.Range("B1:B10")) = 9 Then
With Sheets(11)
z.Cells(11, 2).Value = .Name
z.Cells(11, 3).Value = .Range("A2").Value
End With
z.Range("B1:C11").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
End If
End Sub
__________________
Tom Urtis
Tom Urtis is offline   Reply With Quote
Old Apr 13th, 2004, 01:23 PM   #4
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
Darnit, I thought it would still work with the names of the worksheets being arbitrary, like Bob, Joe, Leslie, and so on. Can you change it to accomodate names, instead of just numbers? And, can you exclude the sheet called "Winners"
This will be my last post on this thread.

You've been asking these questions in small bits instead of just asking for what you want. This is getting messy because you are not specifying what should be done if "Winners" is among the first 10 sheets or not. The following two macros will cover either scenario there. But what if "Results" is among the first 10 sheets, do you want to include that too? And what if you don't have 11 sheets if "Winners" is among the 10 and you don't want to include it?

DRJ's code failed, although he said he could not see why, because:

(1) It relied on Sheet names, not indexes, the rules of which you changed after the fact.
(2) It placed the A2 value of each sheet in column B, not the sheet name like you asked.
(3) For some reason only he knows why, he coded his macro to place the word "Sheet" and then the A2 value divided by 10, in column C, instead of just the A2 value like you asked. That is why his code would show a bizarre sheet name - looking value like "Sheet1.4" in the wrong column if the number 14 were in cell A2 of one of those sheets.

You might also need to end up taking values off sheets based on their VBA object code names, instead of by Index number or sheet tab name.

At this point I do not know what you will end up needing, but here is a set of two macros that do what you want per your latest request, given two possible scenarios described above.

'First 10 sheets, by Index number, excluding "Winners"
Sub SheetArray_TakeFourA()
Dim i As Integer, z As Worksheet
Set z = Worksheets("Results")
For i = 1 To 10
With Sheets(i)
If .Name <> "Winners" Then
z.Cells(i, 2).Value = .Name
z.Cells(i, 3).Value = .Range("A2").Value
End If
End With
Next i
z.Range("B1:C10").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
End Sub

'First 10 sheets, by Index number, that are not "Winners"
Sub SheetArray_TakeFourB()
Dim i As Integer, z As Worksheet
Set z = Worksheets("Results")
z.Range("B1:C11").ClearContents
For i = 1 To 10
With Sheets(i)
If .Name <> "Winners" Then
z.Cells(i, 2).Value = .Name
z.Cells(i, 3).Value = .Range("A2").Value
End If
End With
Next i
z.Range("B1:C10").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
If Application.CountA(z.Range("B1:B10")) = 9 Then
With Sheets(11)
z.Cells(11, 2).Value = .Name
z.Cells(11, 3).Value = .Range("A2").Value
End With
z.Range("B1:C11").Sort Key1:=z.Range("C1"), Order1:=xlAscending, Header:=xlNo
End If
End Sub

Thanks for the assistance and the patience. I asked in little bits because when I posted what I wanted, I didn't get what I wanted. I decided that either what I was asking for was too esoteric, too confusing, or some other reason that I didn't know. That made me decide to ask in steps. I thought that if I asked for generic solutions that would apply to my specific problem, and then build upon that, until I got the final generic solution to my specific problem, I'd have more success receiving possible solutions. Either it worked as I had predicted, or I just got lucky with you all seeing the post, who knows?
Again, thanks all for your help, I really appreciate it, and my apologies for any frustration I've caused.
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 04:24 AM.


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