Avatar of Shanan212
Shanan212Flag for Canada

asked on 

Autofit using excel events

Private Sub Worksheet_Change(ByVal Target As Range)

        Application.EnableEvents = False
        ActiveSheet.Unprotect
        
        ActiveSheet.Columns.AutoFit
        ActiveSheet.EnableAutoFilter = True
        
        ActiveSheet.Protect , DrawingObjects:=True, _
        contents:=True, Scenarios:=True, _
        userinterfaceonly:=True
        Application.EnableEvents = True
End Sub

Open in new window


Hi,

I have the above function, however, it just not working :/

There is no errors but the function should autofit the columns but its not doing it (I even tried outputting a message and its not popping either)

The full function is to include 'autofitting' certain columns only and I have idea like this

activesheet.range("A3","H3").select
selection.columns.autofit

^ Would the above work as well? (if the function starts working?

Thanks!
Microsoft Excel

Avatar of undefined
Last Comment
Rory Archibald
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

You are expecting it to react to an Event when you have switched off Events in line 3.

Thanks
Rob H
Avatar of Shanan212
Shanan212
Flag of Canada image

ASKER

Ok, I removed it (both lines) and still not working. The Sub is in a sheet which is accessed by right clicking on the worksheet name and then clicking on 'view code'
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Shanan212
Shanan212
Flag of Canada image

ASKER

Doing it manually.

Above is also not working.

So I created a new worksheet.

Put in some columns, and tried the above (with the protection stuff disabled) - still no use
Tried with enableEvents stuff removed - no use :/

Thanks for the continues help!
Did you reset events first?
Avatar of Shanan212
Shanan212
Flag of Canada image

ASKER

I put the reset function above the bigger Sub! Also put it in a module and called it from this function  - no use!

Funny thing is, when I was developing the function at the beginning with just 'autofit' line, it worked :/
No - you need to actually run the  reset sub yourself, otherwise the events can never trigger it.
Avatar of Shanan212
Shanan212
Flag of Canada image

ASKER

User generated image
This is the setting I am working
Avatar of Shanan212
Shanan212
Flag of Canada image

ASKER

woops wrong picture

 User generated image
Avatar of Shanan212
Shanan212
Flag of Canada image

ASKER

Aah triggering reset sub myself do works!
No - you don't need to call ResetEvents from the event (that would serve no purpose). You need to actually run ResetEvents yourself once in order to turn events back on.
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo