Hi all,
I am trying to fill in a non-contiguous named range in an active workbook (destination) with projections from another workbook (source), such source workbook defined by the user. The named range has contiguous columns, but non-contiguous rows. The rows of the 2 files will always be identical. But the number of columns may be different e.g. the destination named range may have a forecast period of say 10 years, but the source file might have a 5-year forecast period.
I am using the code below, but in case the source file has a smaller forecast period (e.g. 5 years), it fills in years 6 - 10 in destination named range with zeros, as expected. My approach is to resize the destination named range to match the number of columns in the source file, but I am getting a Run-time error '1004' since resize probably fails with non-contiguous range.
Any suggestions for improvements or another approach are very welcomed. Many thanks for your time and assistance.
I am trying to fill in a non-contiguous named range in an active workbook (destination) with projections from another workbook (source), such source workbook defined by the user. The named range has contiguous columns, but non-contiguous rows. The rows of the 2 files will always be identical. But the number of columns may be different e.g. the destination named range may have a forecast period of say 10 years, but the source file might have a 5-year forecast period.
I am using the code below, but in case the source file has a smaller forecast period (e.g. 5 years), it fills in years 6 - 10 in destination named range with zeros, as expected. My approach is to resize the destination named range to match the number of columns in the source file, but I am getting a Run-time error '1004' since resize probably fails with non-contiguous range.
Any suggestions for improvements or another approach are very welcomed. Many thanks for your time and assistance.
Code:
Sub importBusPlan()
Dim fNameAndPath As Variant
Dim wbFrom As Workbook, wbTo As Workbook
Dim wsFrom As Worksheet
Dim rngFrom As Range, rngTo As Range
Set wbTo = ActiveWorkbook
Set rngTo = wbTo.Names("busPlan").RefersToRange
rngTo.ClearContents
'User to select file to open
fNameAndPath = Application.GetOpenFilename( _
FileFilter:="Excel Files (*.XLSX; *.XLS; *.CSV), *.XLSX; *.xls; *.csv", _
Title:="Select Excel File to import data")
'If no file selected exit sub
If fNameAndPath = False Then Exit Sub
Set wbFrom = Workbooks.Open( _
Filename:=fNameAndPath, _
ReadOnly:=True)
'User to select the upper left cell of the business plan
Set rngFrom = Application.InputBox(Prompt:="Pick first cell of projections", Type:=8)
Set wsFrom = rngFrom.Worksheet
Dim forcPeriod As Integer
'determine the length of explicit forecast period from IBR
forcPeriod = Range(rngFrom, rngFrom.End(xlToRight)).Columns.Count
Set rngTo = rngTo.Resize(ColumnSize:=forcPeriod) 'this is where the code fails
Application.ScreenUpdating = False
'Link excel, by creating an external link
rngTo.FormulaR1C1 = "=[" & wbFrom.Name & "]" & wsFrom.Name & "!RC"
'Close external workbook
wbFrom.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub