LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Check if worksheet exists in LabView

Solved!
Go to solution

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.

0 Kudos
Message 1 of 11
(3,203 Views)

Did you try the "Check if File or Folder Exists.vi" in the File I/O palette? 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
Message 2 of 11
(3,194 Views)

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.

0 Kudos
Message 3 of 11
(3,187 Views)
Solution
Accepted by chensley1975

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. 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 4 of 11
(3,172 Views)

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.

0 Kudos
Message 5 of 11
(3,162 Views)

Hi chensley,

 

since several LabVIEW versions there also is a "Create File with Incrementing Suffix" function…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 11
(3,150 Views)

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 ...Get Excel Worksheet nameGet Excel Worksheet name

Bob Schor

 

0 Kudos
Message 7 of 11
(3,128 Views)
Solution
Accepted by chensley1975

@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.  

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 8 of 11
(3,117 Views)

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).

0 Kudos
Message 9 of 11
(3,116 Views)

@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.

0 Kudos
Message 10 of 11
(2,981 Views)