Script Column Width not Adjusting

R1chard

Active Member
Joined
Jan 14, 2004
Messages
410
I recorded a script, to adjust column widths. But When I run the macro, the column widths are not adjusted correctly. What is wrong with my script?

function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Insert at range 1:1 on selectedSheet, move existing cells down
selectedSheet.getRange("1:1").insert(ExcelScript.InsertShiftDirection.down);
// Insert at range 1:1 on selectedSheet, move existing cells down
selectedSheet.getRange("1:1").insert(ExcelScript.InsertShiftDirection.down);
// Insert at range 1:1 on selectedSheet, move existing cells down
selectedSheet.getRange("1:1").insert(ExcelScript.InsertShiftDirection.down);
// Insert at range 1:1 on selectedSheet, move existing cells down
selectedSheet.getRange("1:1").insert(ExcelScript.InsertShiftDirection.down);
// Insert at range 1:1 on selectedSheet, move existing cells down
selectedSheet.getRange("1:1").insert(ExcelScript.InsertShiftDirection.down);
// Insert at range 1:1 on selectedSheet, move existing cells down
selectedSheet.getRange("1:1").insert(ExcelScript.InsertShiftDirection.down);
// Clear ExcelScript.ClearApplyTo.contents from range 1:7 on selectedSheet
selectedSheet.getRange("1:7").clear(ExcelScript.ClearApplyTo.contents);
// Set height of row(s) at range 1:7 on selectedSheet to 30
selectedSheet.getRange("1:7").getFormat().setRowHeight(30);
// Set width of column(s) at range A:B on selectedSheet to 12
selectedSheet.getRange("A:B").getFormat().setColumnWidth(12);
// Set width of column(s) at range C:C on selectedSheet to 65
selectedSheet.getRange("C:C").getFormat().setColumnWidth(65);
// Auto fit the columns of range E:E on selectedSheet
selectedSheet.getRange("E:E").getFormat().autofitColumns();
// Set width of column(s) at range I:J on selectedSheet to 18
selectedSheet.getRange("I:J").getFormat().setColumnWidth(18);
// Delete range K:U on selectedSheet
selectedSheet.getRange("K:U").delete(ExcelScript.DeleteShiftDirection.left);
// Delete range D:D on selectedSheet
selectedSheet.getRange("D:D").delete(ExcelScript.DeleteShiftDirection.left);
// Delete range E:G on selectedSheet
selectedSheet.getRange("E:G").delete(ExcelScript.DeleteShiftDirection.left);
// Set range D9:F9 on selectedSheet
selectedSheet.getRange("D9:F9").setValues([["VPPS","SDC","FSU"]]);
}
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
which column(s) specifically aren't being adjusted? I tried this code and it works on my end.
 
Upvote 0
which column(s) specifically aren't being adjusted? I tried this code and it works on my end.
Example:

Starting Column A Width: 8.09 (96 Pixels)
Run Script
Finish Column A Width: 1.55 (24 Pixels)

Everything in the script works fine, except for the column width adjustments. Its makes all column widths really small, and not what it says in the script

Before
1727114625479.png


After
1727114664162.png
 
Upvote 0
This is because in your code you are setting columns A and B to 12 pixels

JavaScript:
selectedSheet.getRange("A:B").getFormat().setColumnWidth(12);

If you want this to be 12 units (what Excel normally uses), you can change your code to this:

JavaScript:
selectedSheet.getRange("A:B").getFormat().setColumnWidth(66.75);
 
Upvote 0
This is because in your code you are setting columns A and B to 12 pixels

JavaScript:
selectedSheet.getRange("A:B").getFormat().setColumnWidth(12);

If you want this to be 12 units (what Excel normally uses), you can change your code to this:

JavaScript:
selectedSheet.getRange("A:B").getFormat().setColumnWidth(66.75);
When I changed the number to your value, it worked fine. Thanks

I didn't write this macro, I just recorded my actions, and adjusted my column width, and it entered the values on the script automatically. Is there a setting I should change? Because I don't know what value to change all my other widths to
 
Upvote 0
I recorded the macro to give you the updated number. I manually right-clicked the columns and changed it to 12 units and the macro gave me that 66.75. How did you change it when you were recording the macro?
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top