简体   繁体   English

填写空白Excel公式以替换单元格中的特定文本

[英]Fill in the blanks excel formula to replace particular text in a cell

I'm looking for a formula to find and replace particular piece of text in a cell. 我正在寻找一个公式来查找和替换单元格中的特定文本。 It sound a bit confusing but you can see what I mean by viewing the following image. 听起来有些混乱,但是通过查看下图您可以明白我的意思。

在此处输入图片说明

What I'm trying to achieve is when I fill for example cell B1 I would like to replace " SYS-NAME " in cell A25 and other cells where " SYS-NSME " is present. 我正在努力实现的是当我填写例如B1单元格,我想在小区A25和其他细胞,其中“SYS-NSME”的存在是为了取代“SYS-NAME”。

The short answer would be 简短的答案是

=SUBSTITUTE(A1,"**SYS-NAME**","MYSYSNAME")

starting in B1. 从B1开始。

You need to replace the ** before and after your values. 您需要在值前后替换** You could change it to "" instead if you want to highlight those inserted values. 如果要突出显示那些插入的值,则可以将其更改为"" ** will cause the entire cell to be replaced, instead of its match. **将导致整个单元被替换,而不是匹配。

So the code splits column A into 2 sub columns. 因此,代码将A列分为2个子列。 The first column runs until the first blank row, this contains all your variables and their new value. 第一列一直运行到第一行空白为止,其中包含所有变量及其新值。 The second column runs until the end of the last used row. 第二列一直运行到最后使用的行的末尾。 This contains your configuration with the variables that need to be changed. 这包含您的配置以及需要更改的变量。

Sub FindReplace()
    Dim NewText, OldText As String
    Dim LastRowText, i As Integer

    LastRowText = Range("A1").End(xlDown).Offset(1, 0).Row
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    i = 1

    Do While i < LastRowText
        OldText = Range("A" & i).Value
        NewText = Range("B" & i).Value
        With ActiveSheet.Range("A" & LastRowText & ":A" & LastRow)
            .Replace OldText, NewText, xlPart
        End With
        i = i + 1
    Loop
End Sub

Option without a macro 没有宏的选项

In your configuration, edit each line with a variable to the following format. 在您的配置中,使用变量将每一行编辑为以下格式。 This will instantly update the configuration file too. 这也将立即更新配置文件。

Cell A25: ="sysname " & B1
Cell A34: ="irf domain " & B2
...

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

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