Matching column width in two tabs

Sweedler

Board Regular
Joined
Nov 13, 2020
Messages
132
Office Version
  1. 365
Platform
  1. Windows
Range("A:A").ColumnWidth = Sheets("Kaptnota").Columns("A").ColumnWidth

I am using the above code to transpose column width from one tab to another. But I am getting an error message, and I don't know why.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about this?

VBA Code:
Sub ChangeColumnsW()
  Dim X As Long
  
  For X = 1 To 10
    Columns(X).ColumnWidth = Sheets("Kaptnota").Columns(X).ColumnWidth
  Next X
  

End Sub
 
Upvote 0
How about this?

VBA Code:
Sub ChangeColumnsW()
  Dim X As Long
 
  For X = 1 To 10
    Columns(X).ColumnWidth = Sheets("Kaptnota").Columns(X).ColumnWidth
  Next X
 

End Sub
Hello

For whatever reason that does not seem to be working
 
Upvote 0
What was the exact error message you are getting initially?

Which module exactly are you placing this code into?

Which sheet is the active sheet when you run this code?
 
Upvote 0
I was able to find a work around.
What was it?
Can you please post what the issue was/how you resolved it?
It may help someone else down the road.
 
Upvote 0
What was it?
Can you please post what the issue was/how you resolved it?
It may help someone else down the road.
Hello

So my workaround may be a little bit low tech, but here it is.
  1. I used "Worksheets("DOLD").Range("D2") = Worksheets("Kapnota").Range("A5").ColumnWidth" to enter the column width value to a cell in a secondary tab
  2. Later on in the MACRO code I used "Range("a:a").ColumnWidth = Worksheets("DOLD").Range("D2")"
  3. Now any changes made to the column widths of to the sheet "KAPNOTA", those changes will be executed to all the tabs the code is in
 
Upvote 0
OK, thanks for posting that.

If you are still interested in pursuing this further, and finding out why it wasn't working previously, please answer the 3 questions I asked up in post 4.
If you are happy to "leave well-enough alone", then you do not have to, and can just mark your last post (post 7) as the solution.
 
Upvote 0
The code I posted required you to have the sheet you want to change the columns activated.
 
Upvote 0
The code I posted required you to have the sheet you want to change the columns activated.
Yeah, I am guessing that they may not have been on the right sheet when the code is run, which is why I asked that question.
And that explains why it works when they add a sheet reference to their range reference.
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,949
Latest member
Dupuhini

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