Copying and pasting specific cells between 2 workbooks

HansDK

New Member
Joined
Oct 19, 2021
Messages
7
Office Version
  1. 2019
Platform
  1. Windows
Hi.
What I am currently trying to achieve is to copy specific cell values between dynamically named workbooks. I have found the following code which works to some extent, but I get formulas transferred in to the receiving workbook in stead of values.

VBA Code:
Sub import1()
Dim sFilename As Variant
Dim wsDest As Worksheet

sFilename = Application.GetOpenFilename("Excel Files, *.xls*")
If sFilename = False Then
    MsgBox "Nothing Imported"
    Exit Sub
End If

Set wsDest = ActiveWorkbook.Worksheets("Price sheet")

Application.ScreenUpdating = False
On Error Resume Next
With Workbooks.Open(sFilename)
.Sheets("Calculation").Range("R36").Copy wsDest.Range("Y16")
.Sheets("Calculation").Range("R33").Copy wsDest.Range("Z16")
.Sheets("Calculation").Range("R32").Copy wsDest.Range("AA16")
.Sheets("Calculation").Range("L203").Copy wsDest.Range("AB16")
.Sheets("Calculation").Range("L157").Copy wsDest.Range("AO16")
.Sheets("Calculation").Range("L158").Copy wsDest.Range("AP16")
.Close False
End With
wsDest.Range("B16").Value = DateTime.Now
On Error GoTo 0
Application.ScreenUpdating = True

Set wsDest = Nothing
End Sub

I have tried using
VBA Code:
 xlpastevalues
without luck. I have tried using
VBA Code:
 .value
as well without luck. If anyone could help, I would be very happy.

BR
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
This works fine, where cell C1 in Sheet1 contains the formula '=A1+B1'

Sheets("Sheet1").Select
Range("C1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

What are you doing which is different?
 
Upvote 0
Since you only want values, give this a try, it should also be faster
VBA Code:
With Workbooks.Open(sFilename)
    wsDest.Range("Y16").Value = .Sheets("Calculation").Range("R36").Value
    wsDest.Range("Z16").Value = .Sheets("Calculation").Range("R33").Value
    wsDest.Range("AA16").Value = .Sheets("Calculation").Range("R32").Value
    wsDest.Range("AB16").Value = .Sheets("Calculation").Range("L203").Value
    wsDest.Range("AO16").Value = .Sheets("Calculation").Range("L157").Value
    wsDest.Range("AP16").Value = .Sheets("Calculation").Range("L158").Value
    .Close False
End With
 
Upvote 0
Since you only want values, give this a try, it should also be faster
VBA Code:
With Workbooks.Open(sFilename)
    wsDest.Range("Y16").Value = .Sheets("Calculation").Range("R36").Value
    wsDest.Range("Z16").Value = .Sheets("Calculation").Range("R33").Value
    wsDest.Range("AA16").Value = .Sheets("Calculation").Range("R32").Value
    wsDest.Range("AB16").Value = .Sheets("Calculation").Range("L203").Value
    wsDest.Range("AO16").Value = .Sheets("Calculation").Range("L157").Value
    wsDest.Range("AP16").Value = .Sheets("Calculation").Range("L158").Value
    .Close False
End With
Hi Alex. Perhaps it wasn't very clear from my initial posting but I did try this approach. It also did not work. To me, it should work. However, could it be something with the designation of workbooks? Could excel get confused about which is the Active workbook I am referring to ?
 
Upvote 0
Okay, so I figured it out. I am not sure why this is necessary, but here is the working code:

VBA Code:
Sub import1()
Dim sFilename As Variant
Dim wsDest As Worksheet

sFilename = Application.GetOpenFilename("Excel Files, *.xls*")
If sFilename = False Then
    MsgBox "Nothing Imported"
    Exit Sub
End If

Set wsDest = ActiveWorkbook.Worksheets("Price sheet")

Application.ScreenUpdating = False
On Error Resume Next

wsDest.Range("Y16") = Workbooks.Open(sFilename).Sheets("Calculation").Range("R36")
wsDest.Range("Z16") = Workbooks.Open(sFilename).Sheets("Calculation").Range("R33")
wsDest.Range("AA16") = Workbooks.Open(sFilename).Sheets("Calculation").Range("R32")
wsDest.Range("AB16") = Workbooks.Open(sFilename).Sheets("Calculation").Range("L203")
wsDest.Range("AO16") = Workbooks.Open(sFilename).Sheets("Calculation").Range("L157")
wsDest.Range("AP16") = Workbooks.Open(sFilename).Sheets("Calculation").Range("L158")

Workbooks.Open(sFilename).Close False

wsDest.Range("B16").Value = DateTime.Now
On Error GoTo 0
Application.ScreenUpdating = True

Set wsDest = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,166
Members
452,615
Latest member
bogeys2birdies

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