How to Insert a Column Into a Specific Position in a Loop

I Have an Excel sheet with 3000+columns and the starting index is 2
From starting index to 7 columns after that add three columns(“Lesser MOQ Order”,“Final AOG Qty”,“Final AOG Value”) loop wise. (After 7 colums add 3 above columns)

Hi @Melbin_Antu1
Use Insert column activity

image

Hi @Melbin_Antu1

Try the below way:

Assign activity: Index = 2

Assign activity: NewColumns = Enumerable.Range(1, ExcelDataTable.Columns.Count)
                .Where(Function(i) i >= Index)
                .SelectMany(Function(i) If((i - Index) Mod 7 = 0, {"Lesser MOQ Order", "Final AOG Qty", "Final AOG Value"}, {ExcelDataTable.Columns(i - 1).ColumnName}))
                .ToArray()

For Each activity: Each column in NewColumns
    Use Add Data Column acitivity and column in Column Name and give the datatable name.
Next

NewColumns is of DataType Array(System.String). Index is of DataType System.Int32.

Regards

Hi @Melbin_Antu1

→ Read Range Workbook
Output-> ExcelDataTable
Input:


→ Use the below syntax in Assign activity:

ExcelDataTable1= New DataTable()
Index= 8
NewColumns= Enumerable.Range(1, ExcelDataTable.Columns.Count).Select(Function(i) If(i = Index + 1, {"Lesser MOQ Order", "Final AOG Qty", "Final AOG Value"}, {ExcelDataTable.Columns(i - 1).ColumnName})).ToArray()
For each currentItem in NewColumns.SelectMany(Function(arr) arr)
     -> Add Data Column 
            Column Name: currentItem.ToString
            DataTable: ExcelDataTable1

ExcelDataTable1 is of DataType System.Data.DataTable. NewColumns is of DataType Array(Array(System.String)). Index is of DataType System.Int32.
→ Write Range Workbook
Output:


xaml:
Sequence1.xaml (10.1 KB)

Regards


@vrdabberu

Hi @Melbin_Antu1

Try this process

Regards

Hi @Melbin_Antu1

Please use the code in the below thread and change the Index number value used in the assign activity as per the requirement.

Regards

Or you can use invoke Method.
Add the Column first, then invoke method to change the position.

The index is equal to the column position.

@vrdabberu i used this way no error where found but there is no changes in excel

Hi @Melbin_Antu1

Can you share the workflow because that code have worked for me. I have attached the supporting screenshots of the EXCEL. Please share your workflow I will try to resolve the issue.

Regards

Hi @Melbin_Antu1

See the same code worked for me. Please check the workflow I have attached.

Input:

Output:

Regards


same code worked but no column added

Hi @Melbin_Antu1

Could you share the workflow an the sample excel file.

Regards

@vrdabberu are u tried?

Hi @Melbin_Antu1

Yeah trying that.

Regards

Hi @Melbin_Antu1

Check the below zip file:
BlankProcess25.zip (30.2 KB)

Make sure to enable macros.

Output:
test (5).xlsx (1.2 MB)

Regards

@vrdabberu Thanks its Working :+1:

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