+ Reply to Thread
Results 1 to 5 of 5

Copy/Pasting and then Formatting a Table

Hybrid View

  1. #1
    Registered User
    Join Date
    09-18-2017
    Location
    NH, USA
    MS-Off Ver
    2016
    Posts
    4

    Copy/Pasting and then Formatting a Table

    Hello,

    I am working on a program that does a whole bunch of analysis over 16 sheets of 640x512 pixel values and then copies the data of interest in a new sheet and formats it as a table to be imported into a word template so that it will be easy for the user to get the report of the information that is needed without having to do the analysis and sort through the excel document. I have the program able to load and analyse the data, save the document and do everything I need. I am having trouble importing into word but that will be on a different thread. The problem I am posting to help solve is that I am looking for a cleaner way to structure the following code:
    Sub sbCreateTable()
             
        Dim tbl As ListObject
             
        Sheets.Add After:=Sheets("Std. Dev.")
        Sheets(18).Name = "Report"
        Sheets("Report").Select
        Range("A1").Value = "Channel"
        Range("A2").Value = 0
        Range("A3").Value = 1
        Range("A2:A3").Select
        Selection.AutoFill Destination:=Range("A2:A17"), Type:=xlFillDefault
        Range("A2:A17").Select
        
        
        Range("B2").Value = "=Std. Dev.'!D519"
        Range("B3").Value = "=Std. Dev.'!D520"
        Range("B4").Value = "=Std. Dev.'!D521"
        Range("B5").Value = "=Std. Dev.'!D522"
        Range("B6").Value = "=Std. Dev.'!D523"
        Range("B7").Value = "=Std. Dev.'!D524"
        Range("B8").Value = "=Std. Dev.'!D525"
        Range("B9").Value = "=Std. Dev.'!D526"
        Range("B10").Value = "=Std. Dev.'!D527"
        Range("B11").Value = "=Std. Dev.'!D528"
        Range("B12").Value = "=Std. Dev.'!D529"
        Range("B13").Value = "=Std. Dev.'!D530"
        Range("B14").Value = "=Std. Dev.'!D531"
        Range("B15").Value = "=Std. Dev.'!D532"
        Range("B16").Value = "=Std. Dev.'!D533"
        Range("B17").Value = "=Std. Dev.'!D534"
        
        
        Range("B2:B3").Select
        Selection.AutoFill Destination:=Range("B2:B17"), Type:=xlFillDefault
        Range("B2:B17").Select
            
            
        Range("B1").Value = "Noise"
        Range("A1:B17").Select
        Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes)
        tbl.TableStyle = "TableStyleMedium15"
    The problem I am running into is that it is giving me an error on the first "" Range("B2").Value ="Std. Dev.'!D534" "".
    The error is: "application-defined or object-defined Error."
    I wasn't getting this error when I ran it 3 days ago but not I seem to be having troubles with it.

    Is there a cleaner way to do this? I really hate the 'Copy and Paste' Method but I couldn't figure out of to get the auto fill to cycle through the referenced cells that are in the Formula too.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,962

    Re: Copy/Pasting and then Formatting a Table

    You forgot the leading single quote for the linking formula:

    Sub sbCreateTable2()
        
        Dim tbl As ListObject
        Dim Sh As Worksheet
        
        Set Sh = Sheets.Add(After:=Sheets("Std. Dev."))
        Sh.Name = "Report"
        With Sh
            .Range("A1").Value = "Channel"
            .Range("B1").Value = "Noise"
            With .Range("A2:A17")
                .Formula = "=ROW()-2"
                .Value = .Value
            End With
            
            .Range("B2:B17").Formula = "='Std. Dev.'!D519"
            
            Set tbl = .ListObjects.Add(xlSrcRange, .Range("A1:B17"), , xlYes)
            tbl.TableStyle = "TableStyleMedium15"
        End With
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    09-18-2017
    Location
    NH, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: Copy/Pasting and then Formatting a Table

    Thank you Bernie. This code looks way cleaner and is executed much faster. The issue I am having now however is ( And I should have said this in my original post) The data I am copying from the Std. Dev. sheet is horizontal and I need it to be pasted into a vertical orientation from left to right to top to bottom respectively. Is there a transpose function?

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,962

    Re: Copy/Pasting and then Formatting a Table

    Try just changing

            .Range("B2:B17").Formula = "='Std. Dev.'!D519"
    to

            .Range("B2:B17").Formula = "=INDEX('Std. Dev.'!$D$519:$S$519,1,ROW(A1))"

  5. #5
    Registered User
    Join Date
    09-18-2017
    Location
    NH, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: Copy/Pasting and then Formatting a Table

    Thank You, That edit worked perfectly! Got my project back on track!

+ 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. Replies: 2
    Last Post: 06-08-2016, 06:25 AM
  2. Copy and Pasting Nightmare - Help with Formatting
    By Caleb9124 in forum Excel General
    Replies: 3
    Last Post: 01-24-2014, 06:25 PM
  3. Replies: 0
    Last Post: 09-12-2013, 05:21 AM
  4. Copy & Pasting & Conditional Formatting
    By Barking_Mad in forum Excel General
    Replies: 2
    Last Post: 09-15-2009, 05:15 AM
  5. Copy and Pasting without the formatting
    By discomute in forum Excel General
    Replies: 4
    Last Post: 11-19-2008, 12:30 AM
  6. Copy&pasting coditional formatting... again
    By skatmandu2002 in forum Excel General
    Replies: 4
    Last Post: 03-20-2008, 01:00 PM
  7. Table formatting for pasting in Word?
    By astro_al in forum Excel General
    Replies: 0
    Last Post: 02-07-2005, 10:03 AM

Tags for this Thread

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