简体   繁体   English

删除不同单词并重新格式化文本的公式

[英]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 it helps the steps with the Find/Replace function that I would use are: 如果它可以帮助我使用“查找/替换”功能,请执行以下步骤:
  • remove all text after and including the first bracket 删除所有包含第一个括号之后的文本
  • Replace all months with a 3 letter version (ie replace 'January' with 'Jan' etc, 12 different commands) 用3个字母的版本替换所有月份(即用12月的“ Jan”替换“ January”等12个不同的命令)
  • Add brackets to each end (which I actually do using a =CONCATENATE("(", AD11,")") formula) 在每端添加括号(我实际上使用= CONCATENATE(“(”,AD11,“)”)公式进行此操作)

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: 这是我要遵循的步骤:

  1. Select the column of date ranges. 选择日期范围列。
  2. Click the Text to Columns button. 单击“文本到列”按钮。
  3. Choose Delimited. 选择定界。
  4. Click Next. 点击下一步。
  5. Next to Other, type ( . 在其他旁边,键入(
  6. Click Finish. 单击完成。
  7. Delete the column containing all the durations like "2 years 7 months)". 删除包含所有持续时间(例如“ 2年7个月)”的列。
  8. Once again, select the date ranges. 再次选择日期范围。
  9. Perform another Text to Columns, this time with - instead of ( . 使用-代替( ,执行另一个“文本到列”。
  10. Do a find/replace to get rid of any spaces (ie, type a space into "Find what", and make sure "Replace with" has nothing typed into it). 查找/替换以除去任何空格(即,在“查找内容”中键入一个空格,并确保“替换为”中没有键入任何内容)。

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)易于安装和使用:

  1. ALT-F11 brings up the VBE window ALT-F11弹出VBE窗口
  2. ALT-I ALT-M opens a fresh module ALT-I ALT-M打开一个新模块
  3. paste the stuff in and close the VBE window 将内容粘贴并关闭VBE窗口

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:

  1. bring up the VBE window as above 如上调出VBE窗口
  2. clear the code out 清除代码
  3. close the VBE window 关闭VBE窗口

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.

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