Retaining Column Width when using macro to split data into separate workbooks

LeighBarber

New Member
Joined
Aug 30, 2017
Messages
2
Ive been using the attached macro which i found on these boards and fiddled with a little in order to get it to do what i wanted, which was export filtered data into its own worksheet based on specified criteria, and it works 99% perfectly.

The one thing Im not able to work out how to do it how i get the data created in the new workbooks to retain the column size of the data from the master sheet. Currently it exports and each column is the same default size which is to small.

I would like to either get it to import in the column width of the original data or specify what size i want the columns to be.

Any ideas?

Code:
Sub WorkbookNew()
Dim wbNew As Workbook
Dim wsData As Worksheet
Dim wsCrit As Worksheet
Dim wsNew As Worksheet
Dim rngCrit As Range
Dim LastRow As Long
   
    Set wsData = Worksheets("Master (2)") ' name of worksheet with the data
    Set wsCrit = Worksheets.Add
   
    LastRow = wsData.Range("A" & Rows.Count).End(xlUp).Row
   
    ' column H has the criteria
    wsData.Range("H1:H" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wsCrit.Range("A1"), Unique:=True
   
    Set rngCrit = wsCrit.Range("A2")
    While rngCrit.Value <> ""
        Set wsNew = Worksheets.Add
        ' change E to reflect columns to copy
        wsData.Range("A1:E" & LastRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rngCrit.Offset(-1).Resize(2), CopyToRange:=wsNew.Range("A1"), Unique:=True
        wsNew.Name = rngCrit
        wsNew.Copy
        Set wbNew = ActiveWorkbook
        ' saves new workbook in path of existing workbook
        wbNew.SaveAs ThisWorkbook.Path & "\" & rngCrit
        wbNew.Close SaveChanges:=True
        Application.DisplayAlerts = False
        wsNew.Delete
        rngCrit.EntireRow.Delete
        Set rngCrit = wsCrit.Range("B2")
    Wend
   
    wsCrit.Delete
    Application.DisplayAlerts = True

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Because you're only copying cells rather than full columns it won't bring across the row heights/column widths etc. Might be easier to just code the default column sizes to be set in your coding rather than trying to get and copy across from the data as it doesn't seem to have many columns. You can set the widths using

Code:
[COLOR=#000000][FONT=Menlo]Worksheets([/FONT][/COLOR][COLOR=#A31515][FONT=Menlo]"Sheet1"[/FONT][/COLOR][COLOR=#000000][FONT=Menlo]).Columns([/FONT][/COLOR][COLOR=#A31515][FONT=Menlo]"A"[/FONT][/COLOR][COLOR=#000000][FONT=Menlo]).ColumnWidth = 24 ' etc[/FONT][/COLOR]
 
Upvote 0
Code:
[COLOR=#000000][FONT=Menlo]Worksheets([/FONT][/COLOR][COLOR=#A31515][FONT=Menlo]"Sheet1"[/FONT][/COLOR][COLOR=#000000][FONT=Menlo]).Columns([/FONT][/COLOR][COLOR=#A31515][FONT=Menlo]"A"[/FONT][/COLOR][COLOR=#000000][FONT=Menlo]).ColumnWidth = 24 ' etc[/FONT][/COLOR]
[/QUOTE]

Thanks for the help, but where in the code would i enter this section?
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,679
Members
449,463
Latest member
Jojomen56

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