+ Reply to Thread
Results 1 to 3 of 3

Creating Simpler Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    02-25-2009
    Location
    Not London
    MS-Off Ver
    Excel 2007
    Posts
    1

    Creating Simpler Macro

    Hi, I've created a very crude macro to shift 3 cells in the A column into the A,B,C rows. It looks like this (keep in mind all i did was use the record function)

    Sub Test()
        Range("A2").Select
        Selection.Cut Destination:=Range("A2")
        Range("A3").Select
        Selection.Cut Destination:=Range("B2")
        Range("A4").Select
        Selection.Cut Destination:=Range("C2")
        Range("A5").Select
        Selection.Cut Destination:=Range("A3")
        Range("A6").Select
        Selection.Cut Destination:=Range("B3")
        Range("A7").Select
        Selection.Cut Destination:=Range("C3")
        Range("A8").Select
        Selection.Cut Destination:=Range("A4")
        Range("A9").Select
        Selection.Cut Destination:=Range("B4")
        Range("A10").Select
        Selection.Cut Destination:=Range("C4")
        Range("A11").Select
        Selection.Cut Destination:=Range("A5")
        Range("A12").Select
        Selection.Cut Destination:=Range("B5")
        Range("A13").Select
        Selection.Cut Destination:=Range("C5")
        Range("A14").Select
        Selection.Cut Destination:=Range("A6")
        Range("A15").Select
        Selection.Cut Destination:=Range("B6")
        Range("A16").Select
        Selection.Cut Destination:=Range("C6")
        Range("A17").Select
        Selection.Cut Destination:=Range("A7")
        Range("A18").Select
        Selection.Cut Destination:=Range("B7")
        Range("A19").Select
        Selection.Cut Destination:=Range("C7")
        Range("A20").Select
        Selection.Cut Destination:=Range("A8")
        Range("A21").Select
        Selection.Cut Destination:=Range("B8")
        Range("A22").Select
        Selection.Cut Destination:=Range("C8")
        Range("A23").Select
        Selection.Cut Destination:=Range("A9")
        Range("A24").Select
        Selection.Cut Destination:=Range("B9")
        Range("A25").Select
        Selection.Cut Destination:=Range("C9")
        Range("A26").Select
        Selection.Cut Destination:=Range("A10")
        Range("A27").Select
        Selection.Cut Destination:=Range("B10")
        Range("A28").Select
        Selection.Cut Destination:=Range("C10")
        Range("A29").Select
        Selection.Cut Destination:=Range("A11")
        Range("A30").Select
        Selection.Cut Destination:=Range("B11")
        Range("A31").Select
        Selection.Cut Destination:=Range("C11")
        Range("A32").Select
        Selection.Cut Destination:=Range("A12")
        Range("A33").Select
        Selection.Cut Destination:=Range("B12")
        Range("A34").Select
        Selection.Cut Destination:=Range("C12")
        Range("A35").Select
        Selection.Cut Destination:=Range("A13")
        Range("A36").Select
        Selection.Cut Destination:=Range("B13")
        Range("A37").Select
        Selection.Cut Destination:=Range("C13")
        Range("A38").Select
        Selection.Cut Destination:=Range("A14")
        Range("A39").Select
        Selection.Cut Destination:=Range("B14")
        Range("A40").Select
        Selection.Cut Destination:=Range("C14")
        Range("A41").Select
        Selection.Cut Destination:=Range("A15")
        Range("A42").Select
        Selection.Cut Destination:=Range("B15")
        Range("A43").Select
        Selection.Cut Destination:=Range("C15")
        Range("A44").Select
        Selection.Cut Destination:=Range("A16")
        Range("A45").Select
        Selection.Cut Destination:=Range("B16")
        Range("A46").Select
        Selection.Cut Destination:=Range("C16")
        Range("A47").Select
        Selection.Cut Destination:=Range("A17")
        Range("A48").Select
        Selection.Cut Destination:=Range("B17")
        Range("A49").Select
        Selection.Cut Destination:=Range("C17")
    End Sub
    A1,B1,C1 are fields i don't want to modify, that is why they aren't included.

    but what I would like it to see if anyone can create a simpler form of this macro that can go through the whole A column, not just up to A49 like ive done.

    I would do this myself if i knew anything about creating macros :P

    Thanks guys!

  2. #2
    Forum Contributor
    Join Date
    10-27-2006
    Location
    United Kingdom
    MS-Off Ver
    EXCEL 2003 (2007 at home)
    Posts
    517

    Loopy

    What you need are some nested loops, try:

    Sub Test()
        i = 1
        ir = 2
        Do While i < 49
            For ic = 1 To 3
                Range.Cells(i, 1).Select
                Selection.Cut Destination:=Range.Cells(ir, ic)
                i = i + 1
            Next ic
            ir = ir + 1
        Loop
    End Sub
    If you have done any programming in general, (even if not VBA) this should be fairly familiar.

    Note:
    I just threw this together in a rush, I think it's ok but...
    In any case it should give you pointers as to how to progress.
    Mark.

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Creating Simpler Macro

    d4ftpuNK

    Welcome to Exceltip forum

    Please take a couple of minutes and read ALL the Forum Rules


    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ 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