Problem Importing Data from Directory With VBA


New Member
Mar 20, 2019
I am trying import data from 3 worksheets in a specified work book.
My code works to delete the existing data in the destination wb.

When I the code gets to the import commands it does not import.
I have checked the file names and they match.

When the resource actuals wb opens it is read only. could that be the problem?

my code:
Option Explicit
Sub ImportData()
Dim directory As String
Dim curDirectory As String
Dim fileName As Variant
Dim lastRow As Long
Dim wbPvA As Workbook, wbResource_Actuals_Current As Workbook

Set wbPvA = ThisWorkbook


directory = "C:\Users\ru88725\Documents\CBT\Reports"
fileName = Dir(directory & "*.xl??")


'change directory
'curDirectory = CurDir
ChDrive directory
ChDir directory

'Open filepat,selected Historical Workbook and clear prior data from Import Worksheets
fileName = Application.GetOpenFilename(MultiSelect:=False)
'if user cancels
If fileName = False Then GoTo exitsub

'Turn off screen updating and display alerts
With Application
.ScreenUpdating = False: .DisplayAlerts = False
End With

With wbPvA
End With

On Error GoTo exitsub
Set wbResource_Actuals_Current = Workbooks.Open(fileName, False, True)

'Copy and paste Fieldglass Actuals Data
With wbResource_Actuals_Current.Sheets("Fieldglass")
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A3:AD" & lastRow).Copy
End With

wbPvA.Sheets("Fieldglass").Range("E2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Application.CutCopyMode = False
'Copy and paste MSP Actuals Data
With wbResource_Actuals_Current.Sheets("MSP_Actuals")
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A2:V" & lastRow).Copy
End With

wbPvA.Sheets("MSP_Actuals").Range("D2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Application.CutCopyMode = False

'Copy and paste BU Staff On Board Data
With wbResource_Actuals_Current.Sheets("BU_Staff")
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A7:S" & lastRow).Copy
End With

wbPvA.Sheets("BU_Staff").Range("B7").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Application.CutCopyMode = False

'Close Historical Data Workbook
wbResource_Actuals_Current.Close False

'reset directory
ChDrive curDirectory
'ChDir curDirectory
'Turn on screen updating and display alerts
With Application
.ScreenUpdating = True: .DisplayAlerts = True
End With

'Selects home Resource Analysis.
With ThisWorkbook
End With

'report Errors
If Error <> 0 Then MsgBox (Error(Err)), 48, "Error"

MsgBox "Data Import is complete."

End Sub


Well-known Member
Nov 9, 2002
Hi Rollnation1 and Welcome to the Board! The code looks like it should work. Trial changing the file property of the resource wb to be NOT read only and see what happens. Dave
ps. please use code tags


Well-known Member
Dec 3, 2018
Office Version
I tried all your code and it works.

There is only something that does not make sense to me; in this line:

.Sheets("Fieldglass_Actuals").Range("E2: AE3000").ClearContents
You are cleaning the "Fieldglass_Actuals" sheet, but when importing you do it on another sheet:

[COLOR=#333333]wbPvA.Sheets("[/COLOR][COLOR=#0000ff]Fieldglass[/COLOR][COLOR=#333333]").Range("E2").PasteSpecial Paste:=xlPasteValues, _[/COLOR]
[COLOR=#333333]Operation:=xlNone, SkipBlanks:=False, Transpose:=False[/COLOR]
I recommend you remove this line:
[COLOR=#ff0000]On Error GoTo exitsub[/COLOR]
And try again all your code to see if there is an error that you are not detecting because you are aborting in case of error.

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...