我在列表中有一些产品,我需要将字数减少到6个字符,每个字符限制也不能再长20(所以它必须再次将句子修剪为20个字符)
例如
南方舒适= Southe Comfor(这是我需要的结果)
Courvoisier = Courvo
如果不是VB
,我宁愿使用excel公式来做到这一点非常感谢
答案 0 :(得分:4)
我不认为用公式真的可以做到。你需要一个For/Each
结构来修剪每个单词,然后重新组合句子并修剪结果。 VBA将是最佳选择。
使用Split
函数和合适的分隔符(很可能是空格字符)将句子转换为单词数组,然后迭代单词以创建新字符串。
从请求UDF功能的评论中修改
从=foo(A1)
等工作表中调用
Function foo(r as Range)
Dim sentence As Variant
Dim w As Integer
Dim ret as String
' assign this cell's value to an array called "sentence"
sentence = Split(r.Value, " ")
' iterate each word in the sentence
For w = LBound(sentence) To UBound(sentence)
' trim to 6 characters:
sentence(w) = Left(sentence(w), 6)
Next
' Join the array back to a string/sentence
ret = Join(sentence, " ")
'Make sure the sentence is max 20 chars:
ret = Left(ret, 20)
'return the value to your function expression:
foo = ret
End Function