How to optimize this simple code?

Dear UiPathers

Is there a way to optimize this code so it runs faster? Right now it takes about 10 mins to go through all my rows and I have 1300 cells that needs to be written.

The code is not much but I would like to ehar if there are any better activities which can complete the same job but faster or any options you can tweak. Thanks in advance

image

Yours sincerely.
Khan. S

Hello,

is tablecount number of rows of your excel file ?

I think you should first use read range workbook activity before the for each loop, and store the datatable.

Now you can loop through each row if it instead of creating collection from 0 to number of rows.

in your for each loop : update row item activity instead of writting cell directly, and update the needed field.

After loop : Write range workbook with entire datatable in Excel.

With this you update only one time Excel file, should be much faster.

Enumerable.Range(0,x) is fine, u dont need β€œ.ToArray()”
Also work with dt in background and write one time would be quicker.

1 Like

Are you sure, that you want to write x times (x - rowcount) the same value into the same cell?

1 Like

Sorry, I fixed that, forgot to upload the new image. I am now writing to β€œB”+(Convert.ToInt32(currentItem)).ToString

So you want to write the same value (β€œENS150121”) to 1300 cells?

have a look here:
grafik

we write hello in one go to Range A3 - A9
So you can use the row count and can do it similar

As an alternate as mentioned by

we can selectively update an Excel e.g. with a 1-Column only DataTable and using write range

1 Like

Dear Kali

Thanks, that worked just as I wanted. Thanks for pointing it out to me.

1 Like

Dear PPR

I see. It works much faster. I had to accept Kali’s answer since he was the first, however, I thank you for also reaching out and helping! So thank you for that :slight_smile:

1 Like

Dear ptrobot

Exactly, hwoever, I already got a solution. Haver a wonderful day forward :3

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.