Forum Discussion
Jula78
Oct 23, 2023Copper Contributor
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
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
Sort By
- LeonPavesicSilver 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:
- Open the Excel file where you want to move column C to column A.
- Press Alt+F11 to open the Visual Basic Editor.
- In the Insert menu, click Module.
- Paste the ExcelScript code into the module.
- 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)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?
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) }