VBA - copy column widths and formats from one WS to new WB

dlmoore99

Board Regular
Joined
May 20, 2011
Messages
88
Office Version
  1. 2019
Platform
  1. MacOS
I'm trying to copy rows from one worksheet to a new worksheet while keeping the formatting (column widths etc..) of the active worksheet. I'm getting a "Run-time error '438': Object doesn't support this property type or method". How can I fix this code to copy the active sheet formats to the new sheet. Thank you.

Code:
Private Sub cmdCSV_Click()
    Dim lastrow As Long, i As Long, erow As Long
    Dim sheetdate As Date, startdate As Date, enddate As Date
    Dim wb As Workbook
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("TGT")
    Set wb = Workbooks.Add
        
        ws.Cells.Copy
       [COLOR=#FF0000] With wb.Cells 'this is where the error occurs[/COLOR]
            .PasteSpecial Paste:=xlPasteColumnWidths
            .PasteSpecial Paste:=xlPasteFormats
            Application.CutCopyMode = False
        End With
   
    startdate = Me.DTPicker1.Value
    enddate = Me.DTPicker2.Value
    lastrow = ws.UsedRange.Rows.Count
    For i = 2 To lastrow
        sheetdate = ws.Cells(3, 2).Value
        If sheetdate >= startdate And sheetdate <= enddate Then
        ws.Range(ws.Cells(i, 1), ws.Cells(i, 15)).Copy Destination:=wb.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
    Next i
End Sub
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try
Code:
with wb.sheets(1).cells
 
Upvote 0
Sweet, I knew it was something easy and I should be learning this stuff by now, thank you Fluff. Works perfectly. I know you are suppose to open a new thread, but if I may, how can I add a name to the new workbook in the above code. I've tried adding some code but it's not working yet.

Code:
Private Sub cmdCSV_Click()
    Dim lastrow As Long, i As Long, erow As Long
    Dim sheetdate As Date, startdate As Date, enddate As Date
    Dim wb As Workbook
    Dim ws As Worksheet
    [COLOR=#FF0000]Dim wbName As String
    
    wbName = "Target Log" & Me.DTPicker2.Value[/COLOR]
    
    Set ws = ThisWorkbook.Worksheets("TGT")
    Set wb = Workbooks.Add
        
        ws.Cells.Copy
        With wb.Sheets(1).Cells
            .PasteSpecial Paste:=xlPasteColumnWidths
            .PasteSpecial Paste:=xlPasteFormats
            Application.CutCopyMode = False
        End With
   
    startdate = Me.DTPicker1.Value
    enddate = Me.DTPicker2.Value
    lastrow = ws.UsedRange.Rows.Count
    For i = 2 To lastrow
        sheetdate = ws.Cells(3, 2).Value
        If sheetdate >= startdate And sheetdate <= enddate Then
        ws.Range(ws.Cells(i, 1), ws.Cells(i, 15)).Copy Destination:=wb.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
        End If
    Next i
End Sub
 
Upvote 0
What is the value of wbName?
 
Upvote 0
I was going to call the new workbook "Target Log" and add the date from the enddate "Me.DTPicker2.Value"
 
Upvote 0
You can't name a workbook as such, you need to SaveAs
Code:
wb.SaveAs ThisWorkbook.Path & "\Target Log " & format(Me.dtpicker2.Value, "dd_mm_yyyy") & ".xlsm", 52
 
Upvote 0
At the end of your code.
 
Upvote 0
I've tried multiple locations and not working, getting Run-time error 1004. Also the dtpicker is now selecting and copying rows outside the parameters. The code just froze excel and had to start excel.
 
Last edited:
Upvote 0
Ok, try
Code:
        End If
    Next i
    wb.SaveAs ThisWorkbook.Path & "\Target Log " & format(enddate, "dd_mm_yyyy") & ".xlsm", 52
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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