Last Column Index in an Excel where the columns are dynamic

Want to get the last column of the excel,(the last Col3 refer the attched image) where the column count is not constant. I cant read the data to a datatable because the column headers are not unique and i dont want to disturb them as well.

@Sonadri_Tanaya_Mishra_EXT,

Refer this solution.

@Sonadri_Tanaya_Mishra_EXT

inside use excel file use the below

Excel.Sheet("Sheetname").GetNumberOfColumns this will give count of columns or the index of last column…if you want zero based then subtract 1

then UiPath.Excel.Helpers.ExcelUtilities.ConvertColumnIndexToColumnLetter(Excel.Sheet("Sheetname").GetNumberOfColumns) will give the excel column name like B D E etc

cheers

Hey Anil,

Thanks for the response. I am not using excel scope, i am using Workbook activity, there can use?
Thanks,Sonadri

@Sonadri_Tanaya_Mishra_EXT

then you need to use read rnage and then count the columns…while reading just uncheck add headers so that you wont have any trouble reading

cheers

1 Like

Hi @Sonadri_Tanaya_Mishra_EXT

Here is a solution:

STEP 1:
Use Read Range workbook. But disable Add Headers

The output will be:

STEP 2:
Use the following Assign Statement to get the last column

dt_LastColumn = dt_LastColumn.DefaultView.ToTable(False, dt_LastColumn.Columns(dt_LastColumn.Columns.Count - 1).ColumnName)

The output will be:

STEP 3:
Use the Invoke Code activity to Replace the Column name with First row, and then remove first row.

The Code:

For i As Integer = 0 To dataTable.Columns.Count - 1
    dataTable.Columns(i).ColumnName = dataTable.Rows(0)(i).ToString()
Next

dataTable.Rows.RemoveAt(0)

The Argument:

THE FINAL OUTPUT

If this solves your issue, Do mark it as a solution.

Happy Automation :star_struck:

1 Like

Once i get the index, can I get the character! like if idx is 26 then Z is the column, i need it for range

Thanks

1 Like

@Sonadri_Tanaya_Mishra_EXT,

This code will give you letter from index

UiPath.Excel.Helpers.ExcelUtilities.ConvertColumnIndexToColumnLetter(26) 
1 Like

Thank you so much for the in detailed explanation.

1 Like

@Sonadri_Tanaya_Mishra_EXT

Yes you can using the same formula above

Cheers

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