+ Reply to Thread
Results 1 to 4 of 4

Copy formats and values instead of just values with existing code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Copy formats and values instead of just values with existing code

    Hi everyone,

    I have the following code which does exactly what I need it to do, but copies only worksheet values:

    Sub Stuff()
    Application.DisplayAlerts = False
        Dim SummarySheet As Worksheet
        Dim FolderPath As String
        Dim SelectedFiles() As Variant
        Dim NRow As Long
        Dim FileName As String
        Dim NFile As Long
        Dim WorkBk As Workbook
        Dim SourceRange As Range
        Dim DestRange As Range
        Dim Lastrow As Long
    
        Set SummarySheet = Worksheets("Sheet1")
        
        FolderPath = "C:\"
        
        ChDrive FolderPath
        ChDir FolderPath
        
        SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
    
        NRow = 1
        
        For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
            FileName = SelectedFiles(NFile)
            
            Set WorkBk = Workbooks.Open(FileName)
            
            SummarySheet.Range("A" & NRow).Value = FileName
        
        Lastrow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
            After:=WorkBk.Worksheets(1).Cells.Range("A1"), _
            SearchDirection:=xlPrevious, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows).Row
        
            Set SourceRange = WorkBk.Worksheets("ABC").Range("A1:Y" & Application.Min(47, Lastrow))
            
            Set DestRange = SummarySheet.Range("A" & NRow)
            Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)
               
            DestRange.Value = SourceRange.Value
    
            NRow = NRow + DestRange.Rows.Count
            
            WorkBk.Close savechanges:=False
        Next NFile
        
        SummarySheet.Columns.AutoFit
    Application.DisplayAlerts = True
    End Sub
    What I'd like to do is have this code copy both values AND formats from each workbook selected.

    Any ideas?

    Thanks!!

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Copy formats and values instead of just values with existing code

    Replace this...
    DestRange.Value = SourceRange.Value
    With this (it copies formulas as well)...
    SourceRange.Copy Destination:=DestRange
    Or this (Copies only values and formats, Not formulas)...
    SourceRange.Copy
    DestRange.PasteSpecial xlPasteValuesAndNumberFormats
    DestRange.PasteSpecial xlPasteFormats
    Last edited by AlphaFrog; 12-14-2016 at 02:31 PM. Reason: typo
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Copy formats and values instead of just values with existing code

    That's fantastic. Thanks, Alphafrog. I noticed that while the formats are perfect, the column widths are not. Can it be set so the column widths carry over with the copy?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Copy formats and values instead of just values with existing code

    SourceRange.Copy
    DestRange.PasteSpecial xlPasteColumnWidths
    DestRange.PasteSpecial xlPasteValuesAndNumberFormats
    DestRange.PasteSpecial xlPasteFormats
    If you highlight the PasteSpecial keyword in the code and press F1, you'll get context help. Select xlPasteType in the help window to see a list of the paste options.

+ 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. [SOLVED] Copy Values and Formats to a new WB
    By dkoucky in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-03-2013, 05:36 PM
  2. Copy ALL worksheets (values and formats only) to new workbook
    By Tomkat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-14-2013, 06:39 AM
  3. My macro won't copy values and formats
    By Taffyb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-28-2013, 11:51 AM
  4. Code tweak needed: Copy macro not copying formats only values (with getobject)
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-19-2012, 01:06 PM
  5. [SOLVED] Copy and paste special values and formats
    By Blake 7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2012, 07:14 AM
  6. Copy worksheet values and formats into another workbook
    By bishwino in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-28-2005, 01:19 PM
  7. Copy worksheet to new workbook : Values & Formats
    By Franck in forum Excel General
    Replies: 1
    Last Post: 10-26-2005, 10:05 AM
  8. VBA Code to copy values &formats to the next worksheet
    By bobby in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-19-2005, 10: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