+ Reply to Thread
Results 1 to 35 of 35

How to use macro to generate files based on conditions?

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

    How to use macro to generate files based on conditions?

    Hi all,

    Hope all are doing good.

    Not sure if this is complex and challenging, but i will try to explain in full detail.

    So i need help with a macro which will be placed in personal workbook so that it can be run in any file.

    To explain briefly at first, there is 1 source workbook, 3 target workbooks and based on these aforementioned files, 2 destination workbooks need to be generated

    The data is to be considered from source workbook, then find its relevant data in multiple target workbooks (wherever the data is present) and finally give the result in 2 destination workbooks in CSV format.

    Source workbook and target workbooks will be open before macro is run. Macro will be run in source workbook. Finally 2 destination workbooks need to be created in CSV format and automatically save with a name followed by current date in a destination folder.

    Source workbook =

    Extract

    Target Workbooks =

    EMVS Alerts Log - CEE 2024
    EMVS Alerts Log - DACH 2024 and
    EMVS Alerts Log - Benelux 2024


    Destination workbooks (CSV) =

    import_alert_status_change_template (10-03-2024)

    import_investigation_status_change_template (10-03-2024)



    The value to be searched in file Extract is starting from row 2, column 1, first text up to semicolon i.e. "DE-92195aa3-e4e0-4a5d-91ea-60fc4c67af80"

    This value need to be searched under column AE (UNIQUE_ALERT_ID) in target workbooks. Against this Alert ID, based on some column values, 2 CSV files need to be created automatically.


    Also, the oldest date in file "Extract", which is after the Alert ID (between 1st and 2nd semicolon) shall be considered while searching such that the macro should start searching in target files one day before this date.

    Example: If the oldest date in file Extract is 2nd March 2024, the macro should start searching from 1st March 2024 in target files under column A.

    However, one issue here is that date format in file Extract is MM/DD/YYYY but in target files will be DD/MM/YYYY.


    1st CSV file:

    This shall always contain "Alert ID;Investigation Status" in row 1, column 1.

    From row 2, it shall contain the alert IDs followed by either "Root cause on my side" or "No root cause on my side"

    "Root cause on my side" or "No root cause on my side" will be populated by value under column AK in target workbooks.

    If its GSK, then "Root cause on my side",

    if its Non-GSK, then "No root cause on my side"

    Example:

    Alert ID in extract "DE-92195aa3-e4e0-4a5d-91ea-60fc4c67af80" in file "EMVS Alerts Log - DACH 2024" has "Non-GSK"under column AK. Hence the value in CSV file should be

    DE-92195aa3-e4e0-4a5d-91ea-60fc4c67af80;No root cause on my side

    Similarly for other Alert IDs.

    Now this file should be saved as "import_investigation_status_change_template" followed by current date and saved in Downloads location of This PC.
    Example: import_investigation_status_change_template (10-03-2024)


    2nd CSV file:

    This shall always contain "Alert ID;Status;Reason code;Custom reason" in row 1, column 1.

    From row 2, it shall contain the alert IDs followed by "Under Investigation" and "RC-001" always, separated by semicolon.

    Finally, it will contain the value under column AI in target workbooks against the Alert ID.

    Example:

    Alert ID in extract "DE-92195aa3-e4e0-4a5d-91ea-60fc4c67af80" in file "EMVS Alerts Log - DACH 2024" has "Pack active in ATTP, decommissioned elsewhere" under column AI. Hence this value in CSV file should be

    DE-92195aa3-e4e0-4a5d-91ea-60fc4c67af80;Under Investigation;RC-001;Pack active in ATTP, decommissioned elsewhere

    Similarly for other Alert IDs.

    Now this file should be saved as "import_alert_status_change_template" followed by current date and saved in Downloads location of This PC.

    Example: import_alert_status_change_template (10-03-2024)


    The target workbooks contain column by name AMS Status under column AZ. Once an alert ID from file extract was found in target workbooks, then this column should be populated as Uploaded.


    Finally, the rows 2 to 26 in file extract were found in the target workbooks and have value as Uploaded in target files under column AZ. So these rows should be deleted while others should remain in the file.

    Also, from next time, when the macro is run, if the Alert ID in file "Extract" contains "Uploaded" in target file , then that row same shall be deleted from file Extract and others should be searched.


    Note:

    1. All target workbooks shall be open before running macro. If any of the workbook is not open, it should give a pop-up with the file name that is not open such as "EMVS Alerts Log - CEE 2024 is not open"

    2. Target workbooks will always have same file names and sheet names. Sheets will always be password protected with "abc123"

    3. In actual, there are 8 target workbooks. So i will add them in macro later on.


    A query:

    The target workbooks are placed on teams/sharepoint. Is it possible that if the link is entered in macro, then the macro should automatically search the data within them without the need of keeping all these files open.


    Can someone please help with the macro?
    Last edited by rizwanulhasan; 03-19-2024 at 07:23 PM.

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

    Re: How to use macro to generate files based on conditions?

    Due to file count limitation, attaching 1 more destination workbook in this post
    Attached Files Attached Files

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

    Re: How to use macro to generate files based on conditions?

    The notes are long because i have tried to explain in detail although the requirement is short i guess

    Awaiting support

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

    Re: How to use macro to generate files based on conditions?

    Help please

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: How to use macro to generate files based on conditions?

    Quote Originally Posted by rizwanulhasan View Post
    the requirement is short i guess
    The requirement is not short. This would require a lot of work and it looks to me like you are having a hard time finding someone willing to do it for free.

    You may want to consider offering payment in our Commercial Services forum.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

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

    Re: How to use macro to generate files based on conditions?

    Open new workbook and paste the code then save it as .xlsm file.
    run test
    Please Login or Register  to view this content.
    Last edited by jindon; 03-12-2024 at 07:17 AM.

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

    Re: How to use macro to generate files based on conditions?

    @jindon

    Many many thanks for the response

    I did as instructed. Pasted code in new workbook and saved as .xlsm

    Also changed the paths but get below error

    Capture1.JPG

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

    Re: How to use macro to generate files based on conditions?

    That change is impossible
    Remove ThisWorkbook.Path &

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

    Re: How to use macro to generate files based on conditions?

    First one cleared. Got one more
    Attached Images Attached Images

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

    Re: How to use macro to generate files based on conditions?

    Again....
    Remove ThisWorkbook.Path &

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

    Re: How to use macro to generate files based on conditions?

    Quote Originally Posted by jindon View Post
    Invalid picture.

    Post the line(s) that hou got the error, not a picture.
    sorry. reuploaded.

    Please Login or Register  to view this content.

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

    Re: How to use macro to generate files based on conditions?

    Quote Originally Posted by jindon View Post
    Again....
    Remove ThisWorkbook.Path &
    ...............

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

    Re: How to use macro to generate files based on conditions?

    Done. Modified beginning part of macro as below

    Please Login or Register  to view this content.
    Ran macro but now nothing happens
    Last edited by rizwanulhasan; 03-12-2024 at 05:42 AM.

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

    Re: How to use macro to generate files based on conditions?

    Have you checked DownLoads folder?

    If not csv in downloads folder then add one line before End Sub.
    Please Login or Register  to view this content.
    and run again.

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

    Re: How to use macro to generate files based on conditions?

    my apologies. i missed to check Downloads folder.

    Yes, the files exist

    Few issues. Below requirements not met.

    The target workbooks contain column by name AMS Status under column AZ. Once an alert ID from file extract was found in target workbooks, then this column should be populated as Uploaded.


    Finally, the rows 2 to 26 in file extract were found in the target workbooks and have value as Uploaded in target files under column AZ. So these rows should be deleted while others should remain in the file.

    Also, from next time, when the macro is run, if the Alert ID in file "Extract" contains "Uploaded" in target file , then that row same shall be deleted from file Extract and others should be searched.

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

    Re: How to use macro to generate files based on conditions?

    Also, as mentioned earlier, can i use teams link instead of local drive link because the target files are on teams. Can i modify code as below


    Dim cn As Object, rs As Object
    Dim myDir As String, e, msg As String, wsName As String, st(1)
    myDir = "https://myteams.alerts.com/sites/Serialisation Governance/Business Process Support/EU Data Errors in ATTP EMVS/Alerts/Alerts logs 2024/" '<--- change actual folder path where all workbooks reside
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

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

    Re: How to use macro to generate files based on conditions?

    Code in #6 has been updated.
    Now it overwrites "Extract.csv", so you'd better make copy of it before you run.

    As I haven't used "Team", no idea about the file path etc.
    Last edited by jindon; 03-12-2024 at 07:22 AM.

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

    Re: How to use macro to generate files based on conditions?

    In actual, the rows that were found in target files should be deleted and the ones which were not found shall be retained. Looks like its working opposite.

    Also, instead of giving path for extract file, cant it be run directly in active workbook. Because as mentioned in post # 1, the macro will be run in source file i.e. Extract.csv because this extract can be in any folder. So why not run code directly in active workbook rather than changing path each time.

    Below requirement not working yet

    The target workbooks contain column by name AMS Status under column AZ. Once an alert ID from file extract was found in target workbooks, then this column should be populated as Uploaded.

    Also, from next time, when the macro is run, if the Alert ID in file "Extract" contains "Uploaded" in target file , then that row same shall be deleted from file Extract and others should be searched.
    Last edited by rizwanulhasan; 03-12-2024 at 08:28 AM.

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

    Re: How to use macro to generate files based on conditions?

    Why not???

    Are you opening csv from excel and paste the code and run each time you need???

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

    Re: How to use macro to generate files based on conditions?

    I have pasted code in Personal workbook. But when i run code, it searches the path for Extract.csv file. If the file is not in that folder, it gives below error.

    Since the extract file will be downloaded from a tool daily multiple times, its not feasible to save it each time in the same path with same file name. Hence the workaround is that i will open tool, download the extract (it can be of any name and in any folder) and then use macro located in Personal workbook. So the macro should consider the active workbook as the source workbook. Hope i was able to explain.
    Attached Images Attached Images

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

    Re: How to use macro to generate files based on conditions?

    Please Login or Register  to view this content.

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

    Re: How to use macro to generate files based on conditions?

    Hi jindon,

    Thanks again for you efforts and response.

    This macro works differently than previous one. Here i need to keep target workbooks open. The data is fetched and the target workbooks are closed which is good by the way. However, below issues noted.

    1. Current: Once the macro is run, open dialog box pops up asking to select the file.
    Required: The macro should run in active workbook.

    2. The destination workbooks cannot be found in Downloads folder

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

    Re: How to use macro to generate files based on conditions?

    Jindon, sorry but 1 valuable suggestion required from you.

    Firstly, let me explain the situation. The current 3 target files will have around 2000 entries daily and as a result the data in the files will be huge. Currently, when the alert ID in file "Extract" is being searched in 3 target files, it will search from the beginning. In such a case, there are almost 9 target files and this will consume a lot of time. In fact, there will be increment in time each day.

    So is there any way you can suggest to overcome this issue?

    I thought of one but not sure if its correct so would like to seek your advice - The commoner between two files is the date. So if the oldest date in file "Extract", which is after the Alert ID (between 1st and 2nd semicolon) is considered while searching, then macro should start searching in target files one day before this date.

    Example: Oldest date in file Extract is in row 2 i.e. 2nd March 2024. So the macro should start searching from 1st March 2024 in target files under column A.

    However, one issue here is that date format in file Extract is MM/DD/YYYY but in target files will be DD/MM/YYYY. Can you help please
    Attached Files Attached Files

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

    Re: How to use macro to generate files based on conditions?

    Hi Jindon,

    Can you please help

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

    Re: How to use macro to generate files based on conditions?

    Quote Originally Posted by rizwanulhasan View Post
    Hi jindon,

    Thanks again for you efforts and response.

    This macro works differently than previous one. Here i need to keep target workbooks open. The data is fetched and the target workbooks are closed which is good by the way. However, below issues noted.

    1. Current: Once the macro is run, open dialog box pops up asking to select the file.
    Required: The macro should run in active workbook.

    2. The destination workbooks cannot be found in Downloads folder

    Ok, now i got it why destination files are not generating. Its because instead of deleting rows in file extract, the macro is deleting the rows in target files. The Alert IDs which are present in file extract are being deleted in target files. Hence the macro generates destination files only once and not the next time.


    @jindon

    Can you please check this issue and the points in post # 22 and 23
    Last edited by rizwanulhasan; 03-23-2024 at 05:18 AM.

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

    Re: How to use macro to generate files based on conditions?

    Hi jindon,

    Your help here will be greatly appreciated.

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

    Re: How to use macro to generate files based on conditions?

    Quote Originally Posted by rizwanulhasan View Post
    Ok, now i got it why destination files are not generating. Its because instead of deleting rows in file extract, the macro is deleting the rows in target files. The Alert IDs which are present in file extract are being deleted in target files. Hence the macro generates destination files only once and not the next time.

    If not other points, but just this 1 point is rectified, then i can atleast use macro

    Can you help

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

    Re: How to use macro to generate files based on conditions?

    Hi.

    Can someone help

  29. #29
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,885

    Re: How to use macro to generate files based on conditions?

    Administrative Note:



    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not told us about this. You are required to do so. Cross-posts are allowed but you must provide a link to your posts on other sites.

    Please see Forum Rule #7 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important: https://excelguru.ca/a-message-to-forum-cross-posters/

    (Note: this requirement is not optional. No help to be offered until you provide a link or, for members with fewer than 10 posts, a comment telling us where else you have posted this query.)
    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

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

    Re: How to use macro to generate files based on conditions?

    @alansidman

    Hi Alan,

    Thanks for calling this out.

    I did provide a link in other forum to the original post rather than creating a new one, however, i missed to inform here. Apologies for the same.

  31. #31
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: How to use macro to generate files based on conditions?

    Hi Alan, Thanks for calling this out. I did provide a link in other forum to the original post rather than creating a new one, however, i missed to inform here. Apologies for the same.
    Why not also post the links here!

    Like Jeff pointed out, this is a free forum outside of the paid commercial services. Posting over and over again for somebody to help is not going to help especially if that person is not available at this moment. Again, being this is free, be patient or please pay for some help in the commercial services.
    HTH
    Regards, Jeff

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

    Re: How to use macro to generate files based on conditions?

    Ok Jeff, noted. Sorry for the miss. Sharing link now

    https://forums.excelguru.ca/threads/...ditions.11839/

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

    Re: How to use macro to generate files based on conditions?

    Or instead of deleting rows, can it be possible that another CSV file be created (Pending.CSV) for the alert IDs that are present in Extract file but not found in target files
    Last edited by rizwanulhasan; 03-27-2024 at 03:47 AM.

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

    Re: How to use macro to generate files based on conditions?

    Hi all,

    I am using below macro and its running perfectly.

    Please Login or Register  to view this content.

    Omitting the auto deletion part, can there be a modification in macro such that:

    If any of the alert IDs in source file "Extract" were not found in target files, then,

    Create an excel file with name as "missing_alerts" followed by current date and save in Downloads location of This PC. Example. missing_alerts (10-03-2024)

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

    Re: How to use macro to generate files based on conditions?

    Hi all,

    An issue with the code i am using in post # 34. Perhaps in lines highlighted in red:

    Please Login or Register  to view this content.
    The 2nd generated CSV file in actual is containing " at the beginning and end of a cell. Snip below for clarification.

    As a result, the files are failing to upload in the respective portal. Can someone please suggest how it can be rectified
    Attached Images Attached Images

+ 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] generate email to different recipients based on conditions
    By krvh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-23-2023, 04:40 AM
  2. Replies: 18
    Last Post: 11-27-2018, 05:25 AM
  3. Macro to copy data from 1 worksheet to another based on conditions and split files
    By Khalidngo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2018, 12:33 AM
  4. [SOLVED] Generate PDF files based on the content
    By chathuranga in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 12-29-2016, 07:33 AM
  5. Generate a Table based on several conditions
    By steven340 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-09-2015, 08:44 PM
  6. Macro or addin to generate buy, sell and stop signals under certain conditions
    By babapusy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-30-2013, 07:38 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