+ Reply to Thread
Results 1 to 12 of 12

Copy every 2nd row to the end of the previous row, then delete every second row

  1. #1
    Registered User
    Join Date
    08-22-2021
    Location
    Portugal
    MS-Off Ver
    Excel 2021 Mac
    Posts
    78

    Copy every 2nd row to the end of the previous row, then delete every second row

    Hi,
    I need some help creating a VBA for the attached excel, that I download from my local electricity company.
    The excel shows starting with row 9 my electricity meter readings in columns E - G, with some totals in columns H - J.
    Row 10 is the meter reading of the electricity I put back into the grid. The columns are the same.
    I would like to copy all values from row 10 (12, 14, 16...) columns D - J to the end of the row 9 (11, 13, 15...), then delete row 10 (12, 14, 16...). The resulting excel should only have rows where column D is "Energia consumida".

    After the excel is updated I can copy all values into another excel I use to calculate my electricity cost per day, month, year.
    FYI, I am using Mac Excel 2021.
    Thanks for your help.
    Gerd
    Attached Files Attached Files

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Maybe something like this?
    Please Login or Register  to view this content.
    BSB

  3. #3
    Registered User
    Join Date
    08-22-2021
    Location
    Portugal
    MS-Off Ver
    Excel 2021 Mac
    Posts
    78

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Well, that was fast, and it sure works. Thank you very much.
    But I now realize, that I should have rearranged my columns first, before doing the copying.
    Could you please first move columns G and I and insert it after column E.
    Then we would do the copy.
    This would have the meter readings next to each other and the totals next to each other.
    I apologize for not putting this into my initial request.

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Give this a try.
    Please Login or Register  to view this content.
    BSB

  5. #5
    Registered User
    Join Date
    08-22-2021
    Location
    Portugal
    MS-Off Ver
    Excel 2021 Mac
    Posts
    78

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Thanks again BSB, this works great.
    And I found what the UnMerge does. I had not paid attention to that columns heading being merged cells.

    After I ran the VBA I see a difference in the cell formatting.
    The original downloaded excel shows the numbers in columns E - J left adjusted in their cells, but after the copying was done the values in new columns K - Q are right adjusted in their cells. So the original values look like text, the copied values look like numbers.

    And after the copy is done my original values now have a small green triangle on the top left of each cell, showing the value having an error. I can select the cells and do a convert to number, but thought I add a new line of code and have VBA to do this formatting.

    I found some code on the forum to change the text values to numbers, added it to the VBA but it does not do anything. Obviously this new line is not correct. What code do I need to use to convert the original value to a number (I need numbers in my final calculation excel).

    Please Login or Register  to view this content.
    Gerd

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Try this.
    Please Login or Register  to view this content.
    BSB

  7. #7
    Registered User
    Join Date
    08-22-2021
    Location
    Portugal
    MS-Off Ver
    Excel 2021 Mac
    Posts
    78

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Thanks again for your help, BSB, it works just fine.
    I was working on my main excel several hours yesterday.
    But one thing I still needed to do with this VBA, is to sort the data by date ascending. The problem is, that the date format is Portuguese (dd/mm/yyyy).
    I found some logic on the internet, that works on the date, but it only sorted column A and not all columns A - Q.
    I tried various combinations of setting the range for the sort, but whenever it on the .Apply statement of the logic it triggers an error.
    I was googling some more but have not found a solution yet.
    I am hoping you could look at the code and tell me where the logic is wrong.
    Thanks again for your help.
    Gerd

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    You need to include the columns you wish to move with the sort range to make them move
    only a slight change, add or amend the red in your code

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-22-2021
    Location
    Portugal
    MS-Off Ver
    Excel 2021 Mac
    Posts
    78

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Thanks for your help, davsth.
    I actually had tried this but only with rng (without 1), but all my values changed to dates, but with rng1 it works correctly.

  10. #10
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    if you formatted the dates first so moved this line above the sort
    rng.NumberFormat = "dd.mm.yyyy;@"
    then redefined the rng as .Range("A9:Q" & lr) it would work the way you tried

  11. #11
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Slight simplification...

    Please Login or Register  to view this content.
    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 star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  12. #12
    Registered User
    Join Date
    08-22-2021
    Location
    Portugal
    MS-Off Ver
    Excel 2021 Mac
    Posts
    78

    Re: Copy every 2nd row to the end of the previous row, then delete every second row

    Thank you very much for your code.
    I sure was even more suprised when I saw how "dense" the code is.
    I do love this forum for the excellent help I got here in the past and for this specific VBA.
    Every time I asked for help, I got code I could not have figured out myself, but reading through it and then executing it step by step I see (and learn) what the code does.
    When I saw this line ".Columns(5).Resize(, 13).Value = .Columns(5).Resize(, 13).Value" I first thought it looks like a mistake, wondering what it does, but when stepping through the logic I saw its changing the meter readings from text to numbers.
    And I sure like how you did the formatting of the date and doing the final sort.
    The only change I made was changing ".Range("A8:J" to .Range("A8:Q" " as columns L - Q were not sorted.
    Thanks again for your help (and making my life a bit easier with my main calculation excel).
    Gerd
    And I sure will leave a nice comment for you.
    Last edited by gerdgoebel; 02-21-2024 at 02:33 PM.

+ 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. [SOLVED] Delete All Rows For Previous Months
    By salmasaied in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-13-2023, 03:48 PM
  2. [SOLVED] VBA: Copy data from every other row, paste it in previous row, delete copied row
    By JohnJames in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-21-2017, 11:16 AM
  3. 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
  4. [SOLVED] Find certain word, copy and paste cell in previous row if previous row is blank
    By steven_e in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-19-2013, 12:53 PM
  5. Delete previous weeks file?
    By homer09001 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2013, 10:59 AM
  6. 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
  7. [SOLVED] Delete row containing all previous values.
    By mohd21uk via OfficeKB.com in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 05-10-2006, 09:00 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