+ Reply to Thread
Results 1 to 4 of 4

a macro question

Hybrid View

billandrus a macro question 11-15-2006, 02:26 PM
MDubbelboer a simple for loop with a step... 11-15-2006, 03:30 PM
Mallycat It is usually better to post... 11-15-2006, 03:32 PM
billandrus Thanks. Problem solved! 11-15-2006, 04:36 PM
  1. #1
    Registered User
    Join Date
    10-08-2005
    Posts
    5

    a macro question

    I wish to copy a column of numbers, a number in every 5th row, from sheet1 to sheet 2 same column but in consecutive rows.

    I have tried to write a do loop to do this but I have to have a variable in the adress of a cell (eg R[2]C) has to be R[p]C where p is counter increasing by 5 every cycle in th eloop.


    Thanks for any assistance

    Bill

  2. #2
    Forum Contributor
    Join Date
    07-13-2006
    Posts
    400
    a simple for loop with a step of 5 will do the trick

    Sub everyfifth()
        Dim i As Long
        Dim j As Long
        j = 1
    
        For i = 1 To 100 Step 5
            Worksheets("Sheet2").Cells(i, 1).Copy Destination:=Worksheets("Sheet1").Cells(j, 1)
            j = j + 1
        Next i
    End Sub
    --Mark

    Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?

  3. #3
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    It is usually better to post your code so someone can let you know what you are doing wrong. This code works for the first row in any column, then every 5th after that. You can change it to meet your needs. You don't state if it is always the same column, so I have made it variable - you need to click in the column that you want to copy.


    Sub copyColumn()
        Dim MyRow As Integer
        Dim LastRow As Integer
        Dim X As Integer
        Dim StartPos As Range
        Set StartPos = Selection
        LastRow = Selection.Range("A65000").End(xlUp)
        Selection.EntireColumn.Range("A1").Select
        X = 0
        Do While Selection.Row <= LastRow
            Sheets("Sheet2").Range("A1").Offset(X, 0).Value = Selection.Value
            Selection.Offset(5, 0).Select
            X = X + 1
        Loop
        StartPos.Select
    End Sub

  4. #4
    Registered User
    Join Date
    10-08-2005
    Posts
    5
    Thanks. Problem solved!

+ 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