Column.Autofit not working

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

Just wondering why Column.Autofit is not working in the code below which is copying and pasting special (values and format). I need to autofit the last col (12) in the range. Any suggestion?

VBA Code:
Sub CopyWithFormatting()

'Clean sheet before pasting
Sheets("EmailFormat").Cells.Clear

Dim LastRow As Integer
    
With Worksheets("Email")
    LastRow = .Cells(.Rows.Count, "M").End(xlUp).Row
    Sheets("Email").Range("A3:M" & LastRow).Copy
    
    With Sheets("EmailFormat")
       Range("B2").PasteSpecial xlPasteValues
       Range("B2").PasteSpecial xlPasteFormats
        
       For r = LastRow To 2 Step -1
        If .Range("B" & r) = "N" Then .Range("B" & r).EntireRow.Delete
       Next r
        
    End With
End With

'Last Row after deleting the empty rows
Dim LastRow2 As Integer
With Worksheets("EmailFormat")
    LastRow2 = .Cells(.Rows.Count, "B").End(xlUp).Row
End With

'Formating the range
Dim ConRange As Range
Set ConRange = Worksheets("EmailFormat").Range("C2:N" & LastRow2)

Worksheets("EmailFormat").Columns(12).AutoFit

End Sub

1641981685643.png
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You don't not include the column header in your screen print, so we cannot tell which column is column 12.
But all those columns look autofit to me.

It also seems a little odd to me that you seem to be setting a range named "ConRange" near the end of your code, but don't seem to be doing anything with it.
 
Upvote 0
May be
VBA Code:
Worksheets("EmailFormat"). Columns(12).EntireColumn.AutoFit
 
Upvote 0
Could the data (or at least one item of data) that you have pasted in column 12 contain trailing spaces?
 
Upvote 0
Solution

Forum statistics

Threads
1,214,522
Messages
6,120,019
Members
448,938
Latest member
Aaliya13

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