Excel查找和替换

时间:2011-06-07 12:21:35

标签: excel replace

我的问题是如何进行替换并找到一个永不相同的替换词。 基本上,我想做一个查找并替换“x”这个词,比如用“A”这个词代表5行,然后用“B”这个词进行查找和替换接下来的五行,依此类推一千次。我该如何设法做到这一点?

2 个答案:

答案 0 :(得分:3)

在从互联网上运行代码之前,请始终备份数据。这将在A列中找到“x”的所有实例,并用您在vaReplace中设置的值替换它们。

Sub FindReplace()

    Dim rFound As Range
    Dim vaReplace As Variant
    Dim lFound As Long
    Dim lRepCount As Long

    Const lMAX As Long = 5 'how many to replace before switching values
    Const sFIND As String = "x" 'what to look for
    Const lCOLUMN As Long = 1 'which column to look in

    'list of values to use as replacements
    vaReplace = Array("A", "B", "C", "D", "E")

    Set rFound = Sheet1.Columns(lCOLUMN).Find(sFIND, Sheet1.Cells(Sheet1.Rows.Count, lCOLUMN), xlValues, xlWhole)
    lFound = 1
    lRepCount = 0

    If Not rFound Is Nothing Then
        Do
            If lFound > lMAX Then
                lRepCount = lRepCount + 1
                lFound = 1
            End If

            'if not enough in vaReplace, use the last value
            If lRepCount > UBound(vaReplace) Then lRepCount = UBound(vaReplace)

            rFound.Value = vaReplace(lRepCount)

            Set rFound = Sheet1.Columns(1).FindNext

            lFound = lFound + 1

        Loop Until rFound Is Nothing
    End If

End Sub

答案 1 :(得分:0)

可以导出为CSV吗?如果是这样,你可以这样做,然后在Linux或文本编辑器中使用常见的命令行工具,你知道它们具有你所概述的查找/替换功能。

您可以使用GnumericLibreOffice执行此操作(在Linux,Windows上)。