string缩写

我是一个graphics艺术家,新的Excel和VBA,但试图用它来处理excel中的数据山,作为可变数据在Illustrator中使用。

如果我想将带有“Budwieser,Bud Light&Bud Black Crown”等标志的产品名称的单元格转换为格式为“Budweiser_BL_BBC”的缩写,

大量的谷歌search后,我写了一个function,我认为会完成我的任务,但它返回#VALUE!

编辑:是的,要解释的逻辑 – 我的想法是采取string,分裂在“&”,然后拆分结果数组的第一个位置的“,”然后添加“&”后到“第二个数组 – 此数组sProd将产品分隔到数组的不同位置。 然后遍历该数组,并在创build锯齿状数组的空间处拆分每个产品。 然后循环遍历该数组,再次创build一个string,只取每个产品中每个单词的首字母,用下划线分隔产品。 第一个产品的第一个词是拼写出来,并设置在适当的情况下。 (刚刚在我的逻辑中看到一个错误,并添加了第一个字的例外代码)

编辑#2:该函数应该返回一个string,其中原始string的第一个字在适当的情况下设置,其他所有的字缩写为第一个字母和产品用下划线分隔。 因此,“百威啤酒,百威啤酒和百威啤酒”返回“Budweiser_BL_BLL”,“所有可口可乐啤酒产品”将返回“AllC_DPP”,“佳得乐”返回“佳得乐”。 对不起,如果有任何不清楚,这是我与Excel和VBA的第一回合。

Function Abbrev(p As String) As String Dim sAmpersand() As Variant Dim sProd() As Variant sAmpersand = Split(p, " & ") sProd = Split(sAmpersand(0), ", ") sProd(UBound(sProd)) = sAmpersand(1) Dim ProductCount As Integer Dim ProductEnd As Integer ProductEnd = UBound(sProd) - 1 For ProductCount = 0 To ProductEnd sProd(ProductCount) = Split(sProd(ProductCount), " ") ProductCount = ProductCount + 1 Next ProductCount Dim WordCount As Integer Dim WordEnd As Integer WordEnd = UBound(sProd(ProductCount)) - 1 Abbrev = StrConv(sProd(0)(0), vbProperCase) For ProductCount = 0 To ProductEnd For WordCount = 0 To WordEnd If ProductCount = 0 Then WordCount = 1 End If Abbrev = Abbrev & Left(sProd(ProductCount)(WordCount), 1) WordCount = WordCount + 1 Next WordCount If ProductCount + 1 < ProductEnd Then Abbrev = Abbrev & "_" End If ProductCount = ProductCount + 1 Next ProductCount End Function 

非常感谢您的帮助!

工作代码:

 Function Abbrev(p As String) As String Dim res As String, w1, w2 res = Split(Split(p, ",")(0), " ")(0) If res = Split(p, ",")(0) Then res = res & "_" For Each w1 In Split(Mid(Replace(p, " &", ","), Len(res) + 1), ",") For Each w2 In Split(w1, " ") res = res & Left(w2, 1) Next w2 res = res & "_" Next w1 Abbrev = IIf(Right(res, 1) <> "_", res, Left(res, Len(res) - 1)) End Function 

在这里输入图像说明

这是一个更好的缩写function:

 Function Abbreviate(Name As String) As String Dim I As Integer Dim sResult As String Dim sTemp As String I = InStr(Name, " ") If I < 1 Then Abbreviate = Name Exit Function End If sResult = Left$(Name, I) sTemp = Name Do While I > 0 sTemp = Right$(sTemp, Len(sTemp) - I) If Left$(sTemp, 1) = "(" Then If Mid$(sTemp & "***", 3, 1) = ")" Then sResult = sResult & " " & Left$(sTemp, 3) Else sResult = sResult & " " & Left$(sTemp, 1) End If Else sResult = sResult & " " & Left(sTemp, 1) End If I = InStr(sTemp, " ") Loop Abbreviate = sResult End Function 

这是来自mrexcel.com上的用户al_b_cnu

这是一个修改后的版本,以缩短结果:

 Function Abbreviate(Name As String) As String Dim I As Integer Dim sResult As String Dim sTemp As String I = InStr(Name, " ") If I < 1 Then Abbreviate = Name Exit Function End If sResult = Left$(Name, I) sTemp = Name Do While I > 0 sTemp = Right$(sTemp, Len(sTemp) - I) If Left$(sTemp, 1) = "(" Then If Mid$(sTemp & "***", 3, 1) = ")" Then sResult = sResult & Left$(sTemp, 3) Else sResult = sResult & Left$(sTemp, 1) End If Else sResult = sResult & Left(sTemp, 1) End If I = InStr(sTemp, " ") Loop Abbreviate = sResult End Function