Column.Autofit not working

Guinaba

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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,899
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.
 
Upvote 0

mohadin

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

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
59,294
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?
 
Upvote 0
Solution

Forum statistics

Threads
1,190,590
Messages
5,981,816
Members
439,736
Latest member
Nathan20

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