Excel automation find the count

How can I count the number of characters in each cell in Excel, and set a condition so that if a cell contains more than 100 characters, it only displays the first 100 characters?

Hi @Sai17

Can you try below

CurrentRow("ColumnName") = If(CurrentRow("ColumnName").ToString.Length > 100, CurrentRow("ColumnName").ToString.Substring(0, 100), CurrentRow("ColumnName").ToString)

Regards,

@Sai17

If you want to check all columns, then try below

Method1: By using For Each loop

currentDataRow(currentDataColumn) = If(currentDataRow(currentDataColumn).ToString.Length > 100, currentDataRow(currentDataColumn).ToString.Substring(0, 100), currentDataRow(currentDataColumn).ToString)

Method2: By using Invoke Code activity

For Each row As DataRow In dt.Rows
    For Each column As DataColumn In dt.Columns
        row(column) = If(row(column).ToString.Length > 100, row(column).ToString.Substring(0, 100), row(column))
    Next
Next

Regards,

2 Likes

Thank you for the help. I’ll give it a try and let you know how it goes.

1 Like

Thanks, its working. I have small doubt can we use Linq on this scenario

Hi @Sai17 ,

You can achieve this with LINQ

Below is the Workflow Screenshot

and Below is the LINQ code used in an Assign activity

(From row In dt.AsEnumerable
Let updatedValues = dt.Columns.Cast(Of DataColumn).
                    Select(Function(col) If(row(col).ToString.Length > 100, 
                                                row(col).ToString.Substring(0, 100), 
                                                row(col))).ToArray
Select dt.Clone.Rows.Add(updatedValues)).CopyToDataTable

here dt → Source Datatable
dt_Output → is the Output datatable

Hope it helps you !

Best Regards,
Vikas M

1 Like