Thread: For...Next Loop
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
PCLIVE PCLIVE is offline
external usenet poster
 
Posts: 1,311
Default For...Next Loop

This seems like some type of test or something. There are a few areas that
seem like errors. If I understand correctly, you want to loop through all
the sheets (except Summary) and copy a specific range to the Summary sheet.
I've added some corrections to the code...see comments in those lines.

Sub ListData()
Dim A As Integer 'This seems to have no purpose
Dim rng2 As Range 'This doesn't seem correct for WorkSheets
Set rng2 = Range("B3") 'This doesn't seem correct for WorkSheets

For Each rng2 In ActiveWorkbook.Sheets 'Because of the Dim above, this
doesn't seem to work
If (Sheet.Name) < "Summary" Then

Range("B39:T39").Select
Selection.Copy

Sheets("Summary").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

rng2.Offset(I, 0).Value = Sheet.Name 'Is this a type "I" since there
is no variable set. Should it be 1?
A = A + 1 'This seems to have no purpose
End If
Next rng2
End Sub


**********I would use the following:
I just realized, I need to know more about what this is supposed to do.
Even though it doesn't give errors, some cells are overwriting each other.

Sub ListData()

Dim A As Integer

For Each ws In ActiveWorkbook.Sheets
If ws.Name < "Summary" _
Then
Range("B39:T39").Select
Selection.Copy
Sheets("Summary").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

ActiveCell.Offset(A, 0).Value = ws.Name
A = A + 1
End If
Next ws

End Sub




"ryguy7272" wrote in message
...
Between using the macro recorder and recycling some code that I found on
this
DG a while back, I tried to create a simple loop to select a range of
cells
on each sheet in my workbook, and copy/paste the values into 'Summary'. I
don't want to copy the Range("B39:T39") from the 'Summary', but I do want
it
from all other sheets..and then copy/paste it to the 'Summary'. Should be
a
simple fix.I hope.


Code listed below; would someone please explain what I am doing wrong:

Sub ListData()

Dim A As Integer
Dim rng2 As Range
Set rng2 = Range("B3")

For Each rng2 In ActiveWorkbook.Sheets
If (Sheet.Name) < "Summary" Then

Range("B39:T39").Select
Selection.Copy

Sheets("Summary").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

rng2.Offset(I, 0).Value = Sheet.Name
A = A + 1
End If
Next rng2

End Sub


PS, sorry if this double-posts...I think I just got kicked out of my
original posting window...

--
RyGuy