LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Append number to worksheet name if current name exist

Solved!
Go to solution

Hello,

 

I am attempting to index the worksheet name of an excel workbook.  If You run the code a second time, you get an error because the name already exists.  How to go about adding a new sheet each time with a number added to the original sheet name each time. So, for the second run, the name will be TESTNAME_1, the third run will have the name TESTNAME_2 and so on.

 

Thanks,

0 Kudos
Message 1 of 16
(3,880 Views)

What about inserting this for filenames and increments?

 

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
Message 2 of 16
(3,864 Views)

 

Thanks!

0 Kudos
Message 3 of 16
(3,848 Views)

There is no TRUE case, the other case is the empty string "" case which passes the TESTNAME input on to the worksheet name input.  The earlier attached file is a snippet.  Open the attachment, then drag it across to the block diagram for all of the details.

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
Message 4 of 16
(3,839 Views)

I put your code into a state machine, but the second iteration gets an error.  The attached program generates the name 001.  

 

Please let me know what I did wrong?

 

Also, is there a way to create a new excel file if one does not exist?

 

Thanks!

0 Kudos
Message 5 of 16
(3,816 Views)

OK, I got it to work partially (the "True" case= "").  On the third iteration, the name come out as "TESTNAME001001".  The fourth iteration, the names comes out as "TESTNAME001001001".  How can we fix this?

 

Thanks.

0 Kudos
Message 6 of 16
(3,798 Views)

I think you will need to make some assumptions about your data and decide what format you want.

 

For example, assume you had the following names

TestName, TestName1,TestName, TestName1

 

would you want the names to be TestName, TestName1, TestName2,TestName3, or

would you want the names to be TestName, TestName1, TestName_1,TestName1_1, etc

 

Steps for the first case.

  1. Get a list of all of the sheet names first
  2. Assume that any numbers at the end of the name can be deleted, so you can append other numbers.

Attached is a method, 2015 Version, with credit due to Altenbach as part of code is his from a post somewhere on the discussion forums. Test the code with a list of names, or empty strings and modify as needed.

 

Cheers,

mcduff

 

PS Hopefully, Altenbach will post something much smaller than my superfluous code.

Message 7 of 16
(3,785 Views)
Solution
Accepted by topic author hiNI

Modify the false case as attached.

False case.png

This will allow for the tab names to be TESTNAME, TESTNAME_001, TESTNAME_002,...

If you have more than 1000 files, then adjust the %03d to handle the maximum number of desired files. 

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
Message 8 of 16
(3,760 Views)

Didn't you already ask this question as an "add-on" to your earlier post, "Rename Excel Worksheets"?  There I told you how to name a sheet "MODULATION_1" if there was already a sheet named "MODULATION".

 

Have you actually tried to implement any of the suggestions we've given you, or are you just waiting for one of us to "hand you a complete solution" to your problem?

 

Bob Schor

0 Kudos
Message 9 of 16
(3,751 Views)

@mcduff wrote:

 

PS Hopefully, Altenbach will post something much smaller than my superfluous code.


I don't quite recognize the code, but my memory is no longer what it never was. 😄 Do you have a link? (I don't want to receive false credit, especially if the code contains things I would never do, such as abuse a while loop as a fake FOR loop. 🐵 

 

In any case, LabVIEW has a tool named "create file with incrementing suffix" in the advanced file palette and the code is not locked. I am sure it would be trivial to copy and re-use the core of the code and apply it to worksheet names. We are just dealing with strings! No need to re-invent the wheel 😄

Message 10 of 16
(3,742 Views)