简体   繁体   中英

Set cell value using based on cell length

I have this kind of product id in column D:

  • 1942-PS-M-01
  • 1942-PS-M-233
  • 1942-PM-25
  • 1942-PL-200

I want to set the value in column G Using the last 2/3 number in the product id value.

I tried to use formula such =MID([Product_ID],11,3) and =MID([Sample_ID],10,3), but none of this id efficient and I have to enter the formula 1 by 1 since the id is doest not have same length.

Is there any way to do so by using vba? because I have almost 20k rows of data

I have an idea like using if (len([Product_ID])<13 then =MID([Product_ID],11,3) or if (len([Product_ID])<12 then =MID([Product_ID],10,3) but doest not know how to implement this in vba

Use the following formula:

=MID(D1,FIND("-",D1,FIND("-",D1,FIND("-",D1)+1)+1)+1,LEN(D1))

This will work as long as there are consistently three dashes in your product id. It looks for the position of the first dash, then the second, then the third and extracts the rest of the string after the third dash.

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