ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3361|回复: 5

[求助]如何用VBA合并满足特定条件的两个相邻的段落?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2006-9-25 09:44 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

昨天我发了一个帖子“[求助]如何删除包含组合条件段落的段落标记?”,我的要求是删除特定段落的段落换行符号(这些段落必须是字符数少于150个且段落里包含“Bibliographic Information-”),使这个特定段和下一段落能合并成一个段落。

比方说:

Bibliographic Information- 
Model studies on the crosslinking of epoxy resins with amines at room temperature.
Gross, A.; Brockmann, H.; Kollek, H. Dep. Chem., Univ. Bielefeld, Bielefeld, Fed.
 

(上面是3个段落)

斑竹给了我一个答案:

Sub Rep()

    Dim myString As String, myArray() As String, aArray As Variant
    Dim myText As String
    With ActiveDocument
        myString = .Content.Text
        myArray = VBA.Split(myString, Chr(13))
        For Each aArray In myArray
            If Len(aArray) <= 200 And InStr(aArray, "Bibliographic Information-") > 0 Then
            myText = myText & aArray
            Next
        .Content.Text = myText
            Else
            myText = myText & aArray & Chr(13)
            End If
        Next
        .Content.Text = myText
    End With

我调试了一下,发了一个问题,就是程序运行完成后,大部分文档都合乎要求,只有少部分有点问题。问题是:程序运行完毕后,还有少数行没有被删除段落标记,

例如上面的例子,运行完毕后如下:

Bibliographic Information-Model studies on the crosslinking of epoxy resins with amines at room temperature.
Gross, A.; Brockmann, H.; Kollek, H. Dep. Chem., Univ. Bielefeld, Bielefeld, Fed.

(上面是2个段落)

Bibliographic Information-Model studies on the crosslinking of epoxy resins with amines at room temperature.    这段仍然是满足字符数少于150个且段落里包含“Bibliographic Information-”,当然我知道这是程序运行后才产生的。

因此呢,我呢,就参照斑竹的代码,在上段程序后面加入了同样的代码,合起来是:

Sub Rep()

    Dim myString As String, myArray() As String, aArray As Variant
    Dim myText As String
    With ActiveDocument
        myString = .Content.Text
        myArray = VBA.Split(myString, Chr(13))
        For Each aArray In myArray
            If Len(aArray) <= 200 And InStr(aArray, "Bibliographic Information-") > 0 Then
            myText = myText & aArray
            Next
        .Content.Text = myText
            Else
            myText = myText & aArray & Chr(13)
            End If
        Next
        .Content.Text = myText
    End With

    With ActiveDocument
        myString = .Content.Text
        myArray = VBA.Split(myString, Chr(13))
        For Each aArray In myArray
            If Len(aArray) <= 200 And InStr(aArray, "Bibliographic Information-") > 0 Then
            myText = myText & aArray
            Next
        .Content.Text = myText
            Else
            myText = myText & aArray & Chr(13)
            End If
        Next
        .Content.Text = myText
    End With
end sub

发现所有记录里已经都没有满足字符数少于150个且段落里包含“Bibliographic Information-”,满足了我的要求,可是发现,文档的内容整体重复了两边:

Bibliographic Information-Model studies on the crosslinking of epoxy resins with amines at room temperature.Gross, A.; Brockmann, H.; Kollek, H. Dep. Chem., Univ. Bielefeld, Bielefeld, Fed.
Bibliographic Information-Model studies on the crosslinking of epoxy resins with amines at room temperature.Gross, A.; Brockmann, H.; Kollek, H. Dep. Chem., Univ. Bielefeld, Bielefeld, Fed.

(上面其实是两段,只是网页行的字数有限,呈现出来像是4段)

恳请高手赐教!!!

G42eDIXA.txt (2.34 KB, 下载次数: 10)
[此贴子已经被作者于2006-9-25 9:45:43编辑过]

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-9-25 11:40 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2006-9-25 15:44 | 显示全部楼层
不对吧?一个For对二个next?

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-9-25 16:43 | 显示全部楼层

代码贴错了,那部分如下:

Sub Rep()

    Dim myString As String, myArray() As String, aArray As Variant
    Dim myText As String
    With ActiveDocument
        myString = .Content.Text
        myArray = VBA.Split(myString, Chr(13))
        For Each aArray In myArray
            If Len(aArray) <= 200 And InStr(aArray, "Bibliographic Information-") > 0 Then
            myText = myText & aArray
        .Content.Text = myText
            Else
            myText = myText & aArray & Chr(13)
            End If
        Next
        .Content.Text = myText
    End With

    With ActiveDocument
        myString = .Content.Text
        myArray = VBA.Split(myString, Chr(13))
        For Each aArray In myArray
            If Len(aArray) <= 200 And InStr(aArray, "Bibliographic Information-") > 0 Then
            myText = myText & aArray
        .Content.Text = myText
            Else
            myText = myText & aArray & Chr(13)
            End If
        Next
        .Content.Text = myText
    End With
end sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-9-25 16:44 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-9-25 17:59 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 00:48 , Processed in 0.043065 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表