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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
would this work?

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

Forum statistics

Threads
1,215,391
Messages
6,124,674
Members
449,179
Latest member
fcarfagna

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