Guinaba
Board Regular
- Joined
- Sep 19, 2018
- Messages
- 191
- Office Version
-
- 2016
- Platform
-
- 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?
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