【问题标题】:Find and replace part of string in one cell with value of cell below it with VBA Macro使用 VBA 宏查找并替换一个单元格中的部分字符串及其下方单元格的值
【发布时间】:2014-12-21 01:12:40
【问题描述】:

我在 excel 的一行中有多个单元格,所有单元格都包含 HTML。每个单元格使用不同的语言,但始终具有相同的 URL,例如:(...loc=en-uk) 我需要在每个单元格中查找并替换它,但每个单元格的值不同,例如:find "en-uk " 在单元格 A 中,替换为“it-it”,移动到下一个单元格,找到“en-uk”,替换为“es-es”,依此类推,直到到达空单元格。

尝试了以下方法,但它只需要从相同的 2 个单元格中查找和替换值:

Dim Findtext As String 
Dim Replacetext As String 

Findtext = Range("B2").Value 
Replacetext = Range("C2").Value 
Cells.Replace What:=Findtext, Replacement:=Replacetext, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False 

【问题讨论】:

  • 上传工作表的屏幕截图链接和代码(如果您尝试过)。
  • 尝试了以下方法,但它只需要从相同的 2 个单元格中查找和替换值: Dim Findtext As String Dim Replacetext As String Findtext = Range("B2").Value Replacetext = Range("C2 ").Value Cells.Replace What:=Findtext, Replacement:=Replacetext, LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False
  • 在发布代码时尝试使用正确的格式。您可以通过添加工作表的代码和超链接来编辑您的问题。

标签: vba excel


【解决方案1】:

您将希望使用单元格而不是范围,并使用变量“lCol”循环遍历列。 Range(Column Letter , Row Number) 有它的限制,因为您使用的是字母。很难将 1 加到 C.. C + 1 = D?所以使用 Cells(row#, col#)。这样,您就可以使用数字而不是字母逐步遍历列或行。在这种情况下,变量是列,因此我们将使用 lCol 并从 1 循环到最后一个列号。使用您的原始帖子作为起点替换原始源字符串中的文本。

注意:我不确定您的原始字符串在哪里。此外,如果您知道要从原始字符串中替换什么,并且发现自己在整个表格中看到 Row2 都是相同的东西,您可以在下面的 replaceString 语句中进行设置。

试试这个 - 一旦你取出所有的 cmets,它只有几行。

Sub TextReplace()

Dim sheetName As String
Dim findText As String
Dim replaceText As String
Dim lastCol As Long
Dim lCol As Long

'Set the sheetName here, so you don't have to change it multiple times later
sheetName = "Sheet1"

'Check the Last Column with a value in it in Row 3.  Assuming Row 3 has the Replacement text.
lastCol = Sheets(sheetName).Cells(3, Columns.Count).End(xlToLeft).Column

'Assuming you have an original string in a cell.  Range("A1"), in this case.
origString = Sheets(sheetName).Cells(1, 1).Value

'Loop through each Column - lCol is going to increase by 1 each time.  
'Starting on Column B,since Column A contains your original String.
For lCol = 2 To lastCol

'Using Cells(row#, col#), instead of Range(ColLetter, row#) gives you the ability to loop with lCol.
    findText = Sheets(sheetName).Cells(2, lCol).Value
    replaceText = Sheets(sheetName).Cells(3, lCol).Value

    'Put the value from Range("A1") with the text replaced from Row 2 with Row 3's value.
    Sheets(sheetName).Cells(1, lCol) = Replace(origString, findText, replaceText)

Next lCol

End Sub

编辑:更新 Column 从 B 开始,而不是 A。

【讨论】:

    【解决方案2】:

    试试这个(例如第 16 行和第 20 行):

    Sub ReplaceTextinRow()
    Dim lastCol, iter
    lastCol = ActiveSheet.UsedRange.Columns.Count 'this approach is not always applicable
    
    For iter = 1 To lastCol
    'Cells(16, iter).Offset(4, 0).Value = Replace(Cells(16, iter).Value, "en-us", "en-uk")
    
    Cells(20, iter).value = Replace(Cells(20, iter).value, Cells(20, iter).Offset(-4, 0).Value)
    Next
    End Sub
    

    【讨论】:

    • 感谢您的 ZAT,但这似乎只是将第 20 行中单元格的整个值替换为第 16 行同一列中的单元格值。这非常好......但我需要能够找到并用单元格 16 的值替换第 20 行单元格中包含的字符串的一部分。示例:Find hello .com 替换为 你好 .ie
    • 好的,这给出了编译错误:参数不是可选的(替换)
    猜你喜欢
    • 2021-02-11
    • 2018-04-22
    • 2017-05-23
    • 2021-09-12
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    相关资源
    最近更新 更多