简体   繁体   中英

VBA for truncating data in an MS Excel cell

I have an Excel spreadsheet with some 4000 instances of data entered in a cell as (for example) =234+3455+456+876. [The data are stock market related.] It turns out I only need the first two data elements. Each datum can be anywhere from 1 to 4 or 5 characters long.

I'm VBA illiterate, but was wondering if someone could point me toward a macro or global command that would truncate the string after the last digit of the first two data elements. So, the above string would end up as =234+3455. Maybe an "if, then" function of some sort? How would I apply same to the entire spreadsheet?

At this point, I could probably have just manually deleted the 4000 cells, but now it has become a challenge!! I also realize I really need to learn VBA!

Any help would be most appreciated.

How about a formula? No need for VBA

=LEFT(A1;FIND("+";A1;FIND("+";A1)+1)-1)

Assuming data you want to truncate is in cell A1

One solution to this problem is to use the "Text to Columns" function and direct the function to consider "+" signs as delimiters. This broke the four numbers in the cell into four numbers, each in its own cell across four columns. I could then delete the last two columns and then sum the first two columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM