+ Reply to Thread
Results 1 to 8 of 8

VBA: Copy data from every other row, paste it in previous row, delete copied row

Hybrid View

JohnJames VBA: Copy data from every... 09-19-2017, 05:33 PM
mehmetcik Re: VBA: Copy data from every... 09-19-2017, 06:08 PM
Sintek Re: VBA: Copy data from every... 09-20-2017, 08:04 AM
JohnJames Re: VBA: Copy data from every... 09-20-2017, 11:28 AM
Sintek Re: VBA: Copy data from every... 09-20-2017, 12:00 PM
JohnJames Re: VBA: Copy data from every... 09-20-2017, 01:33 PM
Sintek Re: VBA: Copy data from every... 09-20-2017, 01:42 PM
JohnJames Re: VBA: Copy data from every... 09-21-2017, 11:16 AM
  1. #1
    Registered User
    Join Date
    04-02-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    VBA: Copy data from every other row, paste it in previous row, delete copied row

    Hi all!

    A team I work with sends us reports that we want to put in a database, unfortunately the data for one entry is on multiple lines and it had data that was uneeded. I figured out how to delete the unneeded data and get rid of the blank spaces, just having trouble with the last part which is copying the data in every other row, pasting it in the previous row then deleting the copied row. Fig.1 is what I have currently, I want my data to look like Fig.2.

    I found the following code, but don't know how I could make this work for x amount of rows or my range:

    Set Rng = Range("B7")
    Range(Rng.Address).Offset(-1, 0).Value = Rng.Value

    Fig.1

    dataR1
    data11 data12 data13 data14 data15 data16
    dataR2
    data21 data22 data23 data24 data25 data26
    dataR3
    data31 data32 data33 data34 data35 data36


    Fig.2

    dataR1 data11 data12 data13 data14 data15
    dataR2 data21 data22 data23 data24 data25
    dataR3 data31 data32 data33 data34 data35

    Any help would be greatly appreciated!

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA: Copy data from every other row, paste it in previous row, delete copied row

    Record yourself selecting range A1:A18

    Then selecting Home, Find, Goto Special, Blanks

    then Right clicking on a selected cell

    then selecting Delete, Entire Row.

    You will get this:

    
        Range("A1:A18").Select
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.EntireRow.Delete
    Which can be reduced to:

    
        Range("A1:A18").SpecialCells(xlCellTypeBlanks).Select
        Selection.EntireRow.Delete
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: VBA: Copy data from every other row, paste it in previous row, delete copied row

    Something like this...
    Option Explicit
    Sub JohnJames()
    Dim i As Long
    Application.ScreenUpdating = False
    For i = Cells(Rows.Count, "B").End(xlUp).Row To 1 Step -2
        Range("B" & i & ":G" & i).Copy Range("B" & i - 1)
        Rows(i).Delete
    Next i
    Application.ScreenUpdating = True
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  4. #4
    Registered User
    Join Date
    04-02-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA: Copy data from every other row, paste it in previous row, delete copied row

    Thanks so much Sintek! this is what I needed, just getting the following error when I run it though

    Run-time error '1004' : Method 'Range' of object'_Global' failed
    I know it has to do with Range, but B:G is the range on the sheet, so don't understand why it's doing this.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: VBA: Copy data from every other row, paste it in previous row, delete copied row

    I'm thinking you should specify the sheet....that works for activeSheet only

    With Sheets("YourSheetName")
        For i = .Cells(Rows.Count, "B").End(xlUp).Row To 1 Step -2
            .Range("B" & i & ":G" & i).Copy .Range("B" & i - 1)
            Rows(i).Delete
        Next i
    End With
    Thanks for rep +
    Last edited by Sintek; 09-20-2017 at 12:02 PM.

  6. #6
    Registered User
    Join Date
    04-02-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA: Copy data from every other row, paste it in previous row, delete copied row

    You're welcome, sintek. I figured out what the issue was by debugging.

    When the For loop hits 1, the error occurs so I changed the code to exit the loop when I= 1

    Option Explicit
    Sub JohnJames()
    Dim i As Long
    Application.ScreenUpdating = False
    With Sheets("DBAMonthly")
        For i = .Cells(Rows.Count, "B").End(xlUp).Row To 1 Step -2
        
        
            If i <> 1 Then
            
            
            .Range("B" & i & ":G" & i).Copy .Range("B" & i - 1)
            Rows(i).Delete
            
            
            End If
            
            
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Thanks for all the help!

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,957

    Re: VBA: Copy data from every other row, paste it in previous row, delete copied row

    Nice one John..Did not see that coming...
    perhaps this could also solve?
    For i = .Cells(Rows.Count, "B").End(xlUp).Row To 2 Step -2

  8. #8
    Registered User
    Join Date
    04-02-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA: Copy data from every other row, paste it in previous row, delete copied row

    Thanks sintek. That works as well.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Autofilter, copy from sheet 1 and paste to sheet2, delete previous info on sheet 2
    By leigh12483 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-30-2015, 08:21 PM
  2. Replies: 0
    Last Post: 05-16-2014, 05:54 AM
  3. [SOLVED] Copy data from one worksheet to another then delete the copied cells
    By youranitsas in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-30-2014, 03:48 PM
  4. [SOLVED] Copy/Paste from a Filtered Range and Insert copied data NOT overwrite
    By Simon.Ward in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-12-2013, 11:14 AM
  5. [SOLVED] Copy row of data, paste in another worksheet, delete some of the cells copied
    By dev111ski in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-14-2012, 05:58 PM
  6. Copy and paste data from Previous Month
    By dwalton5 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2011, 12:20 AM
  7. Need to Fin Last Row then copy / paste previous 6 rows and delete data
    By dwalton5 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-01-2011, 01:16 PM

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