+ Reply to Thread
Results 1 to 7 of 7

Macro to copy/paste values to new row OR overwrite existing row

  1. #1
    Registered User
    Join Date
    07-29-2021
    Location
    Charleston, SC
    MS-Off Ver
    Microsoft Office 365
    Posts
    6

    Question Macro to copy/paste values to new row OR overwrite existing row

    Just signed up for this and having trouble finding a solution to the challenge I have at hand. I'm hoping someone can help, and I apologize if there's already been a solution posted, but I wasn't able to find it, so here goes!

    I have a weekly report that will be used to collect data for an asset, and I have built a simple macro to copy & paste the values of that data to a new row on a separate sheet. The purpose is to keep a log of weekly stats so we can identify trends. The macro is working as intended, but I want to improve it to be able to account for a certain element of human error.

    It is reasonable to assume that the people filling out the weekly report will at one point or another (or maybe more often than that) click the "Finalize Report" button which cues the macro to publish the data to a new row, then shortly thereafter realize that they need to correct some data and "re-finalize", thus re-publishing the data to a new row. This would result in multiple rows of data for the same date. The sheet where this is happening is password protected by design so that it can't be altered. I would like to redesign the macro to paste the data to a new row ONLY if the value in column A (the date of the report) is unique, and if the value in column A already exists in the list of data, then overwrite that row with the new data.

    This seems like something that's possible, but I'm fairly new to macros, so I could use some help!

    Here's the macro code as-is, which simply copies and pastes the values to a new row:

    Please Login or Register  to view this content.
    Last edited by alansidman; 07-29-2021 at 07:18 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,900

    Re: Macro to copy/paste values to new row OR overwrite existing row

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    07-29-2021
    Location
    Charleston, SC
    MS-Off Ver
    Microsoft Office 365
    Posts
    6

    Re: Macro to copy/paste values to new row OR overwrite existing row

    Thank you Alan! Much appreciated, and I'll make note of that for future posts. Hoping one of the geniuses on this forum can help me solve this problem!

  4. #4
    Registered User
    Join Date
    07-29-2021
    Location
    Charleston, SC
    MS-Off Ver
    Microsoft Office 365
    Posts
    6

    Re: Macro to copy/paste values to new row OR overwrite existing row

    Just had a thought... it wouldn't necessarily need to "overwrite" the existing row. Alternatively, it could delete the first of two rows which are have a duplicate value in column A. That might be easier to code. I still don't know how to do it, but just another direction to go with it! Hoping someone can help.

  5. #5
    Registered User
    Join Date
    07-29-2021
    Location
    Charleston, SC
    MS-Off Ver
    Microsoft Office 365
    Posts
    6

    Re: Macro to copy/paste values to new row OR overwrite existing row

    Okay so I figured it out! I found some VBA code to remove duplicates. It removes the 2nd one and keeps the 1st. The data is being copied from the 1st row, so what I did was put the Remove Duplicates code before the copy/paste code to the new row. This way it's not actually "overwriting" the row with the same date, but instead noticing there's already an entry for that date and removing it ahead of pasting the updated version of the data for that date. Then I did a sort so that all the rows are in chronological order, even if restating data from a previous date.

  6. #6
    Registered User
    Join Date
    06-15-2023
    Location
    London, England
    MS-Off Ver
    365
    Posts
    4

    Re: Macro to copy/paste values to new row OR overwrite existing row

    Quote Originally Posted by kpfoote811 View Post
    Okay so I figured it out! I found some VBA code to remove duplicates. It removes the 2nd one and keeps the 1st. The data is being copied from the 1st row, so what I did was put the Remove Duplicates code before the copy/paste code to the new row. This way it's not actually "overwriting" the row with the same date, but instead noticing there's already an entry for that date and removing it ahead of pasting the updated version of the data for that date. Then I did a sort so that all the rows are in chronological order, even if restating data from a previous date.
    Could you please share the entire VBA code you have used please? I am trying to achieve the same now

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,900

    Re: Macro to copy/paste values to new row OR overwrite existing row

    @will.good

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

+ 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] Copy existing (hidden) worksheet and replace/overwrite existing worksheet in same workbook
    By Funkymonkey0073 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-11-2020, 08:44 AM
  2. Macro to copy and paste without overwriting existing data
    By jmack36 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-18-2016, 07:49 AM
  3. Deleted
    By MaxStrong in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-11-2015, 01:50 AM
  4. Copy paste data without overwritting existing values
    By GusAlmeida in forum Excel General
    Replies: 2
    Last Post: 01-15-2015, 02:19 AM
  5. Replies: 11
    Last Post: 11-04-2013, 04:32 PM
  6. Replies: 3
    Last Post: 10-14-2013, 03:06 PM
  7. [SOLVED] Change existing MACRO to only copy/paste Values
    By williampdx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-21-2012, 08:07 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