Hamsterdam
New Member
- Joined
- Sep 23, 2019
- Messages
- 5
Hi,
This my first post, apologies if I break any rules in the forum.
I am trying to use VBA to write a simple code to auto fit column width. I have data in columns A:Z and I have also merged cells D5:J5. Naturally, I think a simple line like the following will do the job:
Columns("A:Z").AutoFit
It does work in most cases... but roughly 1/10 times the Macro would set the column width to 255 (max) instead of auto fitting. I have googled around and tried the following but nothing worked:
ActiveSheet.StandardWidth = 17
Rows("1:100").AutoFit
With Columns("A:Z")
Additionally, it is extremely hard to replicate this problem. The Macro would work fine after several executions but would randomly set column width to 255 in one instance, so with each change it would take me 10-20 tries to confirm whether it actually solves the problem or not.
Any explanations or suggestions would be greatly appreciated.
This my first post, apologies if I break any rules in the forum.
I am trying to use VBA to write a simple code to auto fit column width. I have data in columns A:Z and I have also merged cells D5:J5. Naturally, I think a simple line like the following will do the job:
Columns("A:Z").AutoFit
It does work in most cases... but roughly 1/10 times the Macro would set the column width to 255 (max) instead of auto fitting. I have googled around and tried the following but nothing worked:
ActiveSheet.StandardWidth = 17
Rows("1:100").AutoFit
With Columns("A:Z")
.ColumnWidth = 200 'set it to a wider width then let excel autofit narrow it
.AutoFit
End WithAdditionally, it is extremely hard to replicate this problem. The Macro would work fine after several executions but would randomly set column width to 255 in one instance, so with each change it would take me 10-20 tries to confirm whether it actually solves the problem or not.
Any explanations or suggestions would be greatly appreciated.