Forum Discussion

Jula78's avatar
Jula78
Copper Contributor
Oct 23, 2023
Solved

ACTION SCRIPT RECORDER - MOVE COLUMN

Try to register a simple column movement like this:

 

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  // Insert cut cells from C:C on selectedSheet to A:A on selectedSheet.
  selectedSheet.getRange("A:A").insert(ExcelScript.InsertShiftDirection.right);
  selectedSheet.getRange("C:C").moveTo(selectedSheet.getRange("A:A"));
}
 
i move only column C to A
 
see what happen
  • Jula78 

    moveto method only copy values from one range to another, it keeps source range empty after that.

    After shifting column A to the right you shift all other columns as well. Thus column C becomes column D. You may move values from it to new column A and after that remove that column

    function main(workbook: ExcelScript.Workbook) {
      let selectedSheet = workbook.getActiveWorksheet();
    
      selectedSheet
        .getRange("A:A")
        .insert(ExcelScript.InsertShiftDirection.right);
      selectedSheet
        .getRange("D:D")
        .moveTo(selectedSheet
        .getRange("A:A"));
      selectedSheet
        .getRange("D:D")
        .delete(ExcelScript.DeleteShiftDirection.left)
    }

7 Replies

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hi Jula78,

    for moving the entire column C to column A using Excel Script you can try to use the following code:

     

    function main(workbook: ExcelScript.Workbook) {
      let selectedSheet = workbook.getActiveWorksheet();
      // Cut column C on selectedSheet.
      selectedSheet.getRange("C:C").cut();
      // Insert cut cells from clipboard to A:A on selectedSheet.
      selectedSheet.getRange("A:A").insert(ExcelScript.InsertShiftDirection.right);
    }

     

    This code will cut column C to the clipboard, and then insert the cut cells to column A. This will move column C to column A without inserting a blank column in between.

    To run this code, you can copy and paste it into a new ExcelScript module, and then run the module by pressing F5.

    Here are the steps on how to run the ExcelScript code:

    1. Open the Excel file where you want to move column C to column A.
    2. Press Alt+F11 to open the Visual Basic Editor.
    3. In the Insert menu, click Module.
    4. Paste the ExcelScript code into the module.
    5. Press F5 to run the code.


    Please click Mark as Best Response & Like if my post helped you to solve your issue.
    This will help others to find the correct solution easily. It also closes the item.


    If the post was useful in other ways, please consider giving it Like.


    Kindest regards,


    Leon Pavesic
    (LinkedIn)

    • SergeiBaklan's avatar
      SergeiBaklan
      MVP

      LeonPavesic 

      cut method doesn't exists ExcelScript,Range interface

      why TypeScript is inserted into VBA interface?

       

      It looks like AI generated answer, did you test it?

  • Jula78 

    moveto method only copy values from one range to another, it keeps source range empty after that.

    After shifting column A to the right you shift all other columns as well. Thus column C becomes column D. You may move values from it to new column A and after that remove that column

    function main(workbook: ExcelScript.Workbook) {
      let selectedSheet = workbook.getActiveWorksheet();
    
      selectedSheet
        .getRange("A:A")
        .insert(ExcelScript.InsertShiftDirection.right);
      selectedSheet
        .getRange("D:D")
        .moveTo(selectedSheet
        .getRange("A:A"));
      selectedSheet
        .getRange("D:D")
        .delete(ExcelScript.DeleteShiftDirection.left)
    }
    • Jula78's avatar
      Jula78
      Copper Contributor
      ok it's work correctly, but Action recorder not.
      • Jula78 

        Action recorder is good enough as the starting point. The code returned is not always optimal and, as we see, could give wrong result.

Resources