Copy Cell Range From One Workbook to Another

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,590
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have code that opens up a second workbook. Since the second workbook is variable named, I use a formula to name the workbook name.
Code:
With wshT.Range("C2")
    sfname = Format(.Value, "00000") & "(" & Format(.Value, "dd-mmmm-yy") & ").xls"
    Set wshcore_staff = Workbooks(sfname).Worksheets("STAFF")
End With
wshT is workbook1, C2 is user inputted date.
wshcore_staff is the staff worksheet of the workbook resulting from sfname. (This workbook has been previously opened in earlier code)

I need to copy the contents only of a range of cells (B4-B10) from wshcore_staff to wshT cell C5.

I think it's an easy solution, but I'm drawing a blank.
I wish to
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try

Code:
With wsht.Range("C2")
    sfname = Format(.Value, "00000") & "(" & Format(.Value, "dd-mmmm-yy") & ").xls"
    Set wshcore_staff = Workbooks(sfname).Worksheets("STAFF")
End With
wshcore_staff.Range("B4:B10").Copy Destination:=wsht.Range("C5")
 
Upvote 0
Hi VoG, your simple suggestion did the trick, but didn't provide the full expected results.
The range of cells copied are formula based and I simply need the values copied. Currently, the results on the destination sheet are #REF.
 
Upvote 0
Try

Code:
With wsht.Range("C2")
    sfname = Format(.Value, "00000") & "(" & Format(.Value, "dd-mmmm-yy") & ").xls"
    Set wshcore_staff = Workbooks(sfname).Worksheets("STAFF")
End With
wshcore_staff.Range("B4:B10").Copy
wsht.Range("C5").PasteSpecial Paste:=xlPasteValues
 
Upvote 0
Hi VoG,

Thanks for your continued support. I am receiving a "PasteSpecial method of Range class failed." error on this line ...

Rich (BB code):
Sub import_stafflist()
Application.EnableEvents = False

Dim wshT As Worksheet
Dim wshcore_staff As Worksheet
Dim sfname As String
Set wshT = Workbooks("worksheets.xls").Worksheets("Launch")
With wshT.Range("C2")
    sfname = Format(.Value, "00000") & "(" & Format(.Value, "dd-mmmm-yy") & ").xls"
    Set wshcore_staff = Workbooks(sfname).Worksheets("STAFF")
End With
wshcore_staff.Range("H4:H16").Copy
ActiveSheet.Unprotect
wshT.Range("B8").PasteSpecial Paste:=xlPasteValues
Application.EnableEvents = True
ActiveSheet.Protect
End Sub
 
Upvote 0
I have a feeling that unprotecting a sheet may clear the clipboard. Try changing the code order

Rich (BB code):
Sub import_stafflist()
Application.EnableEvents = False

Dim wshT As Worksheet
Dim wshcore_staff As Worksheet
Dim sfname As String
Set wshT = Workbooks("worksheets.xls").Worksheets("Launch")
ActiveSheet.Unprotect
With wshT.Range("C2")
    sfname = Format(.Value, "00000") & "(" & Format(.Value, "dd-mmmm-yy") & ").xls"
    Set wshcore_staff = Workbooks(sfname).Worksheets("STAFF")
End With
wshcore_staff.Range("H4:H16").Copy
wshT.Range("B8").PasteSpecial Paste:=xlPasteValues
Application.EnableEvents = True
ActiveSheet.Protect
End Sub
 
Upvote 0
LOL ... you answered before I had a chance to edit my post. You're quick!!

Yes ... that was the problem .. I put the unprotect line at the top of the code.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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