Curious use of VBA Mid() function

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Not a question - just sharing an interesting use of the VBA Mid() function.

Did you know you can use Mid to assign a replacement string of the same size into a variable:

Rich (BB code):
Dim s As String
s = "True fact: Fred is a hero!"
Mid(s,Instr(1,s,"Fred",vbBinaryCompare)) = "Rich"
MsgBox s
'... Produces:
'... True fact: Rich is a hero!

The code in red is an assignment not a comparison! Looks utterly crazy to me. Not sure this is much (if any) use in the real world, but is is facinating!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
could you use it instead of application.substitute ?

i found that substitute fails if you attempt to sub the same value in as out (ie. a find n replace of the same word)

maybe this wouldnt fail etc
 
Upvote 0
The restriction is that the replacement overwrites the original string from the position given by Mid (ie in my example above from the character in s where "Fred" is found). You can't use this method to change the length of the original string.

It just looks weird to me!
 
Upvote 0
That's interesting Richard, thanks.

I had never thought of doing it that way, but I will :biggrin:
 
Upvote 0
Coincidentally I was reminded of that the other day as I was flicking through the VBA Developers Handbook. I did know it, but I don't think I have actually used it more than about twice. :) It's unfortunate that you can't do something like:
Code:
Mid(selection.formula, 2, 4) = "test"
for example.
 
Upvote 0
Hi

A remark, this is not the Mid function, it's the Mid statement, it's one of the string statements.

I must say that I've also only used it a few times. I think of these 3 together: LSet, Mid, RSet. These 3 string statements don't change the length of the string. They are effective and I like to know they're there, but I really only use them once a year.
 
Upvote 0
Ah so it wasn't a case of a function behaving strangely then! That answers why it looks so wonky. I didn't realise there was a Mid statement until you said that (then confirmed it in VBA Help ;)).

I have used LSet before to parse fixed length records. I could see some use, perhaps, with using the Mid statement to amend a code for example. But in the end I expect I would use normal functions etc to benefit from easier readability.
 
Upvote 0
I've used LSet a few times to copy a UDT, but I don't think I've ever used RSet.
 
Upvote 0
Interesting, but I don't think I'd use it too often.

I prefer Replace.:)
Code:
Dim s As String
s = "True fact: Fred is a hero!"
s = Replace(s, "Fred", "Rich")
MsgBox s
 
Upvote 0
Hi Norie

I prefer Replace.

This is not an alternative. You use the Mid() statement to replace characters at a specific position.

Ex: in "I prefer Replace.", change the "e" to "o" in position 5

Code:
s = "I prefer Replace."
Mid(s, 5, 1) = "o" ' using Mid() statement
 
s = "I prefer Replace."
s = Left(s, 4) & "o" & Mid(s, 6) ' alternative with string functions, messier
 
s = "I prefer Replace."
s = Replace(s, "e", "o") ' wrong, not an alternative
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,105
Members
449,096
Latest member
provoking

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