08-08-2019 08:20 AM
All, we have a program that gathers data and then inserts it into a new worksheet with the current date as the worksheet name. The issue I have (which doesn't happen often, but would like to fix) is that if the test is run twice in the same day, the second test does not appear to be saved. I believe that this is because the worksheet name already exists. Is there a way to check if the file name exists? I would like a way that if the file name exists, then the program would add a number to the end of the date string (i.e. 08-08-2019a). I have attached a screenshot of the code for reference. If anyone can help with this, I would greatly appreciate it.
Solved! Go to Solution.
08-08-2019 08:41 AM
Did you try the "Check if File or Folder Exists.vi" in the File I/O palette?
08-08-2019 08:45 AM
Thank you for your response, however, I have checked that and it does tell me that the file exists (which I know that it does) but it won't tell me if the worksheet exists which is what I am looking for.
08-08-2019 08:54 AM - edited 08-08-2019 08:56 AM
So you want to check if a worksheet called by a certain name exists within a workbook? If so, you want to add another worksheet with a suffix added.
This can be done but let me suggest an easier method that would save on code complexity. Just add a timestamp to the tab name. For example, "2019-08-08_0730". This wouldn't work if you tried to add another worksheet within the same minute but it sounds like that wouldn't be an issue for you. Doing it this way doesn't require a check of the workbook first. You only need to create a new worksheet with a new timestamp and insert data.
08-08-2019 08:59 AM
That is a good idea, I hadn't thought of adding the timestamp to the end. Thank you for your help. I will implement that and it should fix the issue.
08-08-2019 09:15 AM
08-08-2019 09:47 AM
Ha! I thought you were asking the question that appears in your Title and in your formulation of the Problem. You used the term "Worksheet", which Excel uses for its "worksheets". When Excel opens and creates a new Workbook, it creates it with three Worksheets called "Sheet1", "Sheet2", and "Sheet3". The Report Generation Toolkit has functions that let you choose the Worksheet you want to use, rename them, create new ones, etc.
What I thought you wanted to do (maybe because I didn't read your original question too carefully) was to open a Workbook, add some data, and at some later time, reopen the same Workbook and add more data on another Worksheet. As not everyone knows that you can do this, and knows how to do this, I created a little demo to show how easy it is to get the name of the worksheet. Although it is now "off topic", I'll post it here "just in case" anyone is interested ...
Bob Schor
08-08-2019 09:58 AM
@Bob_Schor wrote:
Ha! I thought you were asking the question that appears in your Title and in your formulation of the Problem......What I thought you wanted to do (maybe because I didn't read your original question too carefully) ....
The question was definitely confusing because it starts off talking about worksheets and then it switches over to file names. It's kinda funny to see what we notice and latch on to.
08-08-2019 09:58 AM
You are correct in thinking that I was wanting to check the name of the worksheet and if it already exists, then change the name of the next worksheet. I appreciate your answer as this might help me on future projects that I create. I marked the previous answer as the solution since it resolved my original issue of not being able to create two sheets when the test was run on the same day (it would always be at least a couple of hours apart).
11-06-2019 04:01 PM
@Bob_Schor wrote:
When Excel opens and creates a new Workbook, it creates it with three Worksheets called "Sheet1", "Sheet2", and "Sheet3".
FYI (I got bit with this one), Excel 2016 just creates "Sheet1". I think it created all three back in 2013. I haven't tried with newer versions than 2016.