[英]Formula to remove different words and reformat text
I'm using excel sort a load of data, and need to clean-up and reformat a number of columns (all currently text-based data) into a consistent new format. 我正在使用excel对大量数据进行排序,并且需要将许多列(当前所有基于文本的数据)清理并重新格式化为一致的新格式。
The challenge is that though the format of the 'old' data is fairly consistent, I can't simply use REPLACE, SUBSTITUTE or TRIM functions because of the huge variations in the word length. 挑战在于,尽管“旧”数据的格式相当一致,但是由于字长的巨大差异,我不能简单地使用REPLACE,SUBSTITUTE或TRIM函数。
The data is all date related. 数据与所有日期相关。
Examples are: 例如:
July 2013 - Present (2 years 7 months)
June 2013 - March 2014 (10 months)
March 2008 - May 2016 (7 years 11 months)
1999 - 2012 (3 years)
July 2013 - Present (2 years 7 months)
June 2013 - March 2014 (10 months)
March 2008 - May 2016 (7 years 11 months)
1999 - 2012 (3 years)
And I would like the output / reformatted version to read: 我希望输出/重新格式化的版本为:
(Jul 2013 - Present)
(Jun 2013 - Mar 2014)
(Mar 2008 - May 2016)
(1999 - 2012)
(Jul 2013 - Present)
(Jun 2013 - Mar 2014)
(Mar 2008 - May 2016)
(1999 - 2012)
The key variables in the original data are: - Length of the 'month' word - 'Month' not always used - Second date is sometimes 'Present' - Duration, shown in brackets, varies considerably 原始数据中的关键变量是:-“月”字的长度-并非总是使用“月”-第二个日期有时是“现在”-括号中显示的持续时间差异很大
And it's that that I'm struggling with (I've being trying to rework Formula to remove entire words that start with certain characters without success) 这就是我正在努力解决的问题(我正在尝试重新编写公式以删除以某些字符开头的整个单词而没有成功)
The perfect output format is: - All in brackets - Months shown as first three letters only (Jan, Feb, etc) - No 'duration' after dates - The formula should be able to cope with instance when the second date is 'Present' and / or when there are no months, just years. 理想的输出格式是:-放在方括号中-月仅显示为前三个字母(1月,2月等)-日期后没有“持续时间”-当第二个日期为“现在”时,公式应能够处理实例和/或没有月份,只有几年。
Can anyone help please? 有人可以帮忙吗? I can do it if I use the Find/Replace function* but I'm after a formula solution so I can share with colleagues.
如果我使用“查找/替换”功能*可以这样做,但是我正在寻找公式解决方案,因此可以与同事共享。
Thanks, S 谢谢,S
About me: mid-30s entry-level 'hacker', always convinced "the must be a way". 关于我:30年代中期的入门级“黑客”,始终坚信“必须采取一种方式”。 Assume no prior knowledge!
假设没有先验知识!
If you don't absolutely need to use functions, the easiest way to do what you're trying to is utilize Excel's Text to Columns features (which is in the Data tab of the Ribbon). 如果您并非绝对需要使用函数,则要做的最简单的方法就是利用Excel的“文本到列”功能(位于功能区的“数据”选项卡中)。
Here are the steps I would follow: 这是我要遵循的步骤:
(
. (
。 -
instead of (
. -
代替(
,执行另一个“文本到列”。 This will result in two columns, one with the start date, and the other with the finish date. 这将导致两列,一列带有开始日期,另一列带有完成日期。
With the data in this format, now you can use a formula to get the result you're looking for. 使用这种格式的数据,现在您可以使用公式来获取所需的结果。 If you place your start dates in column A, and your finish dates in column B, the formula would be something like this:
如果将开始日期放置在A列中,而将完成日期放置在B列中,则公式将如下所示:
="("&IF(A1>10000,TEXT(A1,"MMM YYYY"),A1)&" - "&IF(B1>10000,TEXT(B1,"MMM YYYY"),B1)&")"
What this does is, check if each date value is greater than 10,000 (this distinguishes years from Excel date values). 这样做是检查每个日期值是否大于10,000(这将年份与Excel日期值区分开)。 For years, just take what's already in the cell.
多年以来,只需使用单元中已经存在的东西即可。 For dates, format them as "month year" where month is the 3-letter abbreviation.
对于日期,将其格式设置为“月年”,其中month是3个字母的缩写。 The rest is just simple concatenation with parentheses and a dash.
其余的只是带有括号和破折号的简单连接。
Consider the following User Defined Function (UDF) : 考虑以下用户定义函数(UDF) :
Public Function KleanUp(r As Range) As String
Dim inpt As String, outpt As String
Dim L As Long, i As Long, CH As String
Dim Pulling As Boolean
inpt = Trim(r.Text)
L = Len(inpt)
outpt = ""
Pulling = True
For i = 1 To L
CH = Mid(inpt, i, 1)
If CH = "(" Then
outpt = outpt & CH
Pulling = False
Else
If CH = ")" Then
Pulling = True
Else
If Pulling Then
outpt = outpt & CH
End If
End If
End If
Next i
If Left(outpt, 1) = "(" Then outpt = Mid(outpt, 2)
If Right(outpt, 1) = "(" Then outpt = Mid(outpt, 1, Len(outpt) - 1)
ary = Split(outpt, "(")
For i = LBound(ary) To UBound(ary)
ary(i) = "(" & ary(i) & ")"
Next i
KleanUp = Join(ary, " ")
End Function
Here are some examples: 这里有些例子:
User Defined Functions (UDFs) are very easy to install and use: 用户定义函数(UDF)易于安装和使用:
If you save the workbook, the UDF will be saved with it. 如果您保存工作簿,则UDF将随之保存。 If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx
如果您在2003年以后使用Excel版本,则必须将文件另存为.xlsm而不是.xlsx
To remove the UDF: 删除UDF:
To use the UDF from Excel: 要从Excel使用UDF:
=KleanUp(A1) = KleanUp(A1)
To learn more about macros in general, see: 要总体上了解有关宏的更多信息,请参见:
http://www.mvps.org/dmcritchie/excel/getstarted.htm http://www.mvps.org/dmcritchie/excel/getstarted.htm
and 和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx http://msdn.microsoft.com/zh-CN/library/ee814735(v=office.14).aspx
and for specifics on UDFs, see: 有关UDF的详细信息,请参见:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
Macros must be enabled for this to work! 必须启用宏才能使其正常工作!
EDIT#1: 编辑#1:
Replace the previous version with this version: 用此版本替换以前的版本:
Public Function KleanUp(r As Range) As String
Dim inpt As String, outpt As String
Dim L As Long, i As Long, CH As String
Dim Pulling As Boolean, j As Long
Dim ary, bry, cry
bry = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
cry = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
inpt = Trim(r.Text)
L = Len(inpt)
outpt = ""
Pulling = True
For i = 1 To L
CH = Mid(inpt, i, 1)
If CH = "(" Then
outpt = outpt & CH
Pulling = False
Else
If CH = ")" Then
Pulling = True
Else
If Pulling Then
outpt = outpt & CH
End If
End If
End If
Next i
If Left(outpt, 1) = "(" Then outpt = Mid(outpt, 2)
If Right(outpt, 1) = "(" Then outpt = Mid(outpt, 1, Len(outpt) - 1)
ary = Split(outpt, "(")
For i = LBound(ary) To UBound(ary)
ary(i) = "(" & Trim(ary(i)) & ")"
For j = LBound(bry) To UBound(bry)
ary(i) = Replace(ary(i), bry(j), cry(j))
Next j
Next i
KleanUp = Join(ary, " ")
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.