VBA CopyPaste Formatting

RedOctoberKnight

Board Regular
Joined
Nov 16, 2015
Messages
150
Office Version
  1. 2016
Platform
  1. Windows
Good Morning,

I have the following code which copies and pastes from one workbook to another. It only pastes the values though and I'm unsure on how to write it so it pastes formatting too.


VBA Code:
Public Sub Copy_Range_From_Truck_Schedules()

       Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim tsWorkbook As Workbook

Set tsWorkbook = Workbooks.Open(ThisWorkbook.Path & "\truckschedulesdualsides.xlsx")
With ThisWorkbook.ActiveSheet
.Range("A4:R200").Value = tsWorkbook.Worksheets(.Name).Range("A4:R200").Value

End With
tsWorkbook.Close False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub

Any help would be much appreciated.

Thanks.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I'm not sure whether you're copying values from cells with formulas, so this code might seem redundant, but hopefully it helps.

VBA Code:
Public Sub Copy_Range_From_Truck_Schedules()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

Dim tsWorkbook As Workbook
Dim rngCopy As Range

Set tsWorkbook = Workbooks.Open _
   (ThisWorkbook.Path & "\truckschedulesdualsides.xlsx")
   
Set rngCopy = tsWorkbook.Worksheets(.Name).Range("A4:R200")
rngCopy.Copy

With ThisWorkbook.ActiveSheet

  .Range("A4").Select
  Selection.PasteSpecial xlPasteValues
  Selection.PasteSpecial xlPasteFormats
  
End With

tsWorkbook.Close False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thank you for your response.

However i'm getting a "invalid or unqualified reference" error. It's highlighting the .Name
 
Upvote 0
Thank you for your response.

However i'm getting a "invalid or unqualified reference" error. It's highlighting the .Name

Sorry about that. I copied your code and didn't realize that the way I restructured that would cause an error. I think it should work if you change .Name to ActiveSheet.Name.
 
Upvote 0
Sorry to be a pain but now I get a range error.

.range("A4").select is giving me an error for some reason.

Thanks,
 
Upvote 0
How about
VBA Code:
Public Sub Copy_Range_From_Truck_Schedules()
   
   Application.Calculation = xlCalculationManual
   Application.ScreenUpdating = False
   Dim tsWorkbook As Workbook
   
   Set tsWorkbook = Workbooks.Open(ThisWorkbook.Path & "\truckschedulesdualsides.xlsx")
   With ThisWorkbook.ActiveSheet
      tsWorkbook.Worksheets(.Name).Range("A4:R200").Copy
      .Range("A4").PasteSpecial xlPasteValues
      .Range("A4").PasteSpecial xlPasteFormats
   End With
   tsWorkbook.Close False
   Application.Calculation = xlCalculationAutomatic
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Fluff, Works perfectly.

Curious how I go about copying the column widths too?

I tried adding .Range("A4").pastespecial xlpastecolumnwidths but I get a range error.
 
Upvote 0
This works for me
VBA Code:
      .Range("A4").PasteSpecial xlPasteValues
      .Range("A4").PasteSpecial xlPasteFormats
      .Range("A4").PasteSpecial xlPasteColumnWidths
 
Upvote 0
Thanks Fluff.

Does the order matter? I had it above the pastevalues originally.

Also, I get an alert of lots of data on the clipboard. Is there a way clear the clipboard once the data is pasted?
 
Upvote 0
It works for me with it anywhere. But I think I've seen posts where it does seem to matter what order they are in.
 
Upvote 0

Forum statistics

Threads
1,214,845
Messages
6,121,902
Members
449,053
Latest member
Guy Boot

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