Subject: Subject: Using A LOOP In A Macro (Newbie) 
Author Message
 Subject: Subject: Using A LOOP In A Macro (Newbie)

Hello Everyone.

I've recorded a macro that copies and pastes a row into another
worksheet. The problem is I need to put this into a loop so that it
will copy and paste a further 1500 rows. The existing macro code is as
follows:

Sheets("Base").Select
    Rows("7:7").Select
    Selection.Copy
    Range("A2").Select
    ActiveSheet.Paste
    Sheets("KMA Parts demands").Select
    Range("A2:P2").Select
    Range("P2").Activate
    Application.CutCopyMode = False
    Selection.Copy
    Range("R2").Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
        False, Transpose:=False

How can I put this in a loop so that the
Rows("7:7").Select
part will be incremented by +1 and the
Range("R2").Select
part will be incremented by +1 each time also.

Many thanks to anyone who can help a Newbie,

Nick.



Tue, 24 Feb 2004 00:01:21 GMT  
 Subject: Subject: Using A LOOP In A Macro (Newbie)
Nick,

Here is a possible use for you.

If there are no breaks in the data going down the sheet, then

dim lLine    as Long        ' This is for the second sheet

lLine = 1
while activecell.value <> ""
    activecell.entirerow.select
    selection.copy
    sheets("Sheet2 <or what ever your sheet is>").select
    rows(lline).select
    activesheet.paste
    lLine = lLine + 1
    sheets("Sheet1").select
    Application.CutCopyMode = False

    activecell.offset(1).select
wend

If there are breaks and you want to use a line/row to stop change
activecell.value to activecell.row < "Your value"

Basically, you are looping down the sheet and copying the values to a new
activesheet.
Note that the screen will bounce like a yo-yo. To stop that, insert the line

Application.screenupdating = false        ' at the beginnig
Application.screenupdating = true         ' at the end

Again, there are many different solutions that will work. The goal is to
find one that you like
and that fits the situation.

I hope this helps.

Justin


Quote:
> Hello Everyone.

> I've recorded a macro that copies and pastes a row into another
> worksheet. The problem is I need to put this into a loop so that it
> will copy and paste a further 1500 rows. The existing macro code is as
> follows:

> Sheets("Base").Select
>     Rows("7:7").Select
>     Selection.Copy
>     Range("A2").Select
>     ActiveSheet.Paste
>     Sheets("KMA Parts demands").Select
>     Range("A2:P2").Select
>     Range("P2").Activate
>     Application.CutCopyMode = False
>     Selection.Copy
>     Range("R2").Select
>     Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
> SkipBlanks:= _
>         False, Transpose:=False

> How can I put this in a loop so that the
> Rows("7:7").Select
> part will be incremented by +1 and the
> Range("R2").Select
> part will be incremented by +1 each time also.

> Many thanks to anyone who can help a Newbie,

> Nick.



Tue, 24 Feb 2004 01:47:16 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Subject: Using A LOOP In A Macro (Newbie)

2. macro to add recipient's name to subject line

3. Macro to Change Subject Line

4. Macro to read emails with same subject header

5. Macro to Remind Subject Empty before Sending

6. How get new mail subject using VBA?

7. Get email Subject titles using VBA

8. How get new mail subject using VBA?

9. Changing the Subject or Keywords using VBA

10. E-mail addresss and subject using automation

11. Setting e-mail address and subject using vba

12. Subject: Problems using MAPI over a WAN.

 

 
Powered by phpBB® Forum Software