【问题标题】:Replace words in cell between two same cell words - Excel在两个相同的单元格单词之间替换单元格中的单词 - Excel
【发布时间】:2016-05-30 12:23:25
【问题描述】:

我有一张在不同单元格中填满不同单词的工作表。
例如:
字1 字2 字3 字1 字4
字1 字2 字1 字4

我需要一个公式/宏来替换两个相同给定单词之间的单词,即

Word1 Word2 Word3 Word1 Word4 替换为
字 1 字 1 字 1 字 1 字 4


(对于上述内容,我们在找到它的整行中将 Word2、Word3 替换为 Word1)

Word1 Word2 Word1 Word3 替换为
字1 字1 字1 字4

如果您需要更多详细信息,请告诉我。

谢谢!

【问题讨论】:

  • 所以你想要一个循环遍历工作表中所有单元格并将 X 替换为 Y 的函数?函数应该迭代的区域是否有限制?是什么决定了哪些词应该用什么代替?它是静态的,还是基于用户输入的?
  • 欢迎来到 SO。我们需要更多的细节来回答这个问题。最大列数、单元格之间的最大间隙、单元格是否可以位于多于一行等。更重要的是,我们需要查看您已经完成的操作以及为什么它不适合您的解释。您有机会为我们编辑问题吗?
  • 任何东西都适合我,无论是函数还是 vba 脚本。数据可能很大,即 35 列和 10,000 行。要确定单词之间应该替换哪个单词 1-如果我们在 Word1 之间有 Word2 即 Word1 Word2 Word1,我们需要将 Word2 替换为 Word1 .......... 2-如果我们在 Word1 之间有 Word2 Word3 即Word1 Word2 Word3 Word1,我们需要将Word2 Word3替换为Word1......这个条件需要逐行检查30列............3-我们应该' t 替换一个单词,如果它是 Word1 Word4 Word1 Word1...这里系列保持不变。
  • Ambie- 最大列数为 35,并且不会有任何空白单元格......只有 4-6 种单词......Word1 Word2 Word3 Word4 Word5 Word6。 ....我是手动做的,因为我别无选择....尝试使用查找和替换公式并没有帮助。您能否进一步查看之前的评论,因为它可以为您提供更好的图片。
  • 如果你有一行 Word1 Word2 Word1 Word2,它应该转换为 Word1 Word1 Word1 Word 2 还是 Word1 Word2 Word2 Word2

标签: excel excel-formula excel-2010 excel-2007 vba


【解决方案1】:

我在您提供的两种情况下基于下面的代码,即如果在 word1 之间有 word2 更改它,如果在 word1 之间有 word2 和 word3(我假设它们按这个顺序出现,并且总是在两个单元格之间word1) 改变它们。

由于缺乏信息,该方法有点粗略,但我了解您可能出于保密目的无法分享更多内容。

Private Sub sub1()
Dim rng As Range
Dim word1 As String, word2 As String, word3 As String
Dim word4 As String, word5 As String, word6 As String

Set rng = Application.InputBox("Input the range you want to substitute", "Input", Type:=8)

word1 = "WordInCell1"
word2 = "WordInCell2"
word3 = "WordInCell3"
word4 = "WordInCell4"
word5 = "WordInCell5"
word6 = "WordInCell6"

For Each c In rng
    With c
        If .Value = word1 Then
            'Check for first case
            If .Offset(0, 2).Value = word1 Then
                'Check if the word in between the two words1 is word2
                If .Offset(0, 1).Value = word2 Then
                    .Offset(0, 1).Value = word1
                End If
            'Check for second case
            ElseIf .Offset(0, 3).Value = word1 Then
                'Check if the two words in between are word2 and word3 - assuming they always come in that order
                If .Offset(0, 1).Value = word2 And .Offset(0, 2).Value = word3 Then
                    .Offset(0, 1).Value = word1
                    .Offset(0, 2).Value = word1
                End If
            Else
                'Do Nothing
            End If
        End If
    End With
Next c

End Sub

【讨论】:

  • 感谢精灵。此代码非常适合将 Word1 Word2 Word1 修改为 Word1 Word1 Word1。此外,如果我将 Word1 Word2 Word2 Word1 更改为 Word1 Word1 Word1 Word1,我什至需要更改顺序。感谢您为帮助我所做的努力:)
  • 我把它修好了,精灵。只是在此语句中将 word3 更改为 word2 :“If .Offset(0, 1).Value = word2 And .Offset(0, 2).Value = word3 Then”
  • 精灵,您能帮我解决上述问题的另一个条件吗?我需要将“word2”替换为“word1”,其中在任意数量(最多 35 个)“word1”之间有“word2”。
  • 我尝试了以下但不起作用...... ElseIf .Offset(0, 5).Value = word1 Then If .Offset(0, 1).Value = word2 And . Offset(0, 2).Value = word2 And .Offset(0, 3).Value = word2 And .Offset(0, 4).Value = word2 And .Offset(0, 5).Value = word2 然后.Offset( 0, 1).Value = word1 .Offset(0, 2).Value = word1 .Offset(0, 3).Value = word1 .Offset(0, 4).Value = word1 .Offset(0, 5).Value = word1 结束如果
  • 你的意思是最多5个还是35个?
【解决方案2】:

这是解决您问题的更通用的解决方案,无需对您要查找的字符串进行硬编码。

Private Sub replaceWords()
    Dim rng, currentRow As Range
    Dim currentCol, firstCol, lastCol As Integer
    Dim word1 As Variant
    Dim i, j As Integer

    Set rng = Application.InputBox("Input the range you want to substitute", "Input", Type:=8)

    'Disect the range into column numbers
    firstCol = rng.Column
    lastCol = rng.Cells.SpecialCells(xlCellTypeLastCell).Column

    'Iterate through all rows
    For Each currentRow In rng.Rows
        currentCol = firstCol 'start at the left side of the range
        While currentCol < lastCol
            'Set word1 to the value of the cell in the currenct column
            word1 = Cells(currentRow.Row, currentCol).Value

            'Check subsequent columns for the same cell
            For i = currentCol + 1 To lastCol
                If Cells(currentRow.Row, i).Value = word1 Then
                    'If same word1 is found, fill cells in between with word1
                        For j = currentCol + 1 To i - 1
                            Cells(currentRow.Row, j) = word1
                        Next j
                    'Continue search at the column where word1 was last encountered position
                    currentCol = i - 1 '-1 becaus we add 1 again at the end of the while loop
                    Exit For
                End If
            Next i
            currentCol = currentCol + 1 'go to next column
        Wend
    Next

End Sub

这将从左到右工作,因此“Word1 Word2 Word1 Word2”将变为“Word1 Word1 Word1 Word2”而不是“Word1 Word2 Word2 Word2”。如果你想反过来调整代码应该不会太难。

【讨论】:

  • 这不像我预期的那样工作。无论给定条件如何,它将所有 Word1 转换为 Word2。
  • 感谢您的评论。我意识到我可能误解了这个问题。这段代码的作用如下:如果任何单词 - 无论是 Word1 或 Word2 还是其他任何单词 - 连续出现两次,它将用该单词替换其间的所有单词。现在我了解到您实际上想要指定要替换的 Word。在这种情况下,@Genie 的代码确实是一个更好的解决方案。
  • 感谢您帮助我摆脱 NiH :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
相关资源
最近更新 更多