Resize non-contiguous named range

jimgrip

New Member
Joined
Aug 28, 2018
Messages
1
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.


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
 

Forum statistics

Threads
1,226,771
Messages
6,192,926
Members
453,767
Latest member
922aloose

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