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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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