+ Reply to Thread
Results 1 to 8 of 8

Macro for copying and duplicating data based on a cell values.

Hybrid View

  1. #1
    Registered User
    Join Date
    02-26-2007
    Location
    Vancouver, BC, Canada
    MS-Off Ver
    2007
    Posts
    22

    Question Macro for copying and duplicating data based on a cell values.

    I am trying to copy two cells from one worksheet to another in the same work book based on the value in Column B of one of the sheets. This is just a building block to a larger script I am going to create.

    Below is my attempt but I keep getting an error at Range(Cells(x, 3)).Select.

    Any help would be great.


    Sub Macro3()
    '
    ' Macro3 Macro
    '
    
    x = 2
    i = 1
    
    Do While i > 0
        Worksheets("Macro List").Activate
        Rows("3:3").Select
        Range("B3").Activate
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("B3").Select
        Worksheets("Gary Breakout").Activate
        Sheets("Gary Breakout").Select
        Range(Cells(x, 3)).Select
        Selection.Copy
        Worksheets("Macro List").Activate
        Sheets("Macro List").Select
        Range("B3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Worksheets("Gary Breakout").Activate
        Sheets("Gary Breakout").Select
        Range(Cells(x, 4)).Select
        Application.CutCopyMode = False
        Selection.Copy
        Worksheets("Macro List").Activate
        Sheets("Macro List").Select
        Range("C3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("D3").Select
        
        i = Cells(x, 2)
            x = x + 1
        Loop
        
    End Sub
    Last edited by outthere; 08-25-2009 at 12:32 PM. Reason: solved

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro for copying and duplicating data based on a cell values.

    Maybe like this:
    Sub Macro3()
        Dim x As Long
        Dim i As Long
        
        x = 2
        i = 1
    
        Do While i > 0
            With Worksheets("Macro List")
                .Rows(3).Insert
                .Range("B3").Value = Worksheets("Gary Breakout").Cells(x, 3).Value
                .Range("C3").Value = Worksheets("Gary Breakout").Cells(x, 4).Value
                i = .Cells(x, 2)
            End With
            x = x + 1
        Loop
    End Sub
    Last edited by shg; 08-22-2009 at 06:06 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    02-26-2007
    Location
    Vancouver, BC, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Macro for copying and duplicating data based on a cell values.

    I tried the code and i get a "Type Mismatch" Error at line:

    i = .Cells(x, 2)

    Any ideas?

    By the way, thanks for the help, I have a lot of learning to do!

  4. #4
    Registered User
    Join Date
    02-26-2007
    Location
    Vancouver, BC, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Macro for copying and duplicating data based on a cell values.

    I forgot to mention I am using Excel 2007 on Vista 64 Business, if that matters.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro for copying and duplicating data based on a cell values.

    I tried the code and i get a "Type Mismatch" Error at line:

    i = .Cells(x, 2)
    Chances are the cell contain a non-numeric value.

  6. #6
    Registered User
    Join Date
    02-26-2007
    Location
    Vancouver, BC, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Macro for copying and duplicating data based on a cell values.

    You are correct. It contains a count formula, any chance of making it work around that or will i have to paste values in the cell?

  7. #7
    Registered User
    Join Date
    02-26-2007
    Location
    Vancouver, BC, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Macro for copying and duplicating data based on a cell values.

    Ok i fixed it, the reference cell for "i" was in the other worksheet:

    Sub Macro4()
        Dim x As Long
        Dim i As Long
        
        x = 1
        i = 1
    
        Do While i > 0
            x = x + 1
            With Worksheets("Macro List")
                .Rows(3).Insert
                .Range("B3").Value = Worksheets("Gary Breakout").Cells(x, 3).Value
                .Range("C3").Value = Worksheets("Gary Breakout").Cells(x, 4).Value
                i = Worksheets("Gary Breakout").Cells(x, 2).Value
            End With
        Loop
    End Sub

  8. #8
    Registered User
    Join Date
    02-26-2007
    Location
    Vancouver, BC, Canada
    MS-Off Ver
    2007
    Posts
    22

    Re: Macro for copying and duplicating data based on a cell values.

    Thanks for the help. I have posted a new issue in a new thread found here:

    http://www.excelforum.com/excel-prog...ml#post2152004

    I guess it's a bit of a continuation of this one but any help would be appreciated.

+ 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