How to get COLUMNS() function to work in VBA with Evaluate()

TomCon

Active Member
Joined
Mar 31, 2011
Messages
373
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
On my worksheet the following two formulas work and correctly return 5 as the result.
=COLUMN(data_rng[[#Headers],[Net]])
=COLUMN($E$1)

However, trying to use those formulas in Evaluate, they do not work.
Dim c As Variant
c = Evaluate("=COLUMN(data_rng[[#Headers],[Net]])")
c = Evaluate("=column($E$1)")

I have tried Dim c as integer as well.

With c as Variant, the line c=Evaluate... executes, does not give an error, but after the line, c does not have the value 5.
If i type ?c in the Immediate window, after the above line executers, it gives the "Runtime Error 13, Type mismatch".

With c Dim as Integer, the error occurs on the Evaluate line and it does not execute.

How do i get this to work, use the COLUMN() worksheet function within VBA to get the result?

Thanks!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Even though there is only a single value, the column function will return it as an array which vba doesn't like.
I know of 2 methods that will work, there may be others.
Using vba native functions instead of evaluating a formula.
VBA Code:
Range("data_rng[[#Headers],[Net]]").Column
Using INDEX to nullify the array by extracting the first (only) item in the array.
VBA Code:
Evaluate("INDEX(COLUMN(data_rng[[#Headers],[Net]]),1)")
edit:- 3 ways, this one came to mind after posting, this is simply the native vba equivalent to INDEX.
VBA Code:
Evaluate("COLUMN(data_rng[[#Headers],[Net]])")(1)
 
Upvote 0
Solution
Even though there is only a single value, the column function will return it as an array which vba doesn't like.
I know of 2 methods that will work, there may be others.
Using vba native functions instead of evaluating a formula.
VBA Code:
Range("data_rng[[#Headers],[Net]]").Column
Using INDEX to nullify the array by extracting the first (only) item in the array.
VBA Code:
Evaluate("INDEX(COLUMN(data_rng[[#Headers],[Net]]),1)")
edit:- 3 ways, this one came to mind after posting, this is simply the native vba equivalent to INDEX.
VBA Code:
Evaluate("COLUMN(data_rng[[#Headers],[Net]])")(1)

Heh, totally appreciate it. These "obscure things"...hard for me to discover, so appreciate this "intricate insight"!
Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

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