简体   繁体   English

替换字符串的一部分(Excel VBA)

[英]Replacing part of a string (Excel VBA)

I have the following data in Sheet 1: 我在工作表1中有以下数据:

                 A
1   1C BAC Z6 : CHF QM : 1M TOIS
2   2C BAC Z6 : CHF QM : 1M TOIS
3   3C BAC H7 : CHF QM : 1M TOIS
4   30C BAC H7 : CHF AM : 1M TOIS
5   45C BAC M7 : CHF QM : 1M TOIS
6   5C BAC Z7 : CHF QM : 1M TOIS

I'm trying to replace the 2-letter code that follows "BAC", but can't seem to do so using the REPLACE() function. 我正在尝试替换“ BAC”之后的2个字母的代码,但似乎无法使用REPLACE()函数来替换。 I'm not familiar enough with VBA to use InStr either. 我对VBA不太熟悉,也无法使用InStr。

Background : the 2-character code that follows "BAC" represents a month and a year. 背景 :“ BAC”后面的2个字符的代码表示一个月和一年。

The first character in the 2-character code represents a month. 2个字符的代码中的第一个字符代表一个月。 For example, 例如,

"H" represents March “ H”代表三月

"M" represents June “ M”代表六月

"U" represents September “ U”代表9月

"Z" represents December “ Z”代表十二月

There are only four months (March, June, September, December). 只有四个月(3月,6月,9月,12月)。

The second character in the 2-character code represents the year. 2个字符的代码中的第二个字符表示年份。 For example, 例如,

"6" represents 2016 “ 6”代表2016

"7" represents 2017 “ 7”代表2017

"8" represents 2018 etc, etc “ 8”代表2018等,等等

Does anyone know of a solution that can replace the 2-character code with next 2-character code (ie the next month and year)? 有谁知道可以用下一个 2个字符代码(即下个月和下一年)替换2个字符代码的解决方案吗?

For example, 例如,

"Z6" (December 2016) would become "H7" (March 2017) “ Z6”(2016年12月)将变为“ H7”(2017年3月)

"H7" (March 2017) would become "M7" (June 2017) “ H7”(2017年3月)将变为“ M7”(2017年6月)

"M7" (June 2017) would become "U7" (September 2017) “ M7”(2017年6月)将变为“ U7”(2017年9月)

"U7" (September 2017) would become "Z7" (December 2017) “ U7”(2017年9月)将变为“ Z7”(2017年12月)

"Z7" (December 2017) would become "H8" (March 2018) “ Z7”(2017年12月)将变为“ H8”(2018年3月)

etc, etc. 等等等

Thanks in advance for your help with this! 在此先感谢您的帮助!

Try: 尝试:

=REPLACE(A1,FIND("BAC",A1)+4,2,VLOOKUP(MID(A1,FIND("BAC",A1)+4,1),{"H","M";"M","U";"U","Z";"Z","H"},2,FALSE)&MID(A1,FIND("BAC",A1)+5,1)+(MID(A1,FIND("BAC",A1)+4,1)="Z"))

We use the REPLACE function along with a VLOOKUP to determine what to replace the letter part of the code. 我们将REPLACE函数与VLOOKUP一起使用,以确定用什么替换代码的字母部分。

FIND locates the two character code to be replaced by REPLACE or examined with the MID function. FIND定位于被替换为两个字符代码REPLACE与或检查MID功能。

VLOOKUP decides to replace H with M, M with U, etc VLOOKUP决定用M替换H,用U等替换M

The Boolean equality test at the end decides to increment the number by one if and only if the letter is Z . 最后且仅当字母为Z ,布尔相等测试才决定将数字递增1。

EDIT: The OP indicated he might want to search for one of several codes preceding the part to be replaced. 编辑: OP表示他可能要搜索要替换的零件之前的几个代码之一。 This can be done by using an array constant in the FIND function find_text parameter. 这可以通过在FIND函数的find_text参数中使用数组常量来完成。 We also add these constants on to the end of the string in order to avoid errors depending on which is not found. 我们还将这些常量添加到字符串的末尾,以避免根据找不到的错误而导致的错误。 MIN returns the smallest number which, if the key is found, will be within the key. MIN返回最小的数字,如果找到了密钥,它将在密钥内。

In this modification, I also used the MOD function so that 9 would roll over to 0 . 在此修改中,我还使用了MOD函数,以使9翻转为0

=REPLACE(A1,MIN(FIND({"BAC","EMC"},A1&"BACEMC"))+4,2,VLOOKUP(MID(A1,MIN(FIND({"BAC","EMC"},A1&"BACEMC"))+4,1),{"H","M";"M","U";"U","Z";"Z","H"},2,FALSE)&MOD(MID(A1,MIN(FIND({"BAC","EMC"},A1&"BACEMC"))+5,1)+(MID(A1,MIN(FIND({"BAC","EMC"},A1&"BACEMC"))+4,1)="Z"),10))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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