ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何提取字符串中的特定格式的词组并把结果以文本输出?

如何提取字符串中的特定格式的词组并把结果以文本输出?

作者:绿色风 分类: 时间:2022-08-17 浏览:112
楼主
liuguansky
Q:有这样一个段落:In the traffic court of a large mid-western city, a young lady was brought before the judge to answer a ticket given her for driving through a red light. She explained to his honor that she was a school teacher and requested an immediate disposal of her case in order that she might hasten on to her classes. A wild gleam came into the judge's eye. "You are a school teacher, eh?" said he. "Madam, I shall realize my lifelong ambition. Sit down at that table and write 'I went through a red light' five hundred times."
我想其中所有的这种“In the traffic court”--单词空格单词空格单词空格单词
形式的所有都提取出来分行后放到一个文件里,比如上面的那段话提取出来后的部分结果:
In the traffic court
the traffic court of
traffic court of a
court of a large
of a large mid-western
A:用如下代码可以实现:

  1. Sub justtest()
  2.     Dim r As String, s, i As Integer, j As Integer
  3.     r = [a1].Value
  4.     s = Split(r, " ")
  5.     Open "c:\1.txt" For Output As #1 ' 内容保存在c:\1.txt中
  6.     For i = LBound(s) To UBound(s) - 4
  7.         r = ""
  8.         For j = i To i + 3
  9.             r = r + s(j) + " "
  10.         Next j
  11.         r = Left(r, Len(r) - 1)
  12.         If InStr(r, ",") = 0 Then Print #1, r
  13.     Next i
  14. End Sub



2楼
水吉果果
学习****!
3楼
0Mouse
  1.             r = r + " " +  s(j)
  2.         Next j
  3.         r = Mid(r, 2)
关闭文本文件
  1.     Next i
  2.     Close

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap