I am checking two ranges to see if they have the same size. If the new range is smaller than the old range, then my macro expands the range to match the old.
Sub san_import(owb As String, nwb As String, imt As String)
Dim orng As Range
Dim nrng As Range
Dim r_fix As Long
Dim c_fix As Long
Set orng = Workbooks(owb).Names(imt).RefersToRange
Set nrng = Workbooks(nwb).Names(imt).RefersToRange
r_fix = orng.Rows.Count - nrng.Rows.Count
c_fix = orng.Columns.Count - nrng.Columns.Count
If r_fix > 0 Then
With nrng
.Rows(.Rows.Count + r_fix).EntireRow.Offset(-1, 0).Insert
.Resize(.Rows.Count + r_fix, .Columns.Count).Name = .Name.Name
End With
End If
If c_fix > 0 Then
With nrng
.Columns(.Columns.Count + c_fix).EntireColumn.Offset(-1, 0).Insert
.Resize(.Rows.Count, .Columns.Count + c_fix).Name = .Name.Name
End With
End If
Set nrng = Workbooks(nwb).Names(imt).RefersToRange
nrng.ClearContents
nrng.Value2 = orng.Value2
End Sub
However, I am getting an application defined error at this line:
.Resize(.Rows.Count + r_fix, .Columns.Count).Name = .Name.Name
Any ideas as to why this happens? Also I am not sure, that the macro successfully inserts new rows, in my tests this doesn't seem the case.