+ Reply to Thread
Results 1 to 4 of 4

Copy/Paste 30 Times or Loop?

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2008
    Location
    Canada
    Posts
    79

    Copy/Paste 30 Times or Loop?

    Hi guys, I've attached below a small part of my code. I am wondering if there is a way to do this, without copying and pasting something 30 times, but using a loop instead.

    As you can see there are a series of productsNumbers (30 in total, but this example is for 3). Each productNumber is a string that is actually a 5 digit number, so it can be an integer as well, if it has to.

    I would like to code below to loop for as many products as there may be. The way that I have it right now does not work - I feel that I am missing something small or that there is no way to do this. Suggestions are apperciated. Thanks!

    Sub Summarize()
    
        Dim productNumber1 As String
        Dim productNumber2 As String
        Dim productNumber3 As String
    
        Dim fCell As Range
        Dim lCell As Range
        Dim rng As Range
        
        Dim i As Integer
        
        productNumber1 = Sheets("SKUs Input").Range("A3")
        productNumber2 = Sheets("SKUs Input").Range("A4")
        productNumber3 = Sheets("SKUs Input").Range("A5")
        
        For i = 1 To 3
        
        'Atlantic
        
        Sheets("Atl").Select
        Set fCell = Range("E2")
        Set lCell = Range("E65536").End(xlUp)
        Set rng = Range(fCell, lCell)
            
        Sheets("Calculations").Select
        Range("A2", "G98").Select
        Selection.Clear
        
        For Each Cell In rng
            If Cell = "productNumber" & i Then
                Cell.Resize(1, 7).Copy
                Sheets("Calculations").Select
                Range("A65536").End(xlUp).Offset(1, 0).Select
                Selection.PasteSpecial (xlPasteValues)
                ActiveCell.Offset(1, 0).Select
            End If
        Next
        
        Sheets("Calculations").Select
        Range("H100").Copy
        Sheets("STAX Summary").Select
        Range("C22").End(xlUp).Offset(1, 0).Select
        Selection.PasteSpecial (xlPasteValues)
        
        Next i
        
    End Sub
    Last edited by GeorgY; 11-18-2009 at 12:33 PM.

  2. #2
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Copy/Paste 30 Times or Loop?

    Oh my, yes there are a few improvements...

    Why not just use autofilter?

    Also, post an example.
    CC

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Copy/Paste 30 Times or Loop?

    Hi,

    It seems you've already got the guts of your loop. If I've understood correctly, delete the productnumber1, productnumber2 etc. variables.

    Make the loop counter(assuming the product numbers are in a contiguous range),

    For i = 1 to Sheets("SKUs Input").Range("A3").CurrentRegion.Rows.Count
    Then in the For Each Cell..Next cell loop

    If Cell = Range("A3").Cells(i,1) Then
    What's the object of the code? There may be a simpler way of solving your requirement, perhaps a pivot table???

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    12-09-2008
    Location
    Canada
    Posts
    79

    Re: Copy/Paste 30 Times or Loop?

    Thanks Richard, that was exactly what I was looking for.

    Yes, it's currently being done by pivot table. I like code more.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1