Consulting

Results 1 to 9 of 9

Thread: Copy Data / Paste Data with Value and Format

  1. #1
    VBAX Regular
    Joined
    May 2017
    Posts
    49
    Location

    Copy Data / Paste Data with Value and Format

    Hello All -

    I am using the following code (credit to Ron de Bruin) to copy and paste data from different workbooks in a particular folder. The code copies the data range from the first worksheet in each workbook and pastes the data into the workbook that has the macro. This piece of code works super well. The only item that I can't figure out is how to paste the data with the format from the other worksheets. I am hoping someone can take a quick look at the code and provide guidance on how to copy the values along with the format. Thanks in advance for the help.

    Sub MergeAllWorkbooks_2()
        Dim MyPath As String, FilesInPath As String
        Dim MyFiles() As String
        Dim SourceRcount As Long, FNum As Long
        Dim mybook As Workbook, BaseWks As Worksheet
        Dim sourceRange As Range, destrange As Range
        Dim rnum As Long, CalcMode As Long
        Dim FirstCell As String
        'Fill in the path\folder where the files are
        MyPath = "C:\Data\"
        'Add a slash at the end if the user forget it
        If Right(MyPath, 1) <> "\" Then
            MyPath = MyPath & "\"
        End If
        'If there are no Excel files in the folder exit the sub
        FilesInPath = Dir(MyPath & "*.xl*")
        If FilesInPath = "" Then
            MsgBox "No files found"
            Exit Sub
        End If
        'Fill the array(myFiles)with the list of Excel files in the folder
        FNum = 0
        Do While FilesInPath <> ""
            FNum = FNum + 1
            ReDim Preserve MyFiles(1 To FNum)
            MyFiles(FNum) = FilesInPath
            FilesInPath = Dir()
        Loop
        'Change ScreenUpdating, Calculation and EnableEvents
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        'Add a new workbook with one sheet
        Set BaseWks = ThisWorkbook.Worksheets(2)
        rnum = 1
        'Loop through all files in the array(myFiles)
        If FNum > 0 Then
            For FNum = LBound(MyFiles) To UBound(MyFiles)
                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
                On Error GoTo 0
                If Not mybook Is Nothing Then
                    On Error Resume Next
                    With mybook.Worksheets(1)
                        FirstCell = "A2"
                        Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3, .Cells))
                        'Test if the row of the last cell >= then the row of the FirstCell
                        If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
                            Set sourceRange = Nothing
                        End If
                    End With
                    If Err.Number > 0 Then
                        Err.Clear
                        Set sourceRange = Nothing
                    Else
                        'if SourceRange use all columns then skip this file
                        If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                            Set sourceRange = Nothing
                        End If
                    End If
                    On Error GoTo 0
                    If Not sourceRange Is Nothing Then
                        SourceRcount = sourceRange.Rows.Count
                        If rnum + SourceRcount >= BaseWks.Rows.Count Then
                            MsgBox "Sorry there are not enough rows in the sheet"
                            BaseWks.Columns.AutoFit
                            mybook.Close savechanges:=False
                            GoTo ExitTheSub
                        Else
                            'Copy the file name in column A
                            With sourceRange
                                BaseWks.Cells(rnum, "A"). _
                                        Resize(.Rows.Count).Value = MyFiles(FNum)
                            End With
                            'Set the destrange
                            Set destrange = BaseWks.Range("B" & rnum)
                            'we copy the values from the sourceRange to the destrange
                            With sourceRange
                                Set destrange = destrange. _
                                                Resize(.Rows.Count, .Columns.Count)
                            End With
                            destrange.Value = sourceRange.Value
                            rnum = rnum + SourceRcount
                        End If
                    End If
                    mybook.Close savechanges:=False
                End If
            Next FNum
            BaseWks.Columns.AutoFit
        End If
    ExitTheSub:
        'Restore ScreenUpdating, Calculation and EnableEvents
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
    End Sub

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Please use code tags (# button) when posting code
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Instead of
    destrange.Value = SourceRange.Value
    use
    SourceRange.Copy
    destrange.PasteSpecial xlValues
    destrange.PasteSpecial xlFormats
    Application.CutCopyMode = False
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    May 2017
    Posts
    49
    Location

    Talking

    Quote Originally Posted by mdmackillop View Post
    Please use code tags (# button) when posting code
    Thank you again for your assistance. I will make sure to use the proper code tags.

  5. #5
    VBAX Regular
    Joined
    May 2017
    Posts
    49
    Location
    One more item please. I want to remove .xlsx from the file name when it's being copied over. I used this and it works for files that have the .xlsx extension. For files with .xls, it's not being removed even when I added an asterisk.

    With sourceRange
    BaseWks.Cells(rnum, "A"). _
    Resize(.Rows.Count).Value = Replace(MyFiles(FNum), ".xls*", "")

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MyRange.Replace What:=".xls*", Replacement:="", LookAt:=xlPart
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    May 2017
    Posts
    49
    Location
    Thank you again. I added this at the end:

    'Range("A").Replace What:=".xls*", Replacement:="", LookAt:=xlPart'

    And I am getting a method range of object global failed.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    That works if "A" is a named range, or do you mean Range("A:A")
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    May 2017
    Posts
    49
    Location
    Yeah, the range should be (A:A). Thank you again.

Tags for this Thread

Posting Permissions

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