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