copy column width using SpecialCells

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Still struggling a bit. I can run the code below but the target workbooks columns are not sized the same as the primary sheet. There seems to be many ways to copy so i think im trying to mix them is why i keep getting a object error.

Any suggestion would be helpful

in the code below i've commented out the offending line - my attempt - so the macro will run

'data_sh.usedRange.SpecialCells(xlCellTypeVisible).ColumnWidth.Copy nsh.Range("A1")

Thanks


Code:
Sub Split_Data_in_Workbooks()


Application.ScreenUpdating = False


Dim usedRange As Range


Dim data_sh As Worksheet
Set data_sh = ThisWorkbook.Sheets("Data")


Dim setting_Sh As Worksheet
Set setting_Sh = ThisWorkbook.Sheets("Settings")


Dim nwb As Workbook
Dim nsh As Worksheet


'''' Get unique supervisors


setting_Sh.Range("A:A").Clear
data_sh.AutoFilterMode = False
data_sh.Range("B:B").Copy setting_Sh.Range("A1")


setting_Sh.Range("A:A").RemoveDuplicates 1, xlYes


Dim i As Integer


For i = 2 To Application.CountA(setting_Sh.Range("A:A"))


    data_sh.usedRange.AutoFilter 2, setting_Sh.Range("A" & i).Value
    
    Set nwb = Workbooks.Add
    Set nsh = nwb.Sheets(1)
    
    data_sh.usedRange.SpecialCells(xlCellTypeVisible).Copy nsh.Range("A1")
    
    'data_sh.usedRange.SpecialCells(xlCellTypeVisible).ColumnWidth.Copy nsh.Range("A1")  'Danny'
         
    nwb.SaveAs setting_Sh.Range("H6").Value & "/" & setting_Sh.Range("A" & i).Value & ".xlsx"
    nwb.Close False
    data_sh.Range("A:A").Clear


Next i


setting_Sh.Range("A:A").Clear
data_sh.AutoFilterMode = False


MsgBox "Done"


End Sub
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello,

Why not :

Code:
sh.Range("A1")EntireColumn.Autofit<code class="plain"></code>

Hope this will help
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,966
Members
452,539
Latest member
delvey

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