Copy and Save with VBA keep source coloumn widht and row height. is possible?

pasaico

New Member
Joined
Jan 12, 2018
Messages
6
I'm trying to copy the contents of a sheet into a new .xlsx document.
I can not keep source coloumn widht and row height.:eek:

Code:
For i = 1 To wksht.Range("A" & wksht.Rows.Count).End(xlUp).Row
        Set wb = Application.Workbooks.Add
        dataRange.Copy wb.Sheets(1).Range("A1", wb.Sheets(1).Cells(dataRange.Rows.Count, dataRange.Columns.Count))
        wb.SaveAs filename:=path & wksht.Range("A" & i).Value & ".xlsx"
        wb.Close
    Next i

Left is SOURCE --------> RIGHT is COPY/PASTE from code VBA above
6dbSUt3.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You need to use copy like this example.

Code:
Columns(2).Copy
Columns(3).PasteSpecial xlPasteAll
 
Upvote 0
If you're copying the entire sheet, another option would be
Code:
   For i = 1 To wksht.Range("A" & wksht.Rows.Count).End(xlUp).Row
        Sheets("Sheet1").Copy
        ActiveWorkbook.SaveAs fileName:=path & wksht.Range("A" & i).Value & ".xlsx"
        ActiveWorkbook.Close
    Next i
 
Upvote 0
If you're copying the entire sheet, another option would be
Code:
   For i = 1 To wksht.Range("A" & wksht.Rows.Count).End(xlUp).Row
        Sheets("Sheet1").Copy
        ActiveWorkbook.SaveAs fileName:=path & wksht.Range("A" & i).Value & ".xlsx"
        ActiveWorkbook.Close
    Next i


not work...and i copy with range value.
Thank you
 
Upvote 0
If you are only copying a portion of the sheet, we will need to see your complete code.
 
Upvote 0
If you are only copying a portion of the sheet, we will need to see your complete code.


Code:
Private Sub CommandButton1_Clickl()

    Dim wksht As Worksheet
    Set wksht = ActiveSheet

    Dim path As String
    path = "C:\test\"

    If Len(Dir(path, vbDirectory)) = 0 Then
        MkDir path
    End If

    Dim rngeStart
    Dim rngeEnd

    Set rngeStart = wksht.UsedRange.Find(What:="####", LookIn:=xlValues, LookAt:=xlWhole)
    Set rngeEnd = wksht.UsedRange.FindNext(After:=rngeStart)

    Dim dataRange As Range
    Set dataRange = wksht.Range(rngeStart, rngeEnd)

    Dim wb As Workbook
    Dim i As Long

    For i = 1 To wksht.Range("A" & wksht.Rows.Count).End(xlUp).Row
        Set wb = Application.Workbooks.Add
        dataRange.Copy wb.Sheets(1).Range("A1", wb.Sheets(1).Cells(dataRange.Rows.Count, dataRange.Columns.Count))
        wb.SaveAs filename:=path & wksht.Range("A" & i).Value & ".xlsx"
        wb.Close
    Next i

End Sub
 
Upvote 0
How about
Code:
Private Sub CommandButton1_Clickl()

    Dim wksht As Worksheet
    Set wksht = ActiveSheet

    Dim path As String
    path = "C:\test\"

    If Len(Dir(path, vbDirectory)) = 0 Then
        MkDir path
    End If

    Dim rngeStart
    Dim rngeEnd

    Set rngeStart = wksht.UsedRange.Find(What:="####", lookIn:=xlValues, LookAt:=xlWhole)
    Set rngeEnd = wksht.UsedRange.FindNext(After:=rngeStart)

    Dim dataRange As Range
    Set dataRange = wksht.Range(rngeStart, rngeEnd)

    Dim wb As Workbook
    Dim i As Long

    For i = 1 To wksht.Range("A" & wksht.Rows.Count).End(xlUp).Row
      wksht.Copy
      With ActiveSheet
         .Range(.Cells(1, 1), .Cells(1, rngeStart.Column - 1)).EntireColumn.Delete
         .Rows("1:" & rngeStart.Row - 1).Delete
      End With
        ActiveWorkbook.SaveAs fileName:=path & wksht.Range("A" & i).Value & ".xlsx"
        ActiveWorkbook.Close
    Next i

End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,583
Members
449,174
Latest member
chandan4057

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