Macro to open up Multiple Files and Paste Special values retaining formts

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to open up several files and to copy and paste the data

I need the macro amended, so that the data is pasted as values but the original format from the source data is retained

It would be appreciated if someone could kindly amend my code to incorporate this

Code:
 Sub Open_MultipleFiles()
ChDir "C:\My documents"
Dim LR As Long

With Sheets("Imported Data")
LR = .Cells(.Rows.Count, "B").End(xlUp).Row
Range("B2:B" & LR).ClearContents

Dim fDialog As Object, varFile As Variant
Dim nb As Workbook, tw As Workbook, ts As Worksheet
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.CutCopyMode = False
End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set fDialog = Application.FileDialog(3)
ChDir "C:\my Documents"
With fDialog
.Filters.Clear
.Filters.Add "Excel files", "*.xlsm*"
.Show

For Each varFile In .SelectedItems
Set nb = Workbooks.Open(Filename:=varFile, local:=True)
nb.Sheets("Pivot Table").Range("A5:K5").Copy Destination:=ThisWorkbook.Sheets("Imported Data").Range("B" & Rows.Count).End(xlUp).Offset(1)
nb.Close False
Next
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.CutCopyMode = True
End With

 End With
   End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Change this
VBA Code:
nb.Sheets("Pivot Table").Range("A5:K5").Copy Destination:=ThisWorkbook.Sheets("Imported Data").Range("B" & Rows.Count).End(xlUp).Offset(1)

To this

VBA Code:
nb.Sheets("Pivot Table").Range("A5:K5").Copy 
ThisWorkbook.Sheets("Imported Data").Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
ThisWorkbook.Sheets("Imported Data").Range("B" & Rows.Count).End(xlUp).PasteSpecial xlPasteFormats
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,780
Members
449,049
Latest member
greyangel23

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