Excel VBA: Copy Results w/ Evaluate Function

I am using the below VBA to copy parsed values of one range to another location. Nonetheless, instead it only pastes the value from the first cell. (i.e., B5) Will someone show me how to modify the below formula to work as intended? (The modified formula should still use Evaluate in a single line.)

Activesheet.Range(F5:F200).value = Evaluate("=Mid(B5:B200,4,3)")

Example data in column B
Comp224
Comp236
Comp123
Comp887
Comp113
Comp334

Result should be the following values in column F
224
236
123
887
113
334

Nonetheless the following values show in column F instead
224
224
224
224
224
224
ouestqueAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

abbas abdullaCommented:
Hi,
This formula is should be evaluated as an array, Please try below code:

    ActiveSheet.Range("F5:F200").FormulaArray = "=Mid(B5:B200,5,3)"
    ActiveSheet.Range("F5:F200").Value = ActiveSheet.Range("F5:F200").Value

Open in new window

0
Rgonzo1971Commented:
HI,

If you want to use evaluate then try
aRes = Evaluate("=Mid(B5:B200,4,3)")
Activesheet.Range(F5:F200).value = aRes

Open in new window

Regards
0
Fabrice LambertFabrice LambertCommented:
Hi,

Any reason why you use such a dangerous object as ActiveSheet ?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ouestqueAuthor Commented:
Hey Rgonzo,

I tried your new code, but it yielded the same problem. Any other ideas?  Also, when I replace Evaluate with brackets it works!!! I am unsure why though. (see below)

aRes = ["=Mid(B5:B200,4,3)"]
Activesheet.Range(F5:F200).value = aRes

Also, is there a way to put variables in the brackets? My below code doesn't work.
EX: aRes =["=Mid(B5:B" & LastRow & ",4,3)"]
0
ouestqueAuthor Commented:
Correction to previous comment. The new code with the brackets pastes the formulas with updated values. I would need a second line to copy/paste them.
0
ouestqueAuthor Commented:
Hey fabrice,

I was using "active" in my post to keep it short. I agree that it is good to avoid it

Hey Abbas,

Interesting idea. I never thought of using arrayformula that way. Caution though. If he user set the column format to = "text" that code won't work.
0
abbas abdullaCommented:
Interesting comment, what error will prevent function from working if column format set as text?
0
ouestqueAuthor Commented:
Hey Abbas,

No error appears. it simply pastes the array formula as text. (e.g., no calculated result just the formula is repeated in every cell)
0
ouestqueAuthor Commented:
I haven't heard back from rgonzo. Is there also a way to do this with Evaluate? Is better to using formulaarray instead of evaluate?
0
abbas abdullaCommented:
Hi,
If you want to overcome the text error try this code, it will change the format to General before putting the formula and then after putting the formula it will switch the format to text again.

Sub MidArrayFormula()
'Format as General
ActiveSheet.Range("F5:F200").NumberFormat = "General"
    ActiveSheet.Range("F5:F200").FormulaArray = "=IFERROR(VALUE(MID(B5:B200,5,3)),"""")"
    ActiveSheet.Range("F5:F200").Value = ActiveSheet.Range("F5:F200").Value
'Back to Text format
ActiveSheet.Range("F5:F200").NumberFormat = "@"
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.