Link to home
Start Free TrialLog in
Avatar of AL_XResearch
AL_XResearchFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Excel VBA: Named range can't be assigned to range object

Objective

To assign the rows from a named range to a range object so it can be processed.
The named range is created by another earlier function.

Issue

I have the following code (an abbreviated extract showing only the relevant code):

    Dim rngOrigDataRows As Range
Dim blnInputSheetRowsFiltered  as Boolean
    
    ' Init. var.
    blnInputSheetRowsFiltered = blnWorkbookNameExists(wkbInputModule, INPTMOD_NMDRNG_ROWFILTER_RSLTROWS)
    
    With wkbInputModule
    
        ' Get the origonal rowset to work on
        If blnInputSheetRowsFiltered Then
            Set rngOrigDataRows = .Names(INPTMOD_NMDRNG_ROWFILTER_RSLTROWS).RefersToRange
        Else
            Set rngOrigDataRows = .Names("InputSheetDataRows").RefersToRange
        End If

Open in new window


When the named range 'INPTMOD_NMDRNG_ROWFILTER_RSLTROWS' does exist the code that assigns it to a range fails with the following error:

1004::Application-defined or object-defined error

The contents of the named range is as follows:

wkbInputModule.Names(INPTMOD_NMDRNG_ROWFILTER_RSLTROWS).RefersTo = "=InputSheet!$35:$37,InputSheet!$41:$48,InputSheet!$52:$71,InputSheet!$73:$84,InputSheet!$87:$94,InputSheet!$97:$126,InputSheet!$129:$149,InputSheet!$153:$160,InputSheet!$164:$183,InputSheet!$185:$196,InputSheet!$199:$228,InputSheet!$231:$250"

Open in new window


There are 12 areas to the range.

 If I have only 11 areas then I get no error.

Attempts to resolve

I have even tried changing the 12th and final area to point to different rows but it doesn't help.
I have also tried ensuring that the 'InputSheet' worksheet is not protected (just in case) but this had no effect.
I created a new workbook in a new session of Excel and renamed on of the worksheets 'InputSheet' and used the above 'RefersTo' string to create a new range and the error still occurred.

Question

I have never come across this before. To my knowledge ranges can consist of more than 600 areas (no idea why you would want that) but that is far far more than I am using.

My VBA project uses literally hundreds of named ranges over about half-a-dozen workbooks and I have never hit this issue with large row multi-area selections.

What can this be and how can I work around it ?
Avatar of AL_XResearch
AL_XResearch
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I have tried the following test and found there was no error
1. Shortened the area list to exclude the 12th area
2. Assigned the 12th area address to a seperate named range
3. Assigned both of these named ranges to range objects
4. Used the 'Union' function to combine to the two ranges into a third
5. Printed out the 'RefersTo' string of the combined range - this was the 12 area range that I had originally

I then tried to set a new range to the combined range that I had created in step 4 above - no issue

I then created a new named range (using the '.names.add' syntax) and set it's 'RefersTo' argument equal to the combined range I created in step 4 above - no issue

I then created another new range and set it from the 'RefersToRange' property of the newly created named range - it failed with the same error.

It therefore seems that the issue is clearly with the 'RefersToRange' property for some reason.
I have found a workaround - although not a satisfactory one.

The below code will cause the error (where 'Result' is the combination of my previously created ranges to return the original rowset)

Set rngCheck = wkbInputModule.Names("Result").RefersToRange

Open in new window


However as long as you know what worksheet all the cells are on the following workaround works.

Set rngCheck = ThisWorkbook.Worksheets("InputSheet").Range(ThisWorkbook.Names("Result").RefersTo)

Open in new window


As I said this is very limiting because you must know what cells the sheet is on.
Annoyingly I put my 'workaround' into the live code and that too fails with the same non-descriptive-non-helpful error !
Avatar of Rory Archibald
Not sure why you've duplicated this question, or what you mean by "as long as you know what worksheet all the cells are on" but if you created a range that refers to cells on different sheets for some reason, you cannot assign it to a Range object in VBA.
Rory: Thanks for the answers

That is what I meant in my post - that the worksheet.range object wouldn't work unless all the cells in the 'RefersTo' address string were on the same worksheet.

In any case my workaround, whilst working in a test book, doesn't work in my actual code. I can no more guess why the workaround doesn't work than the original code. Both should be valid.

I am not sure I know what you mean by 'Duplicated this question'.

This is critical to my code to resolve or find a workaround.
It's not just Worksheet.Range. Even if you created separate Range objects from each sheet, you couldn't Union them into one Range object. In VBA a Range object can only refer to cells on one sheet. Did you try what I posted in your duplicate? (I hadn't seen this then)
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rory: yes we are both in agreement about the restrictions on a range object - just maybe phrasing it differently.

What duplicate ? This is the first time I have posted this issue, it only happened this morning.
If the length of the 'RefersTo' property is the issue then:

1.

Why can I print it to the debug window even though it is 'too long' to use

2.

Why in the test environment with the same 'refersto' string does it allow me to create a worksheet range whereas in the live system it does not.
I have had much larger multi-area ranges before and never run into this sort of issue.
This duplicate: https://www.experts-exchange.com/questions/28699192/Excel-VBA-Named-range-can't-be-assigned-to-range-object.html

If it's not the length of the refersto, why would it work when shortening the sheet name? Your original fails for me in a new blank workbook.
Not sure how that duplciate was created - certainly not intentional.

I agree that RefersTo seems a likely candidate. I however didn't know until today that there was a limitation on RefersTo - I have always managed to create large named ranges without coming across this issue.

The 'orgional' code failed for me too in a blank workbook but my 'workaround' worked in a new blank book but failed in the live system - which I can't understand.
Do you know what the actual length limit is on 'RefersTo' ?
After some trial and error I have found the maximum length of the 'RefersTo' string (the length it can be before 'RefersToRange' will stop working) is 246.
My suspicion would be the usual 255 characters. That is certainly the most you can use as an argument to the Range property.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FWIW, your original was 241 characters and failed. I've succeeded with names with 249 characters. So I think there is something more complicated afoot, which may relate to the internal storage of the name.
More than likely. I thought 246 was a rather random number. Until I have more definite facts though I will set this as the maximum limit for using 'RefersToRange'
I have it working with 255 characters in the RefersTo. The Excel UI will not allow typing more than this in, which would tend to support it as a limit.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK there is definitely some additional mysterious factor going on here. I got the same '' error today trying to use the 'RefersToRange. method on a defined name which has a 'RefersTo' string of :

"=InputSheet!$A:$U"

I changed the code to use my new function and there was no issue !
Rory helped me on my journey to discover the cause of this and so deserves the points but my function provides a workaround to this issue although neither has actually explained or completely solved the issue.