+ Reply to Thread
Results 1 to 8 of 8

Not sure how to replicate cell formats along with cell values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-24-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    9

    Not sure how to replicate cell formats along with cell values

    I use the following code to replicate the values of cells in a range in one workbook to the corresponding cells in another workbook:

        ActiveWorkbook.Sheets("Sheet1").Range("D6:K26").Value = _
        ThisWorkbook.Sheets("Sheet1").Range("D6:K26").Value
    Fairly straightforward and works just fine in replicating the cell values.

    However I also need to replicate the cell colour and font colour attributes for each cell in the range. It wouldn't bother me if it replicated all format attributes, but those are the two that matter most.

    Is there a similarly straightforward means to replicate the individual cell's format attributes for the range?

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Not sure how to replicate cell formats along with cell values

    Try this,

    ActiveWorkbook.Sheets("Sheet1").Range("D6:K26").Copy ThisWorkbook.Sheets("Sheet1").Range("D6:K26")
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Registered User
    Join Date
    03-24-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Not sure how to replicate cell formats along with cell values

    Thanks.
    Unfortunately that just wipes out all the content in the range in the original sheet (but leaves the formatting) and doesn't copy anything across to the other sheet (values or formats).

  4. #4
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Not sure how to replicate cell formats along with cell values

    I have no idea what you want b/c in 'YOUR' original post you are not transferring any values into a separate workbook, b/c your original code is copying data from the same workbook and just pasting over the top of that data.

    The below code is one and the same. 'ActiveWorkbook' and 'ThisWorkbook' is the same workbook. I just showed you how to do what you wanted to do and you need to adjust the workbook names accordingly.
    ActiveWorkbook
        ThisWorkbook
    Now if you want copy between workbooks it would look something like this, but 'YOU' have to adjust the workbook destination name appropriately.

    Sub dave()
      Dim wb As Workbook
      
        Set wb = ActiveWorkbook
        Workbooks.Open Filename:="C:\Users\Desktop\book2.xlsx" 'Enter your destination workbooks path here
        
         wb.Sheets("Sheet1").Range("D6:K26").Copy ActiveWorkbook.Sheets("Sheet1").Range("D6:K26")
    
    End Sub

  5. #5
    Registered User
    Join Date
    03-24-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Not sure how to replicate cell formats along with cell values

    Quote Originally Posted by JapanDave View Post
    Try this,

    Sub dave()
      Dim wb As Workbook
      
        Set wb = Workbooks("TPS~v1.5.4")
        ClientFileName = "TPS data_" + Sheets("ClientData").Range("Athlete_Name").Value
        
    '   open up a new workbook and add sheets for copying data to
        Workbooks.Add
        
         wb.Sheets("Sheet1").Range("D6:K26").Copy ActiveWorkbook.Sheets("Sheet1").Range("D6:K26")
    
    End Sub
    Cool, thanks, that works.

    It copies values and font formats, but it isn't copying cell colours though.

    I think the problem might be due to the colours in the original worksheet being created by conditional formatting.

    Which might mean I'll need a different approach to the problem. I have a couple of ideas to try...

  6. #6
    Registered User
    Join Date
    03-24-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Not sure how to replicate cell formats along with cell values

    Ok, forgive me but my original code operates fine, with cells being copied from one workbook to a different workbook. In my code, ActiveWorkbook and ThisWorkbook do not seem to refer to the same workbook.

    For context, the code preceding this activates the first workbook (the one I want to copy from, in this case the workbook called "TPS~v.1.5.4") and then adds a new workbook, the one I want to copy to.

    '   set deafult file name as "TPS data_" plus the client's name as shown
    '   at cell "Ä2" or use "Athlete_Name" range name in the ClientData sheet of TPS
        Workbooks("TPS~v1.5.4").Activate
        ClientFileName = "TPS data_" + Sheets("ClientData").Range("Athlete_Name").Value
        
    '   open up a new workbook and add sheets for copying data to
        Workbooks.Add
    After all the copying is done, the code moves onto to save the new workbook, and give user option to rename from a default and choose a directory.

    For some reason the code I use replicates the data from one to the other just fine. I don't however know why.

  7. #7
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Not sure how to replicate cell formats along with cell values

    Try this,

    Sub dave()
      Dim wb As Workbook
      
        Set wb = Workbooks("TPS~v1.5.4")
        ClientFileName = "TPS data_" + Sheets("ClientData").Range("Athlete_Name").Value
        
    '   open up a new workbook and add sheets for copying data to
        Workbooks.Add
        
         wb.Sheets("Sheet1").Range("D6:K26").Copy ActiveWorkbook.Sheets("Sheet1").Range("D6:K26")
    
    End Sub

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Not sure how to replicate cell formats along with cell values

    I reckon you wanted
    ThisWorkbook.Sheets("Sheet1").Range("D6:K26").Copy Activeworkbook.Sheets("Sheet1").Range("D6")
    rather than vice versa ;-)

    this assumes the cells don't contain formulas
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ 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