Results 1 to 5 of 5

VBA copy/paste pasting to wrong row

Threaded View

  1. #1
    Registered User
    Join Date
    07-31-2014
    Location
    Texas
    MS-Off Ver
    Microsoft Office Professional Plus 2021
    Posts
    49

    Question VBA copy/paste pasting to wrong row

    Let me start by saying that I am one of those who's best friend is Google when trying to cobble vba code together for a spreadsheet. I had the following code working, then broke it. All it is supposed to do is clear the contents of cells in AL6 to AQ & Lastrow. Copy values from several columns (A6 to C & lastrow, G6 to I & lastrow, M6 to O & lastrow, etc) and stack that data in AL6 to AN & lastrow. Then copy values from another similar set of columns and stack that data in AO6 to AQ & lastrow.

    The code worked as expected, until I started messing around with code to get columns sorted. It started pasting starting in AL2 instead of AL6 and AO2 instead of AO6 as expected. I deleted the sort code. I went into the spreadsheet and manually cleared any sorting that I could find, but still pasting everything starting in row 2 instead of 6. What did I do?

    Here's my code in all it's glory. May not be pretty to some here in the forum, but it used to work, at least. Any help appreciated.

    Sub test()
    
    Dim LastRow As Long
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        Sheet1.Activate
            
        'Clear cells AL6 to AN & lastrow
        LastRow = Range("AL" & Rows.Count).End(xlUp).Row
        Range(Cells(6, 38), Cells(LastRow, 40)).ClearContents
        
        'Clear cells AO6 to AQ & lastrow
        LastRow = Range("AO" & Rows.Count).End(xlUp).Row
        Range(Cells(6, 41), Cells(LastRow, 43)).ClearContents
        
        'Determine last row of each column, copy, then paste column contents into column AL (#38)
        'and column AO (#41) for all accounts
        Range("A6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 1), Cells(LastRow, 3)).Copy
        Cells(Rows.Count, 38).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("G6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 7), Cells(LastRow, 9)).Copy
        Cells(Rows.Count, 38).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("M6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 13), Cells(LastRow, 15)).Copy
        Cells(Rows.Count, 38).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("S6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 19), Cells(LastRow, 21)).Copy
        Cells(Rows.Count, 38).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("Y6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 25), Cells(LastRow, 27)).Copy
        Cells(Rows.Count, 38).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("AE6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 31), Cells(LastRow, 33)).Copy
        Cells(Rows.Count, 38).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("D6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 4), Cells(LastRow, 6)).Copy
        Cells(Rows.Count, 41).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("J6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 10), Cells(LastRow, 12)).Copy
        Cells(Rows.Count, 41).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("P6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 16), Cells(LastRow, 18)).Copy
        Cells(Rows.Count, 41).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("V6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 22), Cells(LastRow, 24)).Copy
        Cells(Rows.Count, 41).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("AB6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 28), Cells(LastRow, 30)).Copy
        Cells(Rows.Count, 41).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
        
        Range("AH6").Select
        Do Until ActiveCell.Value = ""
        ActiveCell.Offset(1, 0).Select
        Loop
        LastRow = ActiveCell.Row - 1
        Range(Cells(6, 34), Cells(LastRow, 36)).Copy
        Cells(Rows.Count, 41).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
    
        
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
    
    
    End Sub
    Last edited by LITOA; 11-25-2016 at 05:31 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] It sets date format wrong when copy and paste
    By zanshin777 in forum Excel General
    Replies: 18
    Last Post: 01-16-2016, 08:16 AM
  2. Copy/Paste restriction removed when pasting from one wb to another
    By iecbso in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-07-2015, 01:13 PM
  3. Copy and paste under the last used row - what's wrong with this code?
    By luke1908 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-24-2014, 04:10 PM
  4. Copy and paste code pasting won't paste just values
    By thehotbreadguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2013, 11:12 PM
  5. [SOLVED] Copy/paste button code work wrong
    By mirenoba in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2013, 05:52 AM
  6. Copy/paste-button - wrong reference code in sheet 2
    By Krapter in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2009, 05:17 AM
  7. Copy-paste from csv to xls macro > wrong date format
    By Gero in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2009, 06:12 AM

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