logo
search
VBA & Macro Problems

Fix Excel Office Script Recording Incorrect Column Widths

Rana GarciaRana Garcia Sep 25, 2026 869 views

Question details

The user needs to correct an issue where Excel Office Scripts record inaccurate column widths during automation tasks.

How to Fix Excel Office Script Recording Incorrect Column Widths
Product
Microsoft Excel
Device & OS
not provided
Scenario
Using the Excel Action Recorder or writing Office Scripts to automate column width adjustments in a workbook.
Observed behavior
The generated Office Script records and applies column widths that are significantly smaller than the values manually selected in the UI, although column-hiding functionality continues to work correctly.
Before you start

Before modifying your script code, duplicate your Excel workbook to prevent accidental data loss, and create a small, blank sample file to safely test API property changes.

Solution 1Recommended

Verify Script Width Units and Modify the Excel API Property

Check the numerical values generated by the script and adjust them manually, as Office Scripts often use points or pixels instead of the character units displayed in the Excel UI.

The Excel Action Recorder can sometimes misinterpret the UI's character-based width and output the wrong equivalent in the script API. Manually adjusting the recorded values usually resolves the discrepancy.

1
Open the Code Editor

Navigate to the 'Automate' tab on the Excel ribbon and click on 'Code Editor' to view your generated Office Script.

2
Locate the Column Formatting Code

Scroll through your script to find the line where the column width is being set, which typically looks like `range.getFormat().setColumnWidth(value);`.

3
Adjust the Numerical Value

Increase the numerical value passed into the `setColumnWidth` method. Since the API expects point values rather than character counts, you may need to experiment by typing a larger number to match your intended visual width.

4
Save and Run

Click 'Save' in the Code Editor, navigate to your test workbook, and run the script to verify if the new column width appears correct.

Verify Script Width Units and Modify the Excel API Property
Understanding Unit Conversion: Excel's user interface measures column widths in characters (e.g., 8.43), whereas the Office Scripts API requires the width in points or pixels. This conversion gap is usually the root cause of the recorded value being too small.
Free Microsoft Office alternative

Use WPS Spreadsheet for Reliable Macro Automation

If Excel's Office Scripts are causing workflow disruptions with inaccurate recordings, consider switching to WPS Office. WPS Spreadsheet provides a robust and highly compatible environment for data management, complete with powerful Macro and VBA support that reliably records your formatting adjustments.

Fully compatible with Microsoft Excel (.xlsx, .xls) and CSV file formats.Reliable Macro and VBA compatibility for seamless automation without recording unit errors.Free, lightweight, and fast-loading application designed to boost productivity.Familiar user interface that ensures a seamless and zero-learning-curve migration.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Excel Office Script set the column width too small?

The Action Recorder may misinterpret the Excel UI's character-based column width and record it as points or pixels without applying the proper conversion in the script API. This results in the script applying a much smaller width when executed.

Does this recording bug affect my ability to hide columns in the script?

Generally, no. Hiding a column relies on changing its visibility property (setting the hidden flag to true), which does not depend on specific numerical width values. Hiding columns usually continues to work normally despite width recording errors.

How can I manually set the correct column width in an Office Script?

Open the Automate tab's Code Editor, locate the range you want to adjust, and use the `setColumnWidth(width)` method. You will need to input a larger point value to achieve the desired character width you see in the standard Excel UI.

Can I use VBA instead of Office Scripts to fix column width issues?

Yes. VBA uses the `ColumnWidth` property (based on characters) which closely matches the UI, or the `Width` property (based on points). If Office Scripts are bugged in your web or desktop version, utilizing standard VBA macros may provide more accurate results.