+ Reply to Thread
Results 1 to 31 of 31

How to use macro to fetch and replace values in columns?

  1. #1
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    How to use macro to fetch and replace values in columns?

    Hi everyone,

    Hope all's well.

    For the attached file, i need a macro which can perform the below steps one after another.

    1. Column I in sheets "Batches" is not considered as date due to ".". The same is to be changed to "-"

    2. Column K in sheets "Batches" is to be fetched from sheet "Locations" using VLOOKUP. Formula entered in cell K4.

    3. Column K in sheet "Batches" need to be replaced as per the tables "Site" and "CMO" in sheet "Pack Owner". If value in column K matches with columns A and D in sheet "Pack Owner", then the same should be replaced by values in columns B and E respectively. In short, i want the values in column K in sheet "Batches" should exactly match with the values in column U of sheet "Closed"

    4. Column J in sheet "Batches" need to be fetched from sheet "Pack Owner". In case the value in column K in sheet "Batches" falls under column B of sheet "Pack Owner", then column J should populate Site. If it falls under Column E of sheet "Pack Owner", then CMO is to be populated.

    Note: Sheets "Locations" and "Pack Owner" will always be hidden and locked with pass Abc@123.

    Can someone please help ?


    Detailed info as below, if required.

    Lemme give a detailed explanation.

    The data in sheet "Batches" will be pasted by downloading from an external source. The raw data will contain only the columns A to I.

    Now I need to compare data in sheets "Batches" and "Closed" based on below 2 common criteria

    Site and
    Month

    i.e.

    1. Column K in sheet "Batches" and column U in sheet "Closed"

    and

    2. Column M in sheet "Batches" and column Q in sheet "Closed"


    Hence i created additional columns in sheet "Batches" i.e. columns J to M.

    Now to fulfill the first criteria, i need site name. This is fetched into column K from sheet "Locations" based on column H. However, the values will not exactly match and so there couldn't be a comparison between the 2 sheets. Hence, there's a need to duplicate the names and so i created 2 tables in sheet "Pack Owner".

    The columns A and D in this sheet contains names from column B in sheet Locations and columns B and E in this sheet contains names as they are in column U of sheet "Closed". This will align the names in both sheets so that i can compare the data.

    This completes the points 3 and 4 in my 1st post.

    Last point 4 is to populate column J to know whether the value in column K is a Site or CMO. This can be identified from the columns B and E in sheet "Pack Owner". All the ones under column B are Site while under column E are CMO.
    Attached Files Attached Files
    Last edited by rizwanulhasan; 07-30-2023 at 02:25 AM.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,253

    Re: How to use macro to fetch and replace values in columns?

    3. Column K in sheet "Batches" need to be replaced as per the tables "Site" and "CMO" in sheet "Pack Owner". If value in column K matches with columns A and D in sheet "Pack Owner", then the same should be replaced by values in columns B and E respectively. In short, i want the values in column K in sheet "Batches" should exactly match with the values in column U of sheet "Closed"
    Check you have not got A & D vs B & E the wrong way round !

    And much easier if you reversed A/B and D/E (to use VLOOKUP rathet than Index/Match)

    Data volumes : max number of rows?
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Hi John,

    Appreciate your response.

    Apologies for the confusion.

    The problem is that i want the values in column K in sheet "Batches" should exactly match with the values in column U of sheet "Closed".

    Column K in sheet "Batches" will contain values either from column A or D in sheet "Pack Owner". So to match with Column U of sheet "Closed", they should be replaced with columns B and E of sheet "Pack Owner".

    Ex. if value in column K is as in A7 in column A, i.e. DHL Knoxville, then it should be replaced with value in column B i.e. DHL

    so DHL Knoxville = DHL

    Similarly with column D and E.

    Sorry for the data. Deleted some and reattached file in 1st post.

    Hope i was able to explain

  4. #4
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,253

    Re: How to use macro to fetch and replace values in columns?

    Apologies : my error misreading a table!!

    Data volumes ?

  5. #5
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    no worries.

    Data Volumes = you mean i should delete more rows?

    I already deleted and reattached file in 1st post

  6. #6
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,253

    Re: How to use macro to fetch and replace values in columns?

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by JohnTopley; 07-28-2023 at 01:48 PM.

  7. #7
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Hi John,

    Many thanks for the help.

    Just few issues.

    Macro doesn't fulfill the point 1 mentioned in first post.

    VLOOKUP is being applied in column J instead of column K.

    Column J doesn't show Site/CMO.

    You can run macro in attached file.

    Can you please check

    Many thanks in advance.
    Attached Files Attached Files
    Last edited by rizwanulhasan; 07-29-2023 at 03:09 AM.

  8. #8
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,253

    Re: How to use macro to fetch and replace values in columns?

    See attached:
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    All good except for point 1 and 4

  10. #10
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,253

    Re: How to use macro to fetch and replace values in columns?

    Don't understand 4,

    Column J: Barnard Castle (Pack owner ?)

    Column K: BARNARD CASTLE (Site/CMO)

    Same for PCI Rockford (J) PCI ROCKFORD (K)



    1. Needs to addressed at source: why is there "." ? Find/Replace will address by date will be dd/mm/yyyy (as per file)

  11. #11
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Quote Originally Posted by JohnTopley View Post
    Don't understand 4,

    Column J: Barnard Castle (Pack owner ?)

    Column K: BARNARD CASTLE (Site/CMO)

    Same for PCI Rockford (J) PCI ROCKFORD (K)

    The above is working perfectly as per point 3. Now after this is achieved, 4th point is to be achieved as explained below.

    Column J should be populated only with Site or CMO. To decide what should be the value between the two, column K in sheet "Batches" should be searched in columns B and E in sheet "Pack Owner".

    If the value in column K in sheet "Batches" is in column B of sheet "Pack Owner", then column J should populate Site.

    If the value in column K in sheet "Batches" is in column E of sheet "Pack Owner", then column J should populate CMO.

    BARNARD CASTLE is in column B of sheet "Pack Owner" and hence column J should populate as SITE.

    PCI ROCKFORD is in column E of sheet "Pack Owner" and hence column J should populate as CMO.



    Quote Originally Posted by JohnTopley View Post
    Needs to addressed at source: why is there "." ? Find/Replace will address by date will be dd/mm/yyyy (as per file)
    The report i download gives date in 29.07.2023 format and not 29-07-2023. Hence the need to change . to -


    Hope i was able to clarify
    Last edited by rizwanulhasan; 07-29-2023 at 04:01 AM.

  12. #12
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: How to use macro to fetch and replace values in columns?

    Hi rizwanulhasan,

    to replace the dot, all that is needed is the following code to be inserted immediately after the start of the For loop:

    Please Login or Register  to view this content.
    but still I cannot understand the description: what is the relationship between "Location Name" and "Site/CMO".
    I would like to ask you if you could kindly clarify it better (maybe I misunderstood, but in Tabel3 no "Location Name" is matched with Site/CMO)

    Bye
    Max
    let's compare ideas

  13. #13
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Hi @maxpit

    Thanks for the above code. However, it changes the month for few cells such as I11. The month changes from June to September. Also it keeps some of the time format in 12hour and some in 24 hour format. Can you please check?


    Quote Originally Posted by maxpit View Post

    but still I cannot understand the description: what is the relationship between "Location Name" and "Site/CMO".
    I would like to ask you if you could kindly clarify it better (maybe I misunderstood, but in Tabel3 no "Location Name" is matched with Site/CMO)

    Bye

    Apologies. Perhaps i am confusing all. Lemme give a detailed explanation.

    The data in sheet "Batches" will be pasted by downloading from an external source. The raw data will contain only the columns A to I.

    Now I need to compare data in sheets "Batches" and "Closed" based on below 2 common criteria

    Site and
    Month

    i.e.

    1. Column K in sheet "Batches" and column U in sheet "Closed"

    and

    2. Column M in sheet "Batches" and column Q in sheet "Closed"


    Hence i created additional columns in sheet "Batches" i.e. columns J to M.

    Now to fulfill the first criteria, i need site name. This is fetched into column K from sheet "Locations" based on column H. However, the values will not exactly match and so there couldn't be a comparison between the 2 sheets. Hence, there's a need to duplicate the names and so i created 2 tables in sheet "Pack Owner".

    The columns A and D in this sheet contains names from column B in sheet Locations and columns B and E in this sheet contains names as they are in column U of sheet "Closed". This will align the names in both sheets so that i can compare the data.

    This completes the points 3 and 4 in my 1st post.

    Last point 4 is to populate column J to know whether the value in column K is a Site or CMO. This can be identified from the columns B and E in sheet "Pack Owner". All the ones under column B are Site while under column E are CMO.

    Sorry for the confusion.

  14. #14
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: How to use macro to fetch and replace values in columns?

    Hi rizwanulhasan,

    with Italian settings it works, with English settings it reverses month/day:

    Please Login or Register  to view this content.
    Try the above code and let me know.

    Bye

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,224

    Re: How to use macro to fetch and replace values in columns?

    Maxpit - FYI British English settings (and most other English settings) are the same as Italian. Only English US uses month - day - year.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  16. #16
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Quote Originally Posted by maxpit View Post
    Hi rizwanulhasan,

    with Italian settings it works, with English settings it reverses month/day:

    Please Login or Register  to view this content.
    Try the above code and let me know.

    Bye
    Sure, i'll try maxpit.

    However, may i know where exactly to put it in VBA?

    As mentioned by you earlier, i pasted it right after the For line, but it doesn't work

  17. #17
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,253

    Re: How to use macro to fetch and replace values in columns?

    Please Login or Register  to view this content.
    Attached Files Attached Files

  18. #18
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: How to use macro to fetch and replace values in columns?

    Hi rizwanulhasan,

    it is an oversight on my part
    Please Login or Register  to view this content.
    The above code as modified, in the highlighted part, must be inserted immediately after the start of the For loop.

    Bye

  19. #19
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Many thanks John.

    Points 3 and 4 are now achieved perfectly. Just 1 issue.

    The macro is taking a very very long time to run. I even tried restarting PC, but its still the same.

    Your code in post # 6 was completing in a second. However, this one is taking at least 3-4 mins and sometimes excel ends up in Not Responding

  20. #20
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Quote Originally Posted by maxpit View Post
    Hi rizwanulhasan,

    it is an oversight on my part
    Please Login or Register  to view this content.
    The above code as modified, in the highlighted part, must be inserted immediately after the start of the For loop.

    Bye
    Many thanks maxpit.

    This code works perfectly for the file in post # 8 and the code runs in a second.

    Now just the 4th point of post # 1 is to be achieved.

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,554

    Re: How to use macro to fetch and replace values in columns?

    1) Convert Col.K to serial date from string date and format the cell. As well as Col.J to the number, not string.
    2) OK.
    3) No change, since not match.
    4) No output as no match from 3).
    Please Login or Register  to view this content.
    Last edited by jindon; 07-29-2023 at 09:21 PM.

  22. #22
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Hi jindon,

    Thanks for your response.

    Quote Originally Posted by jindon View Post
    1) Convert Col.K to serial date from string date and format the cell. As well as Col.J to the number, not string.


    Sorry i didn't get your instructions

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,554

    Re: How to use macro to fetch and replace values in columns?

    No, that was not the instructions.

    That is what the code does.
    So, just run the code.

  24. #24
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Oh. Sorry i thought those as instructions.

    I ran code. 1 and 2 points works perfectly as per my first post but not 3 and 4.
    Attached Files Attached Files
    Last edited by rizwanulhasan; 07-30-2023 at 02:26 AM.

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,554

    Re: How to use macro to fetch and replace values in columns?

    3. Column K in sheet "Batches" need to be replaced as per the tables "Site" and "CMO" in sheet "Pack Owner". If value in column K matches with columns A and D in sheet "Pack Owner", then the same should be replaced by values in columns B and E respectively. In short, i want the values in column K in sheet "Batches" should exactly match with the values in column U of sheet "Closed"
    As I said already in my post #21, you don't have any matched data after vlookup in col.K with 2 tables in "Pack owner", so non change.

    If my understanding it not correct, you should upload the workbook with your result clearly showing the reason.

  26. #26
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    My apologies Jindon. I got your point now.

    All works now, except the 3rd point of my post 1.

    Column K in sheet "Batches" not being replaced as per columns B and E in sheet "Pack Owner"
    Attached Files Attached Files

  27. #27
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,554

    Re: How to use macro to fetch and replace values in columns?

    OOps, missed one line bold.
    Please Login or Register  to view this content.
    and one more change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Last edited by jindon; 07-30-2023 at 03:02 AM.

  28. #28
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    Please Login or Register  to view this content.
    i have added above line and everything's working perfectly.

    Do you still want me to make the below change i.e.
    Please Login or Register  to view this content.

  29. #29
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,554

    Re: How to use macro to fetch and replace values in columns?

    Because you have blank in Site A4 with the replace string of BORONIA in B4.

    That means blank in Col.K should be replaced with BORONIA.
    If not, you don't need to change.

  30. #30
    Forum Contributor
    Join Date
    01-01-2022
    Location
    India
    MS-Off Ver
    Home 2021
    Posts
    778

    Re: How to use macro to fetch and replace values in columns?

    the first code is fine. All's perfect now.

    Many many thanks @jindon @JohnTopley @maxpit

  31. #31
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: How to use macro to fetch and replace values in columns?

    glad to have been useful to you

+ 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] Lookup formula taking time to fetch values, need a fast macro
    By Pankaj jaswani in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2022, 03:13 AM
  2. [SOLVED] Multiple rows/columns values compare and fetch data
    By sarajun_88 in forum Excel General
    Replies: 9
    Last Post: 01-10-2022, 06:20 AM
  3. Replies: 3
    Last Post: 04-07-2016, 01:15 AM
  4. Replies: 1
    Last Post: 07-08-2015, 05:01 AM
  5. [SOLVED] Macro to scan rows and columns to replace values with a blank - Followup question
    By Vanth_2013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2015, 02:59 PM
  6. [SOLVED] Macro to scan rows and columns to replace values with a blank
    By Vanth_2013 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2015, 12:29 PM
  7. How to Fetch a table values in a webpage to Excel sheet using VBA Macro??? Help Me
    By ParthibanPalaniswamy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-20-2013, 05:08 AM

Tags for this Thread

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