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:
Ok, I had tried it there before and didn't work, working now, maybe after excel was restarted it refreshed something. DTpicker still not working whereas it was before, it's taking all rows and not the ones selected by the DT pickers.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
As the DTpicker code hasn't changed, something else must have, but with out being able to see your data It's difficult to know what.
That said, I don't understand what this part of your code is meant to be doing
Code:
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
 
Upvote 0
Code:
For i = 2 To lastrow [COLOR=#008000]'looking at all rows in worksheet("TGT") from 2 to lastrow, row 2 is the header row[/COLOR]
        sheetdate = ws.Cells(3, 2).Value 'th[COLOR=#008000]is is where the dates in the column start[/COLOR]
        If sheetdate >= startdate And sheetdate <= enddate Then [COLOR=#008000]'se[/COLOR][COLOR=#008000]ts the parameters to copy rows[/COLOR]
        ws.Range(ws.Cells(i, 1), ws.Cells(i, 15)).Copy Destination:=wb.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1) [COLOR=#008000]'copys rows to new workbook[/COLOR]
        End If
    Next i
 
Last edited:
Upvote 0
Sheetdate doesn't change as it's looking at a static cell ie B3.
So you will either copy all rows from a sheet or none.
 
Upvote 0
so how would I fix it, I tried code below and get error Type Mismatch

Code:
sheetdate = ws.Cells(i, 2).Value

how can I fix this code to copy all rows that are >= startdate and <= enddate?


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
        
    startdate = Me.DTPicker1.Value
    enddate = Me.DTPicker2.Value
    lastrow = ws.UsedRange.Rows.Count
    For i = 2 To lastrow
        [COLOR=#ff0000]sheetdate = ws.Cells(i, 2).Value [/COLOR][COLOR=#008000]'Type Mismatch[/COLOR]
        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
    
        ws.Cells.Copy
        With wb.Sheets(1).Cells
            .PasteSpecial Paste:=xlPasteColumnWidths
            .PasteSpecial Paste:=xlPasteFormats
            Application.CutCopyMode = False
        End With
        
    wb.SaveAs ThisWorkbook.Path & "\Target Log " & Format(enddate, "dd_mm_yyyy") & ".xlsm", 52
End Sub

What I don't understand is that the code was working before I tried to add the save as code.
 
Last edited:
Upvote 0
What is the value of i when you get the error, and what is in that cell?
so if i was 2 what is in B2
 
Upvote 0
B2 on the "TGT worksheet is the Date header, the dates start in B3
 
Upvote 0
Then try
Code:
 For i = 3 To lastrow
 
Upvote 0
I lose the headers with that code, but the DTpicker dates work properly. The headers on worksheet("TGT") are on row 2 and would like them to copy over as well. So just to recap, on worksheet ("TGT") the headers are in row 2, the data I want to copy starts on row 3 to last row, I want to copy all rows that are = to and between the start and end dates that are selected via the dtpickers.
 
Last edited:
Upvote 0
Try
Code:
   Ws.Range("A2:O2").Copy wb.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
   For i = 3 To lastrow
      sheetdate = Ws.Cells(i, 2).Value 'Type Mismatch
      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
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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