+ Reply to Thread
Results 1 to 11 of 11

Run a Macro Every 7 Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2013
    Location
    Highlands, TX, USA
    MS-Off Ver
    2011
    Posts
    5

    Run a Macro Every 7 Rows

    I'm pretty new to Macros. I work for a school and we are exporting grades from one system to another. The data is exported in rows that have the students' name, ID #, course, course ID, and grades for Quarter 1, Quarter 2, Exam 1, Semester 1, Quarter 3, Quarter 4, Semester 2 in Columns. The new system requires a row entry for each grade. So I made a macro that cut and paste each grade column into separate rows and then copied the students' name, ID #, course, and course ID to those rows.

    Example:

    Student Name 012112 Algebra I 0123 90 80 70 80 90 90 85 88

    I first used a macro to separate each entry by 7 rows. Then ran my relative Macro to get this result

    Student Name 012112 Algebra I 0123 90
    Student Name 012112 Algebra I 0123 80
    Student Name 012112 Algebra I 0123 70
    Student Name 012112 Algebra I 0123 80
    Student Name 012112 Algebra I 0123 90
    Student Name 012112 Algebra I 0123 90
    Student Name 012112 Algebra I 0123 85
    Student Name 012112 Algebra I 0123 88

    The problem is that I'm unsure of how to make this Macro loop every 7 rows and save myself from getting Carpel Tunnel Syndrome.

    Here is my Macro for converting the rows into columns:

    Sub Grades2Rows2()
    '
    ' Grades2Rows2 Macro
    '
    ' Keyboard Shortcut: Option+Cmd+l
    '
        ActiveCell.Range("A1:G1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:F1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:E1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:D1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:C1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:B1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(-7, -4).Range("A1:D1").Select
        Selection.Copy
        ActiveCell.Offset(1, 0).Range("A1:D7").Select
        ActiveSheet.Paste
    End Sub
    Any help would be greatly appreciated. Thanks.

  2. #2
    Valued Forum Contributor
    Join Date
    10-26-2008
    Location
    Birmingham, UK
    MS-Off Ver
    All versions up to 2010
    Posts
    1,025

    Re: Run a Macro Every 7 Rows

    Hi
    Add this code to the beginning of your macro:

    With originsheet
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    For count = 1 to lastrow step 6

    Put this code at the end of your macro:

    Next count

    Hope this helps.
    Good luck.
    Tony

  3. #3
    Registered User
    Join Date
    08-01-2013
    Location
    Highlands, TX, USA
    MS-Off Ver
    2011
    Posts
    5

    Re: Run a Macro Every 7 Rows

    I'm getting a "Runtime error '424': Object Required" prompt when I try to run it. Am I missing something? Thanks again.

    Sub Grades2Rows3()
    '
    ' Grades2Rows3 Macro
    '
    ' Keyboard Shortcut: Option+Cmd+k
    '
    With originsheet
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    For Count = 1 To lastrow Step 6
    
        ActiveCell.Range("A1:G1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:F1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:E1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:D1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:C1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:B1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(-7, -4).Range("A1:D1").Select
        Selection.Copy
        ActiveCell.Offset(1, 0).Range("A1:D7").Select
        ActiveSheet.Paste
        Next Count
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    10-26-2008
    Location
    Birmingham, UK
    MS-Off Ver
    All versions up to 2010
    Posts
    1,025

    Re: Run a Macro Every 7 Rows

    Hi
    Sorry before With originsheet put "Dim originsheet as Worksheet"
    Tony

  5. #5
    Registered User
    Join Date
    08-01-2013
    Location
    Highlands, TX, USA
    MS-Off Ver
    2011
    Posts
    5

    Re: Run a Macro Every 7 Rows

    So I got it work on for one set and then I get a "Run-time error 1004 Application-defined or object-defined error."


    Sub Grades2Rows3()
    '
    ' Grades2Rows3 Macro
    '
    ' Keyboard Shortcut: Option+Cmd+k
    '
    Dim originsheet As Worksheet
    Set originsheet = Sheets("GRADES")
    With originsheet
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    For Count = 1 To lastrow Step 6
    
        ActiveCell.Range("A1:G1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:F1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:E1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:D1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:C1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1:B1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 1).Range("A1").Select
        Selection.Cut
        ActiveCell.Offset(1, -1).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(-7, -4).Range("A1:D1").Select
        Selection.Copy
        ActiveCell.Offset(1, 0).Range("A1:D7").Select
        ActiveSheet.Paste
        Next Count
    End Sub
    Last edited by fperez; 08-01-2013 at 02:15 PM.

  6. #6
    Valued Forum Contributor
    Join Date
    10-26-2008
    Location
    Birmingham, UK
    MS-Off Ver
    All versions up to 2010
    Posts
    1,025

    Re: Run a Macro Every 7 Rows

    Hi
    Are you able to post a sample of the before and after event so I can see exactly what is going on?
    Tony

  7. #7
    Registered User
    Join Date
    08-01-2013
    Location
    Highlands, TX, USA
    MS-Off Ver
    2011
    Posts
    5

    Re: Run a Macro Every 7 Rows

    Sure. I first run the macro below to separate out each different entry by 7 rows. I then run the Grades2Rows macro on column F for every different entry. Attached is the sample.

    Sub Insert7_v2()
        Dim rng As Range
    
        Set rng = Range("A2")
        While rng.Value <> ""
            rng.Offset(1).Resize(7).EntireRow.Insert
            Set rng = rng.Offset(8)
        Wend
    End Sub
    Attached Files Attached Files
    Last edited by fperez; 08-02-2013 at 12:16 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    10-26-2008
    Location
    Birmingham, UK
    MS-Off Ver
    All versions up to 2010
    Posts
    1,025

    Re: Run a Macro Every 7 Rows

    Hi
    Looking at the sample you provided you do not need a macro to transpose rows to columns, if I understand your need correctly. Have a look at sheet 2 on the attached sample and let me know if this is what you are after.
    If this is not what you are after please provide a before and after view of what you need and I will see what we can do for you.
    Tony
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    08-01-2013
    Location
    Highlands, TX, USA
    MS-Off Ver
    2011
    Posts
    5

    Re: Run a Macro Every 7 Rows

    Hello,

    Attached is the desired result. The new system's import template requires each grade have its own row. I've attached the desired result on sheet 2 of the attachment below. Thank you so much for your continued help.
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    10-26-2008
    Location
    Birmingham, UK
    MS-Off Ver
    All versions up to 2010
    Posts
    1,025

    Re: Run a Macro Every 7 Rows

    Hi
    Can you put column headings on both sheets so I can see how both sheets correlate to each other then I may be able to suggest something.
    Thanks
    Tony

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run a Macro Every 7 Rows

    Hi fperez
    Try the attached. The output is in new sheet.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro code to insert 3 rows on spreadsheet with variable number of rows
    By D18GE in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2013, 07:33 AM
  2. Replies: 9
    Last Post: 05-24-2013, 01:31 PM
  3. Replies: 13
    Last Post: 08-23-2011, 11:00 AM
  4. Macro to split a cell data into rows and copying other cells as it is in rows
    By shaan65 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-26-2010, 12:45 AM
  5. Macro to split a single cell data into rows and copying other cells as it is in rows
    By Pankaj Sonawane in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-25-2010, 07:09 PM

Tags for this Thread

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