+ Reply to Thread
Results 1 to 26 of 26

Looking for a code that will save as different names to folder based on data validation

  1. #1
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Looking for a code that will save as different names to folder based on data validation

    Hello, to put it as simple as possible, I am looking for a code that will select different choices from a drop-down menu and then save a copy of that tab to a folder.

    Here is a deeper explanation: There are two sheets in this sample workbook. The first one Sheet 1, contains a list of employee numbers, names, and a dept. code they appear in. It also has a key to the side giving a description of the dept. code. On sheet2, the dropdown menu is in cell B8 and is simply from the list in Sheet1 from the range L2 thru L5. When you select a Dept. Code, it will then bring up a matching dept description in B7 as well as a list of people who reside in that dept. starting in Cells A11:B40. So what I'd like to do is have a macro that will select each of these department codes and then save a copy of the file in a folder on the desktop called "Sample". For the file name, I would like it to be whatever is in B8 on sheet 2 and have B7 in the name. So for example if the first Choice in B8 called "Test1" is selected, the File name I would want saved in the folder would be "Test 1 - 1st Shift" and the next choice "Test2" would have a file saved called "Test 2 - 1st Shift Office" and so on and so forth. I've limited the sample to just 4 departments, but it would just basically need to stop at whatever the last dept. is.

    I'm not yet at this kind of proficiency in VBA but I believe this would be some kind of Loop coding? Thank you for your time!

    Also just FYI if it matters OneDrive is used.

    also posted here: https://www.mrexcel.com/board/thread...ation.1246044/
    Attached Files Attached Files
    Last edited by PitchNinja; 09-28-2023 at 03:31 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Hello @PitchNinja. If I understand you correctly, then here is an option for you.
    Please Login or Register  to view this content.
    Good Luck!

  3. #3
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Hi Mike, thanks for responding. So I went to run the procedure and I got a path not found run-time error. When i hit debug, it highlights the line that says MKDir folderpath. Is there a reference I maybe need to add? I have the following checked:
    *Visual Basic For Applications
    *Microsoft Excel 16.0 Object Library
    *OLE Automation
    *Microsoft Office 16.0 Object Library

    It's just a wild guess but I wasn't sure if maybe there was something I needed to enable there that was causing the run-time error.

  4. #4
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Sorry it posted this twice. I must have double-clicked reply on the previous
    Last edited by PitchNinja; 09-15-2023 at 09:35 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    I tried to reproduce your error and my code worked fine, without errors. Despite the fact that I deleted the folder on the desktop, it was created again without problems, without a single error. Are you by any chance trying to create this folder from this file that is in the public domain? Or are you logged in not as an Administrator but as a regular Windows user? If yes, then you cannot create this folder when working as a regular Windows user!

  6. #6
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Try this:
    Please Login or Register  to view this content.
    But I don't think this will save you.
    If you use Dir, the result will be represented as a variant and you may need to do an explicit type conversion. If you use Dir$, the result will be represented as a string, which can be more convenient if you need to work with strings specifically.
    Ensure that you have the necessary permissions: Make sure you have the required permissions to create directories in the specified location. Try creating this folder on another local drive on your computer.
    Last edited by MikeVol; 09-15-2023 at 10:11 AM. Reason: Update

  7. #7
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    I am not logged in as admin. I sign on to this computer here at work everyday, but as far as I am aware, it is not as admin.

  8. #8
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    I did try the revision you gave me but still the same error. I also tried downloading your file and doing it form there but got the same error. You said it worked for you, did you have 4 different files in your Sample folder then?
    Last edited by PitchNinja; 09-15-2023 at 10:14 AM.

  9. #9
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    did you have 4 different file
    No, one file is created in the folder depending on the selection in cells B8 (data validation). You wrote a lot in your initial message, but no one really understood. That’s why no one answered you for a long time.
    Try creating this folder on another local drive on your computer.
    ???
    Like for example:
    Please Login or Register  to view this content.
    Last edited by MikeVol; 09-15-2023 at 10:24 AM.

  10. #10
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Okay I think it's maybe a different code entirely then. I apologize. When I had said "So what I'd like to do is have a macro that will select each of these department codes and then save a copy of the file in a folder on the desktop called "Sample",
    I thought maybe it would have been understood from that sentence that I wanted multiple files saved which was why I thought some kind of loop was needed. But yeah that's what I am looking for. In this case because there are 4 different departments - Test1, Test2, Test3, and Test4 - I wanted the macro to select them one at a time and save each as it's own individual copy in the sample folder. So by that, we would have the following 4 files in the Sample Folder: "Test1 - 1st Shift", "Test2 - 1st Shift Office", "Test3 - 2nd Shift", and "Test4 - 3rd Shift". Hope that was clearer.

  11. #11
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    If I understand you correctly
    Please Login or Register  to view this content.
    Good luck!

  12. #12
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Thanks Mike, I tested this out on my home computer with Excel 2007 and changed the D:\Sample to C:\Sample and it worked. It did exactly what I was expecting - it created 4 files. I even added two more sample departments to test them and sure enough it created a total of 6 files. I will be out of the office next week from work so I won't get to test this on the computer there until I return the following week but it IS working on my Home computer, so fingers crossed that it will work when I get back. This is amazing though that this is possible - I figured there had to be some kind of loop in there and had "For Each" in my head but all the other added lines....yeah I got a WAYS to go there. I can't even imagine how long it took to come up with coding like this, so thank you very much and EXCELLENT work! We'll see when I get back how it goes

  13. #13
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    It didn’t take much time, we had our own things to do. Regarding your work computer, you will not be able to create a folder on the local drive C. This is a security policy, system administrators limit the actions of ordinary users. Otherwise there would be a complete mess on work computers. As an option, just create a folder on local drive D, which is what I recommended to you before. Well, or contact your system administrators. Good luck.

  14. #14
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Okay, I finally got caught up with my other stuff from being out of the office so that I could try this out. So it works in that it is saving files to the preferred location and it did save the files as the correct names, so that is very awesome! Looking closer however, I realized it is copying Sheet1, so let me try to clarify what I was after. I apologize as I see now how I could have communicated it better. It is ultimately Sheet2 that I need copied and saved to the location after each selection from the dropdown menu is selected on that sheet. Sheet1 basically serves as the lookup for Sheet2's data and wouldn't be copied/saved to a new workbook. Sheet 2 would basically be a personalized template for each department that gets printed out after it is saved under its different names. So in a step by step scenario with this specific sample, here is what I would hope to have after running the macro:

    On Sheet2
    1. Select the first dept. from dropdown (Test1) - Names are populated for people who belong to that dept.
    2. Copy Sheet2 - Save as new Workbook
    3. Save new workbook in specified location as the specified name
    4. Select the next dept. from the dropdown (Test2) - Names are populated for people who belong to that dept.
    5. Copy Sheet2 - Save as new Workbook
    6. Save new workbook in specified location as the specified name
    7. Select the next dept. from the dropdown (Test3) - Names are populated for people who belong to that dept.
    8. Copy Sheet2 - Save as new Workbook
    9. Save new workbook in specified location as the specified name
    10. Select the next dept. from dropdown (Test4) - Names are populated for people who belong to that dept.
    11. Copy Sheet2 - Save as new Workbook
    12. Save new workbook in specified location as the specified name

    Sheet2 is saved as 4 different files and if you open each one up, they would all of course look different from each other. We are close to the answer and I think maybe the worst of it - the saving to location - is out of the way and at least that is working. Just the Sheet2 portion. Again, I do apologize for not having communicated it better initially but hope that this is clearer and still doable? Thank you for the time you spent on this.

  15. #15
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Hello @PitchNinja. To be honest, I didn’t understand what you needed. In fact, the code works as you described in your post.

  16. #16
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    The macro was actually copying Sheet1 and making new files out of it. I think the coding is specific to what is on Sheet1. But it is actually Sheet2 that I need copied and made into a new file based on what is selected in the dropdown menu in B8. I will include my original post with edits to it bolded:

    Here is a deeper explanation: There are two sheets in this sample workbook. The first one Sheet 1, contains a list of employee numbers, names, and a dept. code they appear in. It also has a key to the side giving a description of the dept. code. Sheet1 simply contains Data for Sheet2 to look up, so we aren't overly concerned with this sheet. On sheet2, the dropdown menu is in cell B8 and is simply from the list in Sheet1 from the range L2 thru L5. When you select a Dept. Code in B8 of Sheet2, it will then bring up a matching dept description in B7(Taken from the data in M2:M5 of Sheet1) as well as a list of people who reside in that dept. starting in Cells A11:B40. So what I'd like to do is have a macro that will select each of these department codes in sheet2 and then save a copy of the file in a folder on the desktop called "Sample". For the file name, I would like it to be whatever is in B8 on sheet 2 and have B7 in the name. So for example if the first Choice in B8 called "Test1" is selected, the File name I would want saved in the folder would be "Test 1 - 1st Shift" and the next choice "Test2" would have a file saved called "Test 2 - 1st Shift Office" and so on and so forth. The macro will automatically select all available menu choices and then save each one of them as its own file. I've limited the sample to just 4 departments, but it would just basically need to stop at whatever the last dept. is in the drop-down menu. So with this sample, we would end up with 4 different Excel Files that are all copies of the selections made in the dropdown menu of Sheet2 - we will have a copy for Test 1 which will contain the names of all in that department and so on and so forth. Ultimately, Sheet2 is the template that is being saved as its own file.
    Last edited by PitchNinja; 09-27-2023 at 12:20 PM.

  17. #17
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Have you tried using the code from post #11? That's how it works.

  18. #18
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    That was the code I used, yes, but that is not how I need it to work. That code is making new workbooks out of Sheet1 not Sheet2. Sheet1 is not the template that needs to be saved. In this sample file, I gave a more simplified format of what sheet2 looks like, but in the actual file, sheet2 is a grid with its own special formatting design that would be printed out after it is saved. This grid displays a listing of people depending on what department is selected in the drop-down, so the names of people shown will of course change as different departments are selected. I did notice that your initial code in Post #2 had a reference to Sheet2 in its coding but that the coding in Post #11 has no reference to Sheet2.
    Last edited by PitchNinja; 09-27-2023 at 02:09 PM.

  19. #19
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Just thinking some. I don't know how VBA handles selecting from dropdown menus, but if it would make the code any simpler, the code could simply copy the department codes from column L of Sheet1 and paste each into B8 on Sheet2 since the selections in the drop-down menu there are coming from that column anyway. So in this case, copy Test1 from Column L in Sheet1, paste into B8 of Sheet2, then save sheet 2 as its specified name for Test1 in the specified location, then copy Test2 from Sheet1, paste into B8 of Sheet2, save Sheet 2 under its specified name for Test2 in the specified location and then so on and so forth.

  20. #20
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Hello. Make an example file based on that file and add another sheet (this sheet will be similar to the new file in the final result) as you see the solution to your question. So far there is a lot of text from you but nothing is clear. Sorry.

  21. #21
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Your code is only doing about half of what I am after. It's saving files under correct names and it is saving to the correct location. The problem is that it is not saving the correct Tab as a new file. It's saving Sheet1 as a new file, it needs to save Sheet2 as a new file. I listed a numbered step-by-step process before and I will do that again but with the method of copying and pasting between Sheet1 and Sheet2. In the same file I attached earlier:

    Step 1. On Sheet1, Copy the first department code from Column L(So starting in L2, this is "Test1")
    Step 2. Go to Sheet2, paste that into B8 - names and numbers will populate on Sheet2 starting in row 11 and on for anyone who belongs in the department pasted in B8
    Step 3. Save Sheet2's name as what appears in cell B8, then a dash, and then what appears in cell B7 in the sample folder. So for the first one, the filename would be "Test1 - 1st Shift"

    Repeat this 3 step process for however many departments are listed in Column L of Sheet1. In this sample we only have 4 departments, but there would be more in the actual file. Opening any of these files will have the tab with data as it appears on Sheet2.

    I really can't make this any clearer what this macro should do and this really isn't a lot of text.

  22. #22
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Hi @PitchNinja. For a long time I could not understand you, apparently life circumstances still influence a person. Sorry, I think this is what you need in the end result.
    Please Login or Register  to view this content.
    Good luck.

  23. #23
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Thank you Mike, THIS is what I was looking for Although question, could we have this code not do whatever auto-fitting/resizing of columns that it is doing in the new sheets?

  24. #24
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Hi. You can remove or comment out this line:
    Please Login or Register  to view this content.

  25. #25
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    212

    Re: Looking for a code that will save as different names to folder based on data validatio

    Awesome thank you very much for your help on this! I hope the life circumstances will start to settle for you soon

  26. #26
    Valued Forum Contributor
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    2021
    Posts
    307

    Re: Looking for a code that will save as different names to folder based on data validatio

    Thank you @PitchNinja. I would like all this in my country to end as soon as possible. Good luck 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. Replies: 0
    Last Post: 10-28-2021, 11:44 AM
  2. [SOLVED] Save outlook attachments to different folder paths based on Domain Names
    By amb2301 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-11-2021, 02:33 AM
  3. Sum Based of names - vba code save as pdf specific range
    By chrismil in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-09-2021, 05:39 PM
  4. [SOLVED] VBA Code to save to a folder within a folder within a folder all based on cell values.
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-24-2016, 04:24 PM
  5. Need help with code to save file in a folder location based on a cell value
    By msantucci in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-10-2015, 05:56 PM
  6. Code to create New Folder (if doesn't exist) and then Save Workbook to folder
    By jenhawley in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-10-2013, 03:09 PM
  7. [SOLVED] Need code to save file to new folder, erase from old folder
    By Ron M. in forum Excel General
    Replies: 1
    Last Post: 02-24-2006, 02:10 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