如何在excel中对一个单词进行子字符串化并添加一个字母并转换为大写

时间:2019-03-07 14:46:45

标签: excel

我正在使用excel工作表,试图使用此函数(查找或提取或left或right)来获取特定值并在单词末尾添加_

例如

  

dlxlep1

我要用大写字母

  

DLXLE_1

类似地

  

hubudp1

我想要它

  

HUBUD_1

我正在尝试使用查找功能,但未按预期工作

这是我尝试过的那个

=left(A1,5)

3 个答案:

答案 0 :(得分:2)

数据在 A1 中,在 B1 中输入:

=UPPER(LEFT(A1,LEN(A1)-1)) &"_" &  RIGHT(A1,1)

enter image description here

这假定字符串的末尾是十进制数字

EDIT#1:

要截断小数位前的最后一个字符,请使用:

=UPPER(LEFT(A1,LEN(A1)-2)) &"_" &  RIGHT(A1,1)

答案 1 :(得分:0)

使用UPPER()将字符串转换为大写。如果最后一个字符始终是一个数字,则使用LEN(长度)用LEFT(A1,LEN(A1)-1)解析左侧,以确定长度减去一个。添加下划线字符&"_"&,然后添加最右边的字符RIGHT(A1,1)。在一起看起来应该像=UPPER(LEFT(A1,LEN(A1)-1)&"_"&RIGHT(A1,1) 编辑:如果“ p”始终在数字之前,则应将其保留为-2  =UPPER(LEFT(A1,LEN(A1)-2)&"_"&RIGHT(A1,1)甚至=UPPER(REPLACE(A1,LEN(A1)-1,1,"_"))

答案 2 :(得分:0)

REPLACE专长。 SEARCHREV

在VBE中

SEARCHREV是一个用户定义的函数(UDF),它与SEARCH(不区分大小写,即AA=Aa=aa)的作用相同,只是从字符串末尾开始。< / p>

将代码复制到标准模块中(在VBE中,转到Insert>>Module)。

'*******************************************************************************
' Purpose:    Returns the position of the last occurence of
'             a case-INsensitive string within a string. Read only. Long.
' Inputs:
'   UseString       Required. String expression being searched.
'   SearchString    Required. String expression being searched for.
'   RightPosition   Optional. Numeric expression that sets the starting position
'                   for each search. If omitted, –1 is used i.e. the search
'                   begins at the last character position.
'                   If start contains Null, an error occurs.
'*******************************************************************************
Function SEARCHREV(UseString As String, SearchString As String, _
    Optional RightPosition As Long = -1) As Long
  Application.Volatile
  SearchREV = InStrRev(UseString, SearchString, RightPosition, vbTextCompare)
End Function
'*******************************************************************************

在Excel中

您的问题可以这样改写:

用下划线(_)替换字母P的最后一次出现 并将大写(大写)字母应用于字符串。

最后一个字母 P 的位置可以通过SEARCHREV函数=SEARCHREV(A1,"P")轻松找到。此结果正是REPLACE函数作为其第二个参数=REPLACE(A1,SEARCHREV(A1,"P"),1,"_")所需要的。现在我们应用大写字母:=UPPER(REPLACE(A1,SEARCHREV(A1,"P"),1,"_"))。未找到 P 时,SEARCHREV将返回0,这将导致REPLACE产生错误。因此,在运行其余IF

之前,我们必须在=IF(SEARCHREV(A1,"P")=0;"";...语句中处理它

如果数据以A1开头,则将以下公式粘贴到B1中。

=IF(SEARCHREV(A1,"P")=0,"",UPPER(REPLACE(A1,SEARCHREV(A1,"P"),1,"_")))

enter image description here