Column.Autofit not working

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
175
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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
63,315
Office Version
  1. 365
Platform
  1. Windows
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.
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,401
Office Version
  1. 2013
Platform
  1. Windows
May be
VBA Code:
Worksheets("EmailFormat"). Columns(12).EntireColumn.AutoFit
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
54,382
Office Version
  1. 365
Platform
  1. Windows
Could the data (or at least one item of data) that you have pasted in column 12 contain trailing spaces?
 
Solution

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
175
Office Version
  1. 2016
Platform
  1. Windows
Thanks @Peter_SSs! Spot on! I've removed the trailing spaces and it is working now.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
54,382
Office Version
  1. 365
Platform
  1. Windows
Cheers. Glad we could help. :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,268
Messages
5,836,301
Members
430,419
Latest member
silvaleroy

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
Top