Rollnation
New Member
- Joined
- Jan 17, 2017
- Messages
- 17
Hello all,
I am trying to import data from a network file to update a dashboard. My VBA retreives the data a brings it into a new workbook but then when my code attempts to copy and paste in into my forecasting model i receive a runtime error 9 "subscript out of range".
I want to declare the active workboook as wbData in the event the user changes the file name...
The error occurs at 'copy and paste historical units which is commented out.
I can upload the workbook im trying to copy if it helps. Any asssitance is truly appreciated!!!!
Code:
Option Explicit
Sub ImportData()
Dim directory As String
Dim fileName As String
Dim wbData As String
Dim sheet As Worksheet
Dim lastRowUnits As Variant
Dim lastRowEDAP As Variant
directory = "K:\CODE 150\COST\RevForecastTool\ImportData"
fileName = Dir(directory & "*.xl??")
wbData = Dir("*.xl??")
'Turn off screen updating and display alerts
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'change directory
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" Or fileName = "" Then
Exit Sub
Else:
Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_Units").Range("A34:Z77").ClearContents
Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_EDAP").Range("A16:BZ60").ClearContents
Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_Rev").Range("A11:Z55").ClearContents
Workbooks.Open (fileName)
End If
'Copy and paste Historical Units
'lastRowUnits = Workbooks(wbData).Sheets("Detail Page_1").Cells(Rows.Count, 1).End(xlUp).Row
'Workbooks(wbData).Sheets("Sheet1").Range("A2:Z" & lastRowUnits).Copy
'Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_Units").Range("A34").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy and paste Historical EDAP
'lastRowEDAP = Workbooks(wbData).Sheets("Detail Page1_2").Cells(Rows.Count, 1).End(xlUp).Row
'Workbooks(wbData).Sheets("Detail Page1_2").Range("A2:BZ" & lastRowEDAP).Copy
'Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_EDAP").Range("A16").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Close Historical Data Workbook
'Workbooks(wbData).Close
'Turn on screen updating and display alerts
'Application.ScreenUpdating = True
'Application.DisplayAlerts = True
End Sub
I am trying to import data from a network file to update a dashboard. My VBA retreives the data a brings it into a new workbook but then when my code attempts to copy and paste in into my forecasting model i receive a runtime error 9 "subscript out of range".
I want to declare the active workboook as wbData in the event the user changes the file name...
The error occurs at 'copy and paste historical units which is commented out.
I can upload the workbook im trying to copy if it helps. Any asssitance is truly appreciated!!!!
Code:
Option Explicit
Sub ImportData()
Dim directory As String
Dim fileName As String
Dim wbData As String
Dim sheet As Worksheet
Dim lastRowUnits As Variant
Dim lastRowEDAP As Variant
directory = "K:\CODE 150\COST\RevForecastTool\ImportData"
fileName = Dir(directory & "*.xl??")
wbData = Dir("*.xl??")
'Turn off screen updating and display alerts
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'change directory
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" Or fileName = "" Then
Exit Sub
Else:
Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_Units").Range("A34:Z77").ClearContents
Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_EDAP").Range("A16:BZ60").ClearContents
Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_Rev").Range("A11:Z55").ClearContents
Workbooks.Open (fileName)
End If
'Copy and paste Historical Units
'lastRowUnits = Workbooks(wbData).Sheets("Detail Page_1").Cells(Rows.Count, 1).End(xlUp).Row
'Workbooks(wbData).Sheets("Sheet1").Range("A2:Z" & lastRowUnits).Copy
'Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_Units").Range("A34").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy and paste Historical EDAP
'lastRowEDAP = Workbooks(wbData).Sheets("Detail Page1_2").Cells(Rows.Count, 1).End(xlUp).Row
'Workbooks(wbData).Sheets("Detail Page1_2").Range("A2:BZ" & lastRowEDAP).Copy
'Workbooks("RevForecast_Beta1.0.xlsm").Sheets("Imp_EDAP").Range("A16").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Close Historical Data Workbook
'Workbooks(wbData).Close
'Turn on screen updating and display alerts
'Application.ScreenUpdating = True
'Application.DisplayAlerts = True
End Sub