+ Reply to Thread
Results 1 to 5 of 5

Destination copy and paste (values only) for copy loop

Hybrid View

mr_mango81 Destination copy and paste... 05-01-2013, 08:05 PM
AlvaroSiza Re: Destination copy and... 05-01-2013, 08:13 PM
mr_mango81 Re: Destination copy and... 05-01-2013, 08:19 PM
AlvaroSiza Re: Destination copy and... 05-01-2013, 08:47 PM
mr_mango81 Re: Destination copy and... 05-01-2013, 08:59 PM
  1. #1
    Registered User
    Join Date
    02-05-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    39

    Destination copy and paste (values only) for copy loop

    Hi guys I'm trying to copy and paste a title and subtotal from a range of data in columns AD:AF;

    Example;
    BH113 13/03/2013 72
    13/03/2013 106
    13/03/2013 67
    Total 245

    BH114 5/03/2013 92
    7/03/2013 77
    13/03/2013 90
    14/03/2013 59
    18/03/2013 52
    19/03/2013 44
    21/03/2013 88
    Total 502

    CR102 25/03/2013 107
    27/03/2013 117
    Total 224

    I want to paste the information in columns O:P to look like the following as a summary, so the data needs to be pasted as values only;

    BH113 245
    BH114 502
    CR102 224

    Here is the code I have so far,

    Sub AAmonth()
    Range("AD3").Select 'this is the fisrt cell with data (BH113)
    Do Until Selection.Value = ""
        
        Selection.Copy Destination:=Range("O3", Range("O" & Rows.Count).End(xlUp).Offset(1, 0))
    Selection.Offset(, 2).End(xlDown).Select
    
    Selection.Copy Destination:=Range("P3", Range("P" & Rows.Count).End(xlUp).Offset(1, 0))
        Selection.Offset(2, -2).Select
    
    Loop
    End Sub
    This code copies the data into the correct columns, but it doesn't paste values only and the data from clolumn AD all ends up as the last value (CR102)

    Help would be greatly appreciated.

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Destination copy and paste (values only) for copy loop

    Please post a sample workbook. My immediate thought would be to use the pastespecial method against a range object. Something like...

    Dim rngTarget As Range
    Set rngTarget = Range("O3", Range("0" & Rows.Count).End(xlUp).Offset(1))
    Selection.Copy
    rngTarget.PasteSpecial xlPasteValues
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    02-05-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Destination copy and paste (values only) for copy loop

    Here is an example of my sheet.
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Destination copy and paste (values only) for copy loop

    An array approach. Please make a copy of your workbook and test on the copy.
    Option Explicit
    
    Sub TryThis()
    
    Dim wb          As Workbook
    Dim ws          As Worksheet
    Dim rngPlant    As Range
    Dim rngDest     As Range
    Dim pArr()      As Variant
    Dim dArr()      As Variant
    Dim i           As Integer
    Dim j           As Integer
    Dim k           As Integer
    
    Set wb = ActiveWorkbook
    Set ws = wb.Sheets("Monthly Fuel Summary")
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
            With ws
                Set rngPlant = .Range("AD3", .Range("AF65532").End(xlUp))
                pArr = rngPlant
            End With
            
            ReDim dArr(1 To UBound(pArr), 1 To 2)
            j = 0
            For i = LBound(pArr) To UBound(pArr)
                If pArr(i, 1) <> "" Then
                    j = j + 1
                    dArr(j, 1) = pArr(i, 1)
                End If
            Next i
            
            j = 0
            For k = LBound(pArr) To UBound(pArr)
                If pArr(k, 2) = "Total" Then
                    j = j + 1
                    dArr(j, 2) = pArr(k, 3)
                End If
            Next k
            
            Set rngTarget = ws.Range("O3")
            rngTarget.Resize(j, 2) = dArr
            
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        
    End Sub

  5. #5
    Registered User
    Join Date
    02-05-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Destination copy and paste (values only) for copy loop

    This works beautifully AlvaroSiza!!! Thankyou so much!!

+ 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