Insert AutoFit Column A into existing VBA.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greeings I have a VBA that works perfectly fine.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Application.Intersect(Target, Columns("T")) Is Nothing Then
        With Target
            If .CountLarge = 1 Then
                Select Case UCase(.Value)
                Case "1 COP": .Offset(0, -10).Value = 2300
                Case "2 COPS": .Offset(0, -10).Value = 4600
                Case "3 COPS": .Offset(0, -10).Value = 6900
                Case "4 COPS": .Offset(0, -10).Value = 9200
                End Select
            End If
        End With
    End If
End Sub

I would like to insert something like
Excel Formula:
Application.EnableEvents = False
Range("A1").EntireColumn.AutoFit

but not sure without ruining the first Macro. This addition piece will autofit the Column A.

Thank you very much indeed.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Did you try adding this to your code
VBA Code:
Range("A1").EntireColumn.AutoFit
 
Upvote 0
How about ...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    With Excel.Application

        If Not .Intersect(Target, Me.Columns("A")) Is Nothing Then
            Me.Columns("A").AutoFit
        End If

        If Not .Intersect(Target, Me.Columns("T")) Is Nothing Then
            .EnableEvents = False
            With Target
                If .CountLarge = 1 Then
                    Select Case UCase(.Value)
                    Case "1 COP": .Offset(0, -10).Value = 2300
                    Case "2 COPS": .Offset(0, -10).Value = 4600
                    Case "3 COPS": .Offset(0, -10).Value = 6900
                    Case "4 COPS": .Offset(0, -10).Value = 9200
                    End Select
                End If
            End With
            .EnableEvents = True
        End If

    End With
End Sub
 
Upvote 0
Solution
How about ...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    With Excel.Application

        If Not .Intersect(Target, Me.Columns("A")) Is Nothing Then
            Me.Columns("A").AutoFit
        End If

        If Not .Intersect(Target, Me.Columns("T")) Is Nothing Then
            .EnableEvents = False
            With Target
                If .CountLarge = 1 Then
                    Select Case UCase(.Value)
                    Case "1 COP": .Offset(0, -10).Value = 2300
                    Case "2 COPS": .Offset(0, -10).Value = 4600
                    Case "3 COPS": .Offset(0, -10).Value = 6900
                    Case "4 COPS": .Offset(0, -10).Value = 9200
                    End Select
                End If
            End With
            .EnableEvents = True
        End If

    End With
End Sub
Perfect Thank you,
 
Upvote 0
Glad we could be of some help and thanks for letting us know (y)
 
Upvote 0

Forum statistics

Threads
1,214,537
Messages
6,120,096
Members
448,944
Latest member
SarahSomethingExcel100

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