+ Reply to Thread
Results 1 to 5 of 5

How Do I Retain Table Formatting (Cell Width) When Copying Sheet Information?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-30-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    8

    How Do I Retain Table Formatting (Cell Width) When Copying Sheet Information?

    Hi All, (bit of a lurker me).

    So ive been building a new spreadsheet that looks into column G, sees if there is a Yes or No and populates a new sheet(s). It all works..fantastic.

    However The formatting is all over the place. Id like for it to maintain the formatting (cell size) primarily.

    My code is
    Sub Ground_Floor()
    Dim lrow As Long
    Dim i As Long
    Dim sname As String
    
    Application.ScreenUpdating = False
    
    With Worksheets("Ground Level")
        lrow = .Range("G" & .Rows.Count).End(xlUp).Row
        For i = 2 To lrow
            sname = .Range("G" & i).Value
            If Not Evaluate("ISREF('" & sname & "'!A1)") Then
                Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = sname
                .Rows("1:1").Copy Worksheets(sname).Range("A1")
            End If
                .Rows(i & ":" & i).Copy Worksheets(sname).Range("A" & .Rows.Count).End(xlUp).Offset(1, 0)
        Next i
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Any ideas?

    My code/macro works perfectly, just not copying over the cell size meaning the table that is produced crops alot of information, requiring the user to resize it EVERY TIME

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: How Do I Retain Table Formatting (Cell Width) When Copying Sheet Information?

    It looks like your range is in columns A to G and it looks like you copy the information to the last worksheet in the workbook.

    Put the following code at the end of the loop before Next i
    Sheets("Ground Level").Range("A2:G2").Copy
        Sheets(sname).PasteSpecial xlPasteFormats
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    04-30-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: How Do I Retain Table Formatting (Cell Width) When Copying Sheet Information?

    Quote Originally Posted by dflak View Post
    It looks like your range is in columns A to G and it looks like you copy the information to the last worksheet in the workbook.

    Put the following code at the end of the loop before Next i
    Sheets("Ground Level").Range("A2:G2").Copy
        Sheets(sname).PasteSpecial xlPasteFormats


    Hi,

    Thank you for the response, however when adding them two lines of code in i get a "Run-time error'1004'" Error. It's worth noting that the Range has already been defined in the line

    .Rows("1:1").Copy Worksheets(sname).Range("A1")
    So i removed the top line of your code, and added just

    Sheets(sname).PasteSpecial xlPasteFormats
    However, with this i get a "PasteSpecial method of Worksheet class failed" error (also Run-time 1004).

    Any ideas?

    I have provided a dropbox link to download the excel file. The Module in VB to look at it is Module 1 through to 13. Each module does the same action on each sheet, and compiles it into 2 x Yes/No Sheets. The Call macro allows for a button on the Helvar Snags sheet to auto compile, auto save to pdf etc..


    Link: https://www.dropbox.com/s/cbtpv9sxv1...heet.xlsm?dl=0
    (this link expires in 7 days)
    Attached Files Attached Files
    Last edited by kitchenspoon; 04-19-2016 at 03:57 AM.

  4. #4
    Registered User
    Join Date
    04-30-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: How Do I Retain Table Formatting (Cell Width) When Copying Sheet Information?

    If anybody can help with this, they will receive one humorous picture of my cat.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,320

    Re: How Do I Retain Table Formatting (Cell Width) When Copying Sheet Information?

    It's quite inefficient to repeat the same code for every sheet. Just use one routine that takes a sheet as an argument:
    Sub CopyToYesNo(wsFrom As Worksheet)
    
        Dim lrow                        As Long
        Dim i                           As Long
        Dim sname                       As String
    
        Application.ScreenUpdating = False
    
        With wsFrom
            lrow = .Range("G" & .Rows.Count).End(xlUp).Row
            For i = 2 To lrow
                sname = .Range("G" & i).Value
                If Not Evaluate("ISREF('" & sname & "'!A1)") Then
                    Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = sname
                    .Rows("1:1").Copy
                    With Worksheets(sname).Range("A1")
                        .PasteSpecial xlPasteAll
                        .PasteSpecial xlPasteColumnWidths
                    End With
                End If
                .Rows(i & ":" & i).Copy
                With Worksheets(sname).Range("A" & .Rows.Count).End(xlUp).Offset(1, 0)
                    .PasteSpecial xlPasteAll
                    .PasteSpecial xlPasteColumnWidths
                End With
    
            Next i
        End With
    
        Application.ScreenUpdating = True
    
    End Sub
    Then all you need for each sheet is:
    Sub Ground_Floor()
        Call CopyToYesNo(Worksheets("Ground Level"))
    End Sub
    changing the sheet name as appropriate.

    See attached.
    Attached Files Attached Files
    Last edited by romperstomper; 04-19-2016 at 07:12 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Unable to Retain Table Formatting (Cell Width) When Copying Sheet Information
    By kitchenspoon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2016, 08:43 AM
  2. [SOLVED] Excel 2013: Copy Pivot Table Values to New Sheet and Retain Formatting
    By greatjobtoday in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 03-27-2015, 08:07 AM
  3. Replies: 3
    Last Post: 10-31-2013, 04:23 PM
  4. copy and retain information from a data entry sheet to trending sheet
    By thuddleston11 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-22-2012, 11:13 AM
  5. Replies: 0
    Last Post: 08-16-2011, 04:20 PM
  6. Data in cell triggers copying of information to different sheet
    By Jrykiss in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-17-2010, 11:46 PM
  7. retain table formatting when writing excel table to a txt file
    By deanop in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-03-2005, 01:05 AM

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