+ Reply to Thread
Results 1 to 18 of 18

Delete data in duplicate cells and replace text with last 4 digits

  1. #1
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Delete data in duplicate cells and replace text with last 4 digits

    Good morning Forum,

    I need your help today to finalize a project and that would be a wonderful Christmas gift !

    I am including a sample file that explains exactly what I want to achieve.

    First, I need to replace some of my column headers with only the 4 last numbers of said headers.

    Second, I have a code that copies a row as many times as needed, according to the year columns with data. Example: if I have amounts in the 2024 and 2025 columns, the code will create a second row so I have 2 rows of the same ID.
    What I need is to delete the duplicate amounts for that same ID. See the attached file, it will be clearer...

    If needed, here is the code I use to duplicate the rows with more than 1 year/amount:

    Please Login or Register  to view this content.
    Thank you so much in advance for your help and have a very great day!
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: Delete data in duplicate cells and replace text with last 4 digits

    In your code, col has the number 131. That seems unlikely as there are no data that far to the right.
    Further, I don't understand how the amounts ( 447.877 $ and twice 537.452 $) were calculated. Is there a formula for that or is this randomly chosen?
    Regarding the years in row 8, is it not easier to just type the correct year?

  3. #3
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Hello Kaasplankje,

    My original spreadsheet is far larger than the sample I created for this thread.

    In column 131, I calculate the number of year columns with amounts, and the code creates the duplicate rows according to it.

    The amounts are not formula, they are entered manually BEFORE I run the code to duplicate the rows.

    Row 8: no, the replacement needs to be a part of the code. This spreadsheet is received from an external collaborator and I am building an formatting and "adjusting" macro.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Delete data in duplicate cells and replace text with last 4 digits


    Tabernacle Sybille !

    Quote Originally Posted by Sybille View Post
    First, I need to replace some of my column headers with only the 4 last numbers of said headers.
    According to your attachment just with this single VBA codeline : [D8:H8] = [IF({1},"'"&RIGHT(D8:H8,4))] !

  5. #5
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Hey Marc

    Thank you but could you be a little more explicit with your codeline?

    Thank you!

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Delete data in duplicate cells and replace text with last 4 digits


    According to your attachment this is just a codeline to include in your VBA procedure
    in order to update the D8:H8 headers as requested
    just reproducing what any Excel beginner can easily achieve with cells formulas …

  7. #7
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Marc, I didn't know you could just put a code line starting with [YourRange] like that. You just taught me something.
    This part of the code is solved thank to you .

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Delete data in duplicate cells and replace text with last 4 digits


    It's the short syntax of the Evaluate method, a VBA help must see !

  9. #9
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Please have a look at the attached file and tell me if this is how it should behave. Changed col to 13 to convenience and added preceding years for routine to work.

    20221202.xlsm
    Last edited by kaasplankje; 12-02-2022 at 12:58 PM.

  10. #10
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Thank you very much kaasplankje

    However, I am not the queen of VBA and I just know enough to work around my needs, picking stuff here and there on internet.

    I tried your code and I need your help to correct the errors below:

    1) the code correcting the column headers should apply only to 5 columns (from D to H in my example, and from DQ to DU in my original spreadsheet). Your code changes all the column headers from column 2 to the last column. I was able to correct the starting column but not the "until the last column" part:

    2) when I run the code on my original spreadsheet, I must miss an adjustment because the second part of the code, where the amounts are adjusted in the duplicate rows, applies to the column B, C, D of my original spreadsheet, instead of the range from DQ to DU

    3) I had to declare the variables, but I need you to confirm they are correct


    Here are the changes I made:

    Please Login or Register  to view this content.
    Thank you again!

  11. #11
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: Delete data in duplicate cells and replace text with last 4 digits

    1) Replace Set yearrange = datasheet.Cells(8, 121).Resize(, datasheet.Cells(8, 121).End(xlToRight).Column) by Set yearrange = datasheet.Cells(8, 121).Resize(, 5)

    2) Make sure fy_column is set to 121 and ly_column to 126, and remove the lines that may change those values

    3) You only need to declare your variables if you work in Option Explicit (top row= in your macro). It's relevant for speed/avoiding typos but not for functionality. Anyway, what you have typed seems right.

  12. #12
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Hello Kaasplankje,

    So I tried to modify the code according to your last post. The first one, correcting the column headers, is solved.

    I still have the same problem with the amount distribution though, and maybe I did not change the code correctly.

    Here it is:

    Please Login or Register  to view this content.
    And it still "distributes" columns B, C and D values, instead of DS, DT and DU (for this specific example).

    I attach screen shots so you can visualize the error.

    Thank you so much for your time and help.
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Try this:

    Please Login or Register  to view this content.
    Note that this routine is not dynamic as we have now manually entered which columns it concerns.

  14. #14
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Good morning kaasplankje,

    The new code doesn't work either. The amounts are copied except the last year.

    However, I discovered something with the second version, with

    Please Login or Register  to view this content.
    See the image attached. The first project GOC3533196 is wrong (amounts disappear), but the second GOC4491081 is correct (amounts correctly distributed).

    Screenshot 2022-12-08.png

    How come?

    Have a very good day !

  15. #15
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: Delete data in duplicate cells and replace text with last 4 digits

    For this erroneous rows, what does col 131 read?
    Are the columns before DQ affected?

  16. #16
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Good morning kaasplankje,

    The columns before DQ are not affected.

    Before I run the code, GOC3533196 row shows 3 in col 131 and GOC4491081 shows 2

    After I run the code, GOC3533196 row shows 0 in col 131 and GOC4491081 shows 1

    For your information, the formula in col 131 is as follows:
    Please Login or Register  to view this content.
    Since the GOC3533196 amoutns disappear instead of being distributed, it is normal that the result show 0.

    However I just realized that this portion of the programming code could use some more in-depth work and I'm going to remove it from the current code. If needed I'll open another thread about it.

    I therefore consider that you have solved my request since the fist part works perfectly.

    Thank you very much for your patience and time and have a very happy holiday season.

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Delete data in duplicate cells and replace text with last 4 digits


    Quote Originally Posted by Sybille View Post
    If needed I'll open another thread about it.
    Then crystal clear explain the logic to follow without anything to guess, with details, samples, …

  18. #18
    Forum Contributor
    Join Date
    10-09-2018
    Location
    Quebec, Canada
    MS-Off Ver
    Office 365
    Posts
    125

    Re: Delete data in duplicate cells and replace text with last 4 digits

    Quote Originally Posted by Marc L View Post

    Then crystal clear explain the logic to follow without anything to guess, with details, samples, …
    Promise! I'll give all the details needed!

+ 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] Macro to duplicate a line but replace data in cells from a comment column
    By Kalamari in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-26-2021, 09:30 AM
  2. [SOLVED] Replace and delete certain text in the selected cells
    By Dunno123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-06-2020, 06:37 PM
  3. [SOLVED] Delete Duplicate Data from certain cells in specific columns
    By telcocook in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-18-2017, 07:40 AM
  4. macro for replace and delete text chain in several cells
    By alesiracer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-18-2014, 06:58 PM
  5. Replies: 2
    Last Post: 05-02-2013, 08:01 PM
  6. Find duplicate text in cells in column, then give names of cells containing duplicate text
    By spelunkerforlife in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-19-2013, 10:13 AM
  7. Replies: 2
    Last Post: 06-26-2010, 06:59 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