Autofit Column width works of first sheet but not second sheet

Gowapiti

New Member
Joined
Jun 3, 2019
Messages
4
I am trying to make my table columns autofit to width. I have the following code which works on the first sheet with Table1 but after the next sheet is copied and becomes the active sheet the next line errors out with "Application defined or object defined error" and will not autofit the columns on Sheet 2

Code:
Private Sub SAVE_QUOTE_Click()
        
Dim Quotation As Worksheet: Set Quotation = ThisWorkbook.Sheets("Quotation")
     
    ActiveSheet.Unprotect
    Quotation.Range("I4") = Quotation.Range("I4") + 1
    
    Application.DisplayAlerts = False
    
    'Copy Quote to a New Workbook
    Dim NewFN As Variant
    Dim wb As Workbook
    Set wb = Workbooks.Add
    ThisWorkbook.Sheets("Quotation").Copy After:=wb.Sheets(1)
    ActiveSheet.ListObjects(1).Range.Columns.AutoFit
    ThisWorkbook.Sheets("Internal Use Only").Copy After:=wb.Sheets(2)
    ActiveSheet.ListObjects(1).Range.Columns.AutoFit
    Set wb = Nothing
    
    Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
    'Sheets("Sheet3").Activate
      
    ActiveWindow.SelectedSheets.Delete
[\code]

The remainder of my save function works like it should, I just need to resize the columns on this 2nd sheet.

Thanks!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Works for me.
Any difference between the 2 sheets?
Executes the macro in a new book, with the leaves named "Quotation" and "Internal Use Only" each sheet with a table.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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