简体   繁体   English

使用VBA宏在一个单元格中查找并替换其下面的单元格值替换字符串的一部分

[英]Find and replace part of string in one cell with value of cell below it with VBA Macro

I have multiple cells on one row in excel that all contain HTML. 我在excel的一行上有多个单元格,都包含HTML。 Each cell is in a different lang but has the same URLs throughout eg: (...loc=en-uk) I need to find and replace this in each cell but with a different value for each cell eg: find "en-uk" in cell A, Replace with "it-it", mover to next cell, find "en-uk", replace with "es-es" and so on till empty cell is reached. 每个单元格都位于不同的语言中,但整个URL都具有相同的URL,例如:(... loc = en-uk)我需要在每个单元格中查找并替换此URL,但每个单元格的值都不同,例如:find“ en-uk在单元格A中,替换为“ it-it”,移动到下一个单元格,找到“ en-uk”,替换为“ es-es”,依此类推,直到到达空单元格为止。

Had tried the following but it only takes find and replace values from the same 2 cells: 尝试了以下操作,但只需要查找和替换相同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 

You will want to use Cells, instead of Range, and loop through the columns using a Variable "lCol". 您将要使用单元格而不是范围,并使用变量“ lCol”遍历各列。 Range(Column Letter , Row Number) has it's limitations because you are using a letter. 范围(列字母,行号)有其局限性,因为您使用的是字母。 It's hard to add 1 to C.. C + 1 = D? 很难将C加1。C + 1 = D? So use Cells(row#, col#). 因此,请使用Cells(row#,col#)。 That way, you can incrementally work your way through the columns or rows using numbers instead of letters. 这样,您可以使用数字而不是字母逐步地遍历列或行。 In this case, the variable is the column, so we will use lCol and loop from 1 to the Last Column Number. 在这种情况下,变量是列,因此我们将使用lCol并从1到最后一个列号循环。 Replacing the text from an original source string using your original post as a starting point. 以原始帖子为起点,从原始源字符串替换文本。

Note: I'm not sure where your original string was. 注意:我不确定您的原始字符串在哪里。 Also, if you know what you are replacing from the original string, and find yourself looking at Row2 being all the same thing across the sheet, you can just set that in your replaceString statement below. 另外,如果您知道要从原始字符串中替换什么,并发现Row2在整个工作表中都一样,则可以在下面的replaceString语句中进行设置。

Try This - It's only a few lines, once you take out all the comments. 尝试一下-删除所有评论后,仅需几行。

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

Edit: Updated Column to start on B, instead of A. 编辑:更新列开始于B,而不是A。

Try this (for row 16 and for row 20 for example): 尝试以下操作(例如对于第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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM