I want a macro that can let the user autofit with a shortcut when a cell is like ######

gerotutu

New Member
Joined
Jun 19, 2015
Messages
28
Hi! I have been looking and asking for solutions everywhere to find how to let the user autofit the columns when the figures are shown with ########

The best I could find was something like the code below but it doesn't work and on the other hand I want to make the macro work ONLY for the cells that the user selected firstly (not for the whole sheet)

Code:
Private Sub Autofit()
    If Me.ProtectContents = True Then
        Me.Unprotect Password:="test"
        Me.Rows.AutoFit
        Me.Protect Password:="test"
    End If
End Sub

By the way, how can I add also a warning so the user can cancel the procedure before doing any change?

Many many thanks.
Gerónimo

ps: I know that crossposting is not so popular but it's hard to have some help sometimes and I am quite rookie developing excel. Here you will find an old post I made How to autofit only selected cells
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
would this work?

Code:
Sub test()
    Selection.Columns.AutoFit
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,649
Members
449,462
Latest member
Chislobog

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