Defined Name error in VBA

alpine_21

New Member
Joined
Aug 4, 2011
Messages
2
Hello,

I'm using Excel 2003 on XP Pro.

I'm creating a basic user form which will allow users to view and edit key variables in a large workbook. The workbook uses defined names for these variables.

When I assign the value of a defined name with a certain format to a text box, I receive an error message. This appears to only happen with defined names that include 2 or more underbars (e.g. _2011_Tax). Names with one underbar (e.g., _2011Rev) work fine.

-----

a = Evaluate(ThisWorkbook.Names("_2011Rev").RefersTo)
UserForm1.TextBox1.Value = a

(This works fine)

-----

b = Evaluate(ThisWorkbook.Names("_2011_Tax").RefersTo)
UserForm1.TextBox2.Value = b

(This fails with an error of: )

Run-time error '-2147352571 (80020005)':
Could not set the Value property. Type mismatch.

-----

Has anyone ever seen this? The defined names function fine in the worksheets, just not with VBA. What am I missing?

Any help appreciated,

Thanks,
Brian

PS - Its been awhile since I've used VBA...
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello,

I'm using Excel 2003 on XP Pro.

I'm creating a basic user form which will allow users to view and edit key variables in a large workbook. The workbook uses defined names for these variables.

When I assign the value of a defined name with a certain format to a text box, I receive an error message. This appears to only happen with defined names that include 2 or more underbars (e.g. _2011_Tax). Names with one underbar (e.g., _2011Rev) work fine.

-----

a = Evaluate(ThisWorkbook.Names("_2011Rev").RefersTo)
UserForm1.TextBox1.Value = a

(This works fine)

-----

b = Evaluate(ThisWorkbook.Names("_2011_Tax").RefersTo)
UserForm1.TextBox2.Value = b

(This fails with an error of: )

Run-time error '-2147352571 (80020005)':
Could not set the Value property. Type mismatch.

-----

Has anyone ever seen this? The defined names function fine in the worksheets, just not with VBA. What am I missing?

Any help appreciated,

Thanks,
Brian

PS - Its been awhile since I've used VBA...


its the variable type you are using.

what is it now? try string, in my opinion if all else fails declare as variant
 
Upvote 0
Hi Brian - Welcome to the Board!

I was able to use your code with the named range "_2011_Tax" without getting an error. I tested this on both xl2007 and xl2003.

I tried several ways to generate errors and found that Excel gives a Type Mismatch error when you try to assign an error value such as #DIV/0! or #N/A to TextBox.Text. Perhaps the range you were referencing had an error value at that point in the testing?

BTW..Other tries were:
Reference to a range of more than one Cell: 1004 Error
Merged Cell: Surprisingly worked without error.

Is it possible the problem was something other than the two underbars and that it was coincidence that the error came up at the point in your testing?
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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