I want to add new column to the existing Excel workbook - now the column header will always contains previous month & current year, for example in this month => 82021 but here is one condition: if that column header is already there, do not add anything.
So I’m looking for some kind of Excel HLookUp but I didn’t find anything in the UiPath - I’m not sure how I can check the first row data, to avoid having duplicated columns names like on below screen:
Any ideas much appreciated!
For this u can try this way as below
Read the excel and store in dt1
Now assuming for this month u need column name 92021
store the name in a variable using assign activity
Now use a if condition
dt1.Columns.Cast(Of DataColumn).Where(Function(c) c.ColumnName.ToString.Equals(column_name)).Count=0
If the condition is true:
Then there is no column named as 92021 and then u can add column
Thank you for your support but the condition you gave was not working as it should - it was adding the column even if it was already presented in the file - it throw an error after adding duplicated column but this wasn’t the effect I’d like to achieve.
However I’ve found another workaround to this problem with a for each loop as below:
dateTargetDate = New DateTime(Now.AddMonths(-1).Year,Now.AddMonths(-1).Month,1)
strTargetDate = dateTargetDate.ToString(“MMyyyy”)
boolColumnExists = False
For each item in dtMyTable.Columns:
If: item.ToString = cint(strTargetDate).ToString
boolColumnExists = True
Now after all columns have been checked, there is final If statement to check bool variable and add column if needed:
If: boolColumnExists = False
Add Data Column
And the above is working as of today.
Have o good afternoon,
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.