r/visualbasic • u/NarrowG • Feb 06 '20
VBA Excel removing undesired text from cells
I need to create a formula that cleans the text of data in a cell. My data set has different things that I need removed.
For example cell has “GANUS_LEM_Prime” The formula I create removes GANUS_ part, leaving LEM_Prime Cell can contain other type of logic like: ASIA_BIBLE_POSTCARD In that case I want the formula to keep the cell as it is, it does not have the “GANUS_” portion so formula should return the exact same data.
1
u/fasti-au Feb 07 '20
Can’t remember what code vba uses but if it’s a one off just do it manually. Make a cell look for the substr(source,0,6). Make a new cell checking that result is gamut_ then sort it all using that result and find the trues. Then write a macro just to trim the left 6 cells where your result was true
Doing the check in vba is easy enough I just can’t remember what vba code looks like
Probably something like
if search(cells:cells,’gamut_*) Then set cell = substr(current cell,6,100) Loop
The 100 is just a number longer than the source cell is likely to be
Sorry I can’t vba it. Not my current language but I did a lot of this kind of thing manually at a reporting job I had because it was never corrected data
1
u/MildewManOne Feb 09 '20
I would recommend posting VBA questions in r/vba.
For your question though, the built in Replace function should do what you want.
Range("A1").Value = Replace(Range("A1").Value, "GANUS_", vbNullString, 1, -1, compareType) Replace compareType with either vbBinaryCompare (case sensitive compare) or vbTextCompare (not case sensitive).
0
2
u/andrewsmd87 Web Specialist Feb 06 '20
Put this in a cell
=SUBSTITUTE(A1, "GANUS", "")
Where A1 is whatever cell contains the text