+ Reply to Thread
Results 1 to 7 of 7

copy range and paste to new worksheet

  1. #1
    Moon
    Guest

    copy range and paste to new worksheet

    Hi All,
    I'm stuck with my code to copy a range of cells and insert into new
    worksheet with the name of the old worksheet. Also, I want to keep the
    format of the original range of cells but not sure how to do so. Can
    anyone help.
    Thanks,Moon

    Sub Format_All_Worksheets()
    Dim sh As Worksheet
    Dim Newsh As Worksheet


    For Each sh In ThisWorkbook.Worksheets


    Set Newsh = ThisWorkbook.Worksheets.Add

    With sh.Range("A19:G89")

    sh.Range("A19:G89").Cut Newsh.Cells
    Newsh.Name = sh.Name


    End With


    Next sh
    End Sub


  2. #2
    Dave Peterson
    Guest

    Re: copy range and paste to new worksheet

    If you add a new worksheet to the same workbook, then it can't have the same
    name as a worksheet already in that workbook.

    If you're trying to create a new workbook with lots of worksheets (with the same
    name) but only A19:G89 copied, maybe something like:

    Option Explicit
    Sub Format_All_Worksheets()
    Dim sh As Worksheet
    Dim NewSh As Worksheet
    Dim NewWkbk As Workbook

    Set NewWkbk = Workbooks.Add(1) 'single sheet
    NewWkbk.Worksheets(1).Name = "Deletemelater"

    For Each sh In ThisWorkbook.Worksheets
    Set NewSh = NewWkbk.Worksheets.Add
    sh.Range("A19:G89").Cut _
    Destination:=NewSh.Range("a1")
    NewSh.Name = sh.Name
    Next sh

    Application.DisplayAlerts = False
    NewWkbk.Worksheets("deletemelater").Delete
    Application.DisplayAlerts = True

    End Sub


    Moon wrote:
    >
    > Hi All,
    > I'm stuck with my code to copy a range of cells and insert into new
    > worksheet with the name of the old worksheet. Also, I want to keep the
    > format of the original range of cells but not sure how to do so. Can
    > anyone help.
    > Thanks,Moon
    >
    > Sub Format_All_Worksheets()
    > Dim sh As Worksheet
    > Dim Newsh As Worksheet
    >
    > For Each sh In ThisWorkbook.Worksheets
    >
    > Set Newsh = ThisWorkbook.Worksheets.Add
    >
    > With sh.Range("A19:G89")
    >
    > sh.Range("A19:G89").Cut Newsh.Cells
    > Newsh.Name = sh.Name
    >
    >
    > End With
    >
    >
    > Next sh
    > End Sub


    --

    Dave Peterson

  3. #3
    Moon
    Guest

    Re: copy range and paste to new worksheet

    Hi Dave,
    Ah, that makes sense; not having same worksheet names in same workbook.
    Also, I was wondering how to retain the format of the copied range
    after it is pasted onto a new worksheet. The column widths are narrowed
    when it is pasted onto the new worksheet.


  4. #4
    Dave Peterson
    Guest

    Re: copy range and paste to new worksheet

    If you're using xl2k or higher, you can copy|paste special|columnwidths.

    Option Explicit
    Sub Format_All_Worksheets()
    Dim sh As Worksheet
    Dim NewSh As Worksheet
    Dim NewWkbk As Workbook

    Set NewWkbk = Workbooks.Add(1) 'single sheet
    NewWkbk.Worksheets(1).Name = "Deletemelater"

    For Each sh In ThisWorkbook.Worksheets
    Set NewSh = NewWkbk.Worksheets.Add
    sh.Range("A19:G89").Cut _
    Destination:=NewSh.Range("a1")
    sh.Range("A:g").Copy
    NewSh.Range("a:g").PasteSpecial Paste:=8 'xlPasteColumnWidths
    NewSh.Name = sh.Name
    Next sh

    Application.DisplayAlerts = False
    NewWkbk.Worksheets("deletemelater").Delete
    Application.DisplayAlerts = True

    End Sub

    if you're using xl97 (actually any version):

    Option Explicit
    Sub Format_All_Worksheets()
    Dim sh As Worksheet
    Dim NewSh As Worksheet
    Dim NewWkbk As Workbook
    Dim iCol As Long

    Set NewWkbk = Workbooks.Add(1) 'single sheet
    NewWkbk.Worksheets(1).Name = "Deletemelater"

    For Each sh In ThisWorkbook.Worksheets
    Set NewSh = NewWkbk.Worksheets.Add
    sh.Range("A19:G89").Cut _
    Destination:=NewSh.Range("a1")
    For iCol = 1 To 7
    NewSh.Columns(iCol).ColumnWidth = sh.Columns(iCol).ColumnWidth
    Next iCol
    NewSh.Name = sh.Name
    Next sh

    Application.DisplayAlerts = False
    NewWkbk.Worksheets("deletemelater").Delete
    Application.DisplayAlerts = True

    End Sub

    Moon wrote:
    >
    > Hi Dave,
    > Ah, that makes sense; not having same worksheet names in same workbook.
    > Also, I was wondering how to retain the format of the copied range
    > after it is pasted onto a new worksheet. The column widths are narrowed
    > when it is pasted onto the new worksheet.


    --

    Dave Peterson

  5. #5
    Moon
    Guest

    Re: copy range and paste to new worksheet

    Hi Dave,
    Thanks, that worked although the rows didnt get formatted. I tried
    Newsh.Range("a:g").PasteSpecial Paste:=xlPasteFormats but that copied
    all formats including highlights on certain rows. I guess I could
    change the color back in the copied sheet but is there a way to format
    the columns and rows?
    Thanks...Moon


  6. #6
    Dave Peterson
    Guest

    Re: copy range and paste to new worksheet

    You could use the second version of that code (the one that looped through the
    columns) to loop through the rows.

    Option Explicit
    Sub Format_All_Worksheets()
    Dim sh As Worksheet
    Dim NewSh As Worksheet
    Dim NewWkbk As Workbook
    Dim iCol As Long
    Dim iRow As Long

    Set NewWkbk = Workbooks.Add(1) 'single sheet
    NewWkbk.Worksheets(1).Name = "Deletemelater"

    For Each sh In ThisWorkbook.Worksheets
    Set NewSh = NewWkbk.Worksheets.Add
    sh.Range("A19:G89").Cut _
    Destination:=NewSh.Range("a1")
    For iCol = 1 To 7
    NewSh.Columns(iCol).ColumnWidth = sh.Columns(iCol).ColumnWidth
    Next iCol
    For iRow = 1 To 71 '19 to 89
    NewSh.Rows(iRow).RowHeight = sh.Rows(iRow + 18).RowHeight
    Next iRow
    NewSh.Name = sh.Name
    Next sh

    Application.DisplayAlerts = False
    NewWkbk.Worksheets("deletemelater").Delete
    Application.DisplayAlerts = True

    End Sub

    Moon wrote:
    >
    > Hi Dave,
    > Thanks, that worked although the rows didnt get formatted. I tried
    > Newsh.Range("a:g").PasteSpecial Paste:=xlPasteFormats but that copied
    > all formats including highlights on certain rows. I guess I could
    > change the color back in the copied sheet but is there a way to format
    > the columns and rows?
    > Thanks...Moon


    --

    Dave Peterson

  7. #7
    Moon
    Guest

    Re: copy range and paste to new worksheet

    Hi Dave,
    Thanks for all your help. That worked perfectly!
    Moon


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1