logo
search
VBA & Macro Problems

Fix Excel Office Scripts setColumnWidth Producing Incorrect Widths

Camila MilosovichCamila Milosovich Sep 29, 2026 869 views

Question details

The user needs to fix or work around an issue where the Excel Office Scripts setColumnWidth method creates narrower columns than specified.

How to Fix Excel Office Scripts setColumnWidth Producing Incorrect Widths
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating column width adjustments using Excel Office Scripts.
Observed behavior
The setColumnWidth method calculates and sets an incorrect, much narrower column width than the value defined in the script.
Before you start

Ensure that your Office Script targets the correct worksheet and cell range, and verify that no subsequent formatting operations or macros are overwriting your initial column width settings.

Solution 1Recommended

Use Adjusted Values as a Temporary Workaround

Since this is a known calculation bug within Excel Office Scripts (tracked internally by Microsoft as #9333095), the most reliable immediate fix is to manually scale your width values until the desired visual width is achieved.

The setColumnWidth method currently miscalculates the unit mapping, producing narrower columns. Until an official patch is deployed, developers must use a trial-and-error approach to offset the calculation discrepancy.

1
Identify the target width discrepancy

Run your current Office Script and measure the actual output column width compared to your intended width to find the scaling ratio.

2
Adjust the script value

Multiply your desired column width value by the discrepancy ratio and mathematically increase the argument passed to the setColumnWidth(value) method in your code editor.

3
Test and refine the output

Execute the script again in the Excel workbook and tweak the numeric value iteratively until the displayed column width exactly meets your visual requirements.

Use Adjusted Values as a Temporary Workaround
Known Issue: Microsoft has logged this exact behavior under internal tracking number 9333095. This workaround may be necessary until a background fix is applied to Office Scripts.
Free Microsoft Office alternative

Try WPS Office for Reliable Spreadsheet Automation

If you are frustrated by Office Script bugs affecting your workflow, consider switching to WPS Office. WPS Spreadsheet offers a lightweight, highly compatible alternative with robust support for traditional VBA macros, ensuring your automation runs exactly as intended without unexpected formatting glitches.

  1. 1. Download WPS Office: Visit the official WPS website and download the free WPS Office suite for your operating system.
  2. 2. Open your spreadsheet: Launch WPS Spreadsheet and easily open your existing .xlsx or .xlsm files.
  3. 3. Run your macros: Navigate to the Developer tab to reliably manage and execute your formatting macros with precision.
Highly compatible with Microsoft Excel (.xlsx) and macro-enabled formats (.xlsm)Free, lightweight, and uses minimal system resourcesFamiliar user interface requires no learning curveReliable execution of traditional VBA macros for accurate column formatting
microsoft office alternative - wps office

Frequently Asked Questions

Why does setColumnWidth produce narrower columns in Excel Office Scripts?

This is a known calculation bug in Excel Office Scripts (Microsoft internal tracking #9333095) where the method miscalculates the pixel or character width mapping, resulting in columns that are significantly narrower than the specified numeric value.

Are there more stable alternatives to setColumnWidth in Excel automation?

If Office Scripts are failing, you can rely on traditional VBA macros. Using the Columns.ColumnWidth property in VBA remains highly stable and accurate for setting exact column dimensions without the calculation bugs present in the newer Office Scripts.

Can other automated formatting operations affect my column widths?

Yes. If your script uses getFormat().autofitColumns() after manually setting the column width, or if external data refreshes trigger table resizing, they will overwrite your predefined width settings. Always place static width adjustments at the end of your script.