Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel - Many sheets into one sheet

Status
Not open for further replies.

MarjiS

Programmer
Jul 5, 2005
13
US
I am copy/pasting info from multiple sheets into one master sheet. The format is the same on all sheets. Here is the code I'm using (simplified):

Code:
Private Sub btnCreate_Click()
Dim i, P1i, P2i
i = 2
For P1i = 2 To 10
    If Sheet2.Cells(P1i, 1) <> "" Then
        Sheet2.Rows(P1i).Copy
        'Sheet1.Rows(i).PasteSpecial
        Sheet1.Rows(i).PasteSpecial Paste:=xlPasteValues
        i = i + 1
    Else
        Exit For
    End If
Next
For P2i = 2 To 10
    If Sheet3.Cells(P1i, 1) <> "" Then
        Sheet3.Rows(P1i).Copy
        'Sheet1.Rows(i).PasteSpecial
        Sheet1.Rows(i).PasteSpecial Paste:=xlPasteValues
        i = i + 1
    Else
        Exit For
    End If
Next
End Sub

Sheet1 and Sheet2 contain 5 rows of information followed by 5 blank rows.

The subroutine stops on Sheet1 after correctly pasting the rows from Sheet2, with a msg of 'Select destination and press ENTER or choose paste'. It does not continue to copy/paste from Sheet3. The last pasted row on Sheet1 is selected and its corresponding copied row on Sheet2 is selected with the flashing dashes.

I have used the following code variation successfully but I'm trying to avoid using .Select.

Code:
i = 2
For P1i = 2 To 10
    If Sheet2.Cells(P1i, 1) <> "" Then
        Sheet2.Rows(P1i).Copy
        Sheet1.Rows(i).Select
        Selection.PasteSpecial Paste:=xlPasteValues
        i = i + 1
    Else
        Exit For
    End If
Next

What am I missing?

Thanks,
Marji
 
apart from on Sheet3 it copies the same row over and over...
Code:
    If Sheet3.Cells(P1i, 1) <> "" Then
        Sheet3.Rows(P1i).Copy
[green]' should be[/green]
    If Sheet3.Cells(P2i, 1) <> "" Then
        Sheet3.Rows(P2i).Copy



Robert Cumming
 
*looks embarrassed*
Well, duh.
I've gotten so used to getting syntax errors that I didn't think to look for error in my code.
Thanks, Bob.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top