Copy data from another file

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm sure this has been asked before but I can't seem to find the answer in the search function on this site or Google; I'm probably not asking correctly.

I have the following code:
Rich (BB code):
Sub Step2_ImportEFHoldings()

Dim archiveworkbook As Workbook
Dim SecurityNames As Range, SecurityHoldings As Range

Set currentworkbook = ActiveWorkbook

Application.DisplayAlerts = False

msg = "Please enter a valid file date"
With Sheets("MS File")
    .Range("FileDate").Select
    If Len(.Range("FileDate")) < 10 Then
        MsgBox msg, vbExclamation, "Invalid date"
        Exit Sub
    End If
    
End With

On Error GoTo WorkbookError
Workbooks.Open Filename:=Range("FileName"), UpdateLinks:=True, ReadOnly:=True
Set archiveworkbook = ActiveWorkbook
On Error GoTo 0

Range("CLOSING_NAV").Copy
currentworkbook.Activate
Range("FileNAV").PasteSpecial Paste:=xlValues
archiveworkbook.Close savechanges:=False

Exit Sub
WorkbookError:
msg = "File not found, please check"
MsgBox msg, vbExclamation, "File not found"

End Sub
Range("CLOSING_NAV") is local to the workbook that is opened.
I would rather have some code along the lines of:
Rich (BB code):
currentworkbook.Range("FileNAV") = archiveworkbook.Range("CLOSING_NAV") but when I try this (as well as workbook.Sheets("sheet name").Range("FileNAV") = etc..) I keep getting an error message of invalid range or object

Can someone explain how I should be writing this?

Thanks,
Jack
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try something like:
Code:
'change "Sheet1" to suit
currentworkbook.Sheets("Sheet1").Range("FileNAV") = Evaluate(archiveworkbook.Names("CLOSING_NAV").Value)
 
Upvote 0
Hi Warship,

Thanks for the reply, but unfortunately, it's returns a value of #REF!. Any suggestions?

Best,
Jack
 
Upvote 0
Only thing I can think of at the moment is to double check spelling and existance of named ranges.
 
Upvote 0
Thanks Warship, will have a read and hopefully find a solution.
Best,
Jack
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,151
Members
452,891
Latest member
JUSTOUTOFMYREACH

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