keep formatting and borders when copy specific cells from multiple files

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
hi experts
I got this code from this thread Macro to Extract Specific cell data from Multiple Worksbooks into one summary.and mod what I need , but the problem deletes the formatting and borders from summary file .if there is way to keep borders and formatting in file summary when run the macro without delete them . that will treat this problem . any idea,please?
Code:
Public Sub Copy_Values_From_Workbooks()

    Dim matchWorkbooks As String
    Dim destSheet As Worksheet, r As Long
    Dim folderPath As String
    Dim wbFileName As String
    Dim fromWorkbook As Workbook
 
    'Folder path and wildcard workbook files to import cells from
 
    matchWorkbooks = "D:\path\*.xlsx"                                             'CHANGE THIS
 
    'Define destination sheet
 
    Set destSheet = ActiveWorkbook.Worksheets("Summary")                                'CHANGE THIS
 
    destSheet.Cells.Clear
    r = 0
 
    Application.ScreenUpdating = False
         
    folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
    wbFileName = Dir(matchWorkbooks)
    While wbFileName <> vbNullString
        Set fromWorkbook = Workbooks.Open(folderPath & wbFileName)
        With fromWorkbook.Worksheets(1)
            destSheet.Range("B4").Offset(r).Value = .Range("B4").Value
            destSheet.Range("C4").Offset(r).Value = .Range("H4:H5").Value
            destSheet.Range("E4").Offset(r).Value = .Range("H19").Value
           
            r = r + 1
        End With
        fromWorkbook.Close savechanges:=False
        DoEvents
        wbFileName = Dir
    Wend
 
    Application.ScreenUpdating = True
 
    MsgBox "Finished"
 
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,​
use the Range.Copy method rather than just allocating a value …​
 
Upvote 0

In order to see where you failed just activate the Macro Recorder and operate manually …​
 
Upvote 0
I fix it , but not entire solution. I changed this

VBA Code:
    destSheet.Cells.Clear

to this

Code:
 destSheet.Cells.ClearContents

but it remains one thing . it deletes the headers in row3 . how avoid that? ideas , please?
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top