ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   workbook macro help (https://www.excelbanter.com/excel-discussion-misc-queries/2522-workbook-macro-help.html)

ditchy

workbook macro help
 
Hello there, I would like some help with a macro that I need to run in
a workbook. this is the macro.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Columns("A:Z").EntireColumn.AutoFit
End Sub
what or how do I get it to work for every worksheet in the workbook, I
currently place the macro in each worksheet which is very time
consuming. Is there a way to place the macro in the workbook so all
worksheets have the autofit macro?
regards
Ditchy


KL

Hi ditchy,

Try putting the following code into the ThisWorkbook module (replace the
names of the sheets to single out as necessary).
Rgards,
KL

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
For Each s In ThisWorkbook.Worksheets
Select Case s.Name
Case "Sheet1", "Sheet2" 'Single out sheets 1 and 2.
Case Else: s.Columns("A:Z").EntireColumn.AutoFit
End Select
Next s
End Sub


"ditchy" wrote in message
ups.com...
Hello there, I would like some help with a macro that I need to run in
a workbook. this is the macro.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Columns("A:Z").EntireColumn.AutoFit
End Sub
what or how do I get it to work for every worksheet in the workbook, I
currently place the macro in each worksheet which is very time
consuming. Is there a way to place the macro in the workbook so all
worksheets have the autofit macro?
regards
Ditchy





All times are GMT +1. The time now is 10:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com