Problem Importing Data from Directory With VBA

Rollnation1

New Member
Joined
Mar 20, 2019
Messages
1
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

'******************************************SETTINGS***********************************************************

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
.Sheets("MSP_Actuals").Range("D2:Y250000").ClearContents
.Sheets("Fieldglass_Actuals").Range("E2:AE3000").ClearContents
.Sheets("BU_Staff").Range("B7:T250").ClearContents
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

exitsub:
'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
.Activate
wbPvA.Sheets("Resource_Analysis").Select
End With

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

MsgBox "Data Import is complete."

End Sub
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,347
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,824
Office Version
2007
Platform
Windows
I tried all your code and it works.


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


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


Code:
[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:
Code:
[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

Threads
1,082,040
Messages
5,362,804
Members
400,693
Latest member
jenlj

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...
Top