+ Reply to Thread
Results 1 to 5 of 5

macro to copy and paste data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299

    macro to copy and paste data

    I need to copy cells in A and paste it into sheet "Upload" in col A from the last used row down. However, I only need to copy the cells that begin with a 6 digit number. I also need to take E and copy to C on upload sheet.
    For example: if A2 = "total revenue" then do nothing, if A3 = "163001.419"
    Then copy It to A in upload and copy E to C in upload.
    1 more thing...I need B in upload to =the sheet name that A and C came from plus the following text "Undistributed Stores Exp".
    Is this possible? Would be a huge time saver.
    thank you.

    I can post a sample workbook if need be.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    erock24,

    It seems to me you have no intention of trying to learn VBA yourself as you get this forum and MrExcel to do it all for you.

    So instead of doing your job for you and not getting your pay I think it's about time you did some work

    See link to extract data

    http://www.rondebruin.nl/copy5.htm

    You need to use a line such as for your criteria

    rng.AutoFilter Field:=1, Criteria1:=">99999", Operator:=xlAnd
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299
    That's a wonderful site...thank you for the link.
    It's unfortunate I come accross that way. It's not my intention. I feel bad. My questions are normally apart of a much bigger picture.

    Is there a way to get all the results for this, but to exclude any that contain a "-"
    rng.AutoFilter Field:=1, Criteria1:="=*.*", Operator:=xlAnd

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Try turning on the VBA recorder. Will give code like

    .AutoFilter Field:=1, Criteria1:="<>*-*", Operator:=xlAnd, _
            Criteria2:=">99999"
    VBA Noob

  5. #5
    Forum Contributor
    Join Date
    02-20-2007
    MS-Off Ver
    2003 & 2007
    Posts
    299
    With the help of Ron de Bruins site, VBA Noob, and the macro recorder, I completed this macro. It works perfect. It's long because it repeats for each sheet. I couldn't figure out how to set WS1 to the activesheet and have the macro cycle through my sheets activating them so as to only have on code that runs for everysheet. But it works.
    Sub Upload()
        Dim WS1 As Worksheet
        Dim WS2 As Worksheet
        Dim rng As Range
        Dim rng2 As Range
        Dim shname As Variant
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    
        'Name of the worksheet with the data and the destination sheet
        Set WS1 = Sheets("TH")  '<<< Change
        Set WS2 = Sheets("Upload")  '<<< Change
        WS2.Activate
        Rows("2:2").Select
        Range(Selection, Selection.End(xlDown)).ClearContents
        Range("A1").Select
    
        'Set filter range : A1 is the top left cell of your filter range and
        'the header of the first column, D is the last column in the filter range
        Set rng = WS1.Range("A6:E" & Rows.Count)
    
        'Firstly, remove the AutoFilter
        WS1.AutoFilterMode = False
    
        'This example filters on the first column in the range (change the field if needed)
        'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
        'Use "<>Netherlands" if you want the opposite
        rng.AutoFilter Field:=1, Criteria1:="<>*-*", Operator:=xlAnd, Criteria2:="*.*"
    
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        With WS1.AutoFilter.Range
            On Error Resume Next
            ' Set rng2 to rng without the header row
            Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
                       .SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not rng2 Is Nothing Then
                'Copy and paste the cells into WS2 below the existing data
                rng2.Copy
                With WS2.Range("A" & lastrow(WS2) + 1)
                    ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
                    '.PasteSpecial Paste:=8
                    .PasteSpecial xlPasteValues
                    '.PasteSpecial xlPasteFormats
                    Application.CutCopyMode = False
                    End With
                'Delete the rows in WS1
                'rng2.EntireRow.Delete
            End If
        End With
        With WS2
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).FormulaR1C1 = "TH Undistributed Stores Exp"
        .Range(.Cells(.Rows.Count, "B").End(xlUp), .Cells(.Rows.Count, "A").End(xlUp).Offset(, 1)).FillDown
        End With
    
        WS1.AutoFilterMode = False
    
        'Name of the worksheet with the data and the destination sheet
        Set WS1 = Sheets("NM")  '<<< Change
        Set WS2 = Sheets("Upload")  '<<< Change
    
        'Set filter range : A1 is the top left cell of your filter range and
        'the header of the first column, D is the last column in the filter range
        Set rng = WS1.Range("A6:E" & Rows.Count)
    
        'Firstly, remove the AutoFilter
        WS1.AutoFilterMode = False
    
        'This example filters on the first column in the range (change the field if needed)
        'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
        'Use "<>Netherlands" if you want the opposite
        rng.AutoFilter Field:=1, Criteria1:="<>*-*", Operator:=xlAnd, Criteria2:="*.*"
    
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        With WS1.AutoFilter.Range
            On Error Resume Next
            ' Set rng2 to rng without the header row
            Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
                       .SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not rng2 Is Nothing Then
                'Copy and paste the cells into WS2 below the existing data
                rng2.Copy
                With WS2.Range("A" & lastrow(WS2) + 1)
                    ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
                    '.PasteSpecial Paste:=8
                    .PasteSpecial xlPasteValues
                    '.PasteSpecial xlPasteFormats
                    Application.CutCopyMode = False
                    End With
                'Delete the rows in WS1
                'rng2.EntireRow.Delete
            End If
        End With
        With WS2
       .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).FormulaR1C1 = "NM Undistributed Stores Exp"
        .Range(.Cells(.Rows.Count, "B").End(xlUp), .Cells(.Rows.Count, "A").End(xlUp).Offset(, 1)).FillDown
        End With
        
        WS1.AutoFilterMode = False
    
        'Name of the worksheet with the data and the destination sheet
        Set WS1 = Sheets("Holden")  '<<< Change
        Set WS2 = Sheets("Upload")  '<<< Change
    
        'Set filter range : A1 is the top left cell of your filter range and
        'the header of the first column, D is the last column in the filter range
        Set rng = WS1.Range("A6:E" & Rows.Count)
    
        'Firstly, remove the AutoFilter
        WS1.AutoFilterMode = False
    
        'This example filters on the first column in the range (change the field if needed)
        'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
        'Use "<>Netherlands" if you want the opposite
        rng.AutoFilter Field:=1, Criteria1:="<>*-*", Operator:=xlAnd, Criteria2:="*.*"
    
        'Copy the visible data and use PasteSpecial to paste to the new worksheet
        With WS1.AutoFilter.Range
            On Error Resume Next
            ' Set rng2 to rng without the header row
            Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
                       .SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not rng2 Is Nothing Then
                'Copy and paste the cells into WS2 below the existing data
                rng2.Copy
                With WS2.Range("A" & lastrow(WS2) + 1)
                    ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
                    '.PasteSpecial Paste:=8
                    .PasteSpecial xlPasteValues
                    '.PasteSpecial xlPasteFormats
                    Application.CutCopyMode = False
                    End With
                'Delete the rows in WS1
                'rng2.EntireRow.Delete
            End If
        End With
        With WS2
        .Columns("C:D").Delete Shift:=xlToLeft
        .Range("C1").FormulaR1C1 = "Value"
        .Range("D1").FormulaR1C1 = "Ref1"
        .Range("E1").FormulaR1C1 = "Ref2"
        .Range("F1").FormulaR1C1 = "Ref3"
        .Range("G1").FormulaR1C1 = "Ref4"
        .Range("H1").FormulaR1C1 = "Ref5"
        .Range("I1").FormulaR1C1 = "Ref6"
        .Range("J1").FormulaR1C1 = "DebCred"
        .Range("K1").FormulaR1C1 = "DueDate"
        .Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).FormulaR1C1 = "Holden Undistributed Stores Exp"
        .Range(.Cells(.Rows.Count, "B").End(xlUp), .Cells(.Rows.Count, "A").End(xlUp).Offset(, 1)).FillDown
        .Columns("C:C").Style = "Comma"
        End With
        
        WS1.AutoFilterMode = False
        WS2.Activate
        Rows("2:2").Select
        Range(Selection, Selection.End(xlDown)).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortTextAsNumbers
        Range("A1").Select
    End Sub
    Function lastrow(sh As Worksheet)
        On Error Resume Next
        lastrow = sh.Cells.Find(What:="*", _
                                After:=sh.Range("A1"), _
                                Lookat:=xlPart, _
                                LookIn:=xlValues, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, _
                                MatchCase:=False).Row
        On Error GoTo 0
    End Function

+ 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