# Using Named Range name in formula first and then writing the Named range name causes &amp;quot;Invalid Name Error&amp;quot;

Hi,
The usage of named range in a cell is not valid until and unless the named cell is written first, in other words, if we use the named cell name in a formula first without writing the named cell first and then write the named cell we end up in “Invalid Name Error” in excel.
To better understand this issue I am attaching the file which got generated by below sample code, and even attaching the file that got generated, as you can see B2 has “Invalid Name Error”.
Is this is an known issue or is there any workaround for fixing the “Invalid Name Error”, as in our case we would some times write the formula using named cell and then set the name some where else, and we end up with “Invalid Name Error”.

static void worksheetEvalutionException() throws Exception
{
InputStream templateAsStream = null;
try {
Workbook workbook = new Workbook(LoadFormat.AUTO);
Worksheet worksheet = workbook.getWorksheets().get(0);

/*
* This works fine
* 1. Creating named cell
* 2. Using it
/
Range r = worksheet.getCells().createRange(“A1”);
r.setName("_cell1_A1");
Cell cell = worksheet.getCells().get(“A1”);
cell.putValue(11); // setting 11 in name1
cell = worksheet.getCells().get(“B1”);
cell.setFormula("_cell1_A1");

/

* This does not work
* We end up in “Invalid Name Error” in Cell B2
* 1. First writing/using the name in the cell
* 2. Creating the named cell which is used above in step.1
*/
cell = worksheet.getCells().get(“B2”);
cell.setFormula("_cell1_A2");
cell = worksheet.getCells().get(“A2”);
cell.putValue(12);
r = worksheet.getCells().createRange(“A2”);
r.setName("_cell1_A2");

workbook.save(“testAsposeIssue.xls”);
}
finally {
if (templateAsStream != null) {
templateAsStream.close();
}
}
}

Hi,

Thanks for your posting and using Aspose.Cells for Java.

Please download and use the latest version: Aspose.Cells
for Java v7.3.4.3
. It should work fine at your end.

I have tested your issue with the latest version with the following code and the generated output file did not show any error in the Ms-Excel file.

I have attached the output file and screenshot for your reference.

Java

Workbook workbook = new Workbook(LoadFormat.AUTO);

Worksheet worksheet = workbook.getWorksheets().get(0);

/*
* This works fine
* 1. Creating named cell
* 2. Using it
/

Range r = worksheet.getCells().createRange(“A1”);

r.setName("_cell1_A1");

Cell cell = worksheet.getCells().get(“A1”);

cell.putValue(11); // setting 11 in name1

cell = worksheet.getCells().get(“B1”);

cell.setFormula("_cell1_A1");

/

* This does not work
* We end up in “Invalid Name Error” in Cell B2
* 1. First writing/using the name in the cell
* 2. Creating the named cell which is used above in step.1
*/

cell = worksheet.getCells().get(“B2”);

cell.setFormula("_cell1_A2");

cell = worksheet.getCells().get(“A2”);

cell.putValue(12);

r = worksheet.getCells().createRange(“A2”);

r.setName("_cell1_A2");

workbook.save(“output.xls”);

Screenshot:

The version we are using is 7.1.2, and the issue is not replicated with the latest version of Aspose (7.3.4.3), thanks for the update and fix.

Hi,

Good to know that your issue is resolved by the latest version/fix (shared by Shakeel Faiz).
Feel free to contact us any time if you need further help or have any other issue. We will be happy to assist you here.

Thank you.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8044706,"math_prob":0.49964672,"size":3147,"snap":"2021-21-2021-25","text_gpt3_token_len":837,"char_repetition_ratio":0.16353802,"word_repetition_ratio":0.39869282,"special_character_ratio":0.2802669,"punctuation_ratio":0.1952381,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9599918,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-17T06:11:26Z\",\"WARC-Record-ID\":\"<urn:uuid:0096a4ca-051b-4862-99b6-1dc9a6be76b0>\",\"Content-Length\":\"21883\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:869bf7ab-ce16-4610-97d7-97b4cf7a198d>\",\"WARC-Concurrent-To\":\"<urn:uuid:7e3113ec-50c7-4a7d-b776-6dfddd489979>\",\"WARC-IP-Address\":\"54.70.205.143\",\"WARC-Target-URI\":\"https://forum.aspose.com/t/using-named-range-name-in-formula-first-and-then-writing-the-named-range-name-causes-amp-quot-invalid-name-error-amp-quot/100477\",\"WARC-Payload-Digest\":\"sha1:H7EDKN4SS7FQ2SYRNEK5Z6DWOZQBOZ2Z\",\"WARC-Block-Digest\":\"sha1:IEIDE5MI4GZ5B4FIJDGY7SRLJ7E4HF3B\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487629209.28_warc_CC-MAIN-20210617041347-20210617071347-00236.warc.gz\"}"}