Replacing a value in a cell with another

ksenia

I am writing a macro in Excel spreadsheets to replace a value in one cell by the content of another cell and loop through the original text replacing the same value, whenever it sees this word. For example, I have a text in a range of cells, where every line has a word "tagname" I want to replace "tagname" with the value of cell A1 of the same spreadsheet, for example to say "Maggie" instead of tagname. This is my code thus far:

Private Sub CommandButton21_Click()
Dim OriginalText As Range
Dim CorrectedText As Range
'definition of ranges

Set OriginalText = Range("H4:H10")

'setting of ranges

For Each OriginalText In CorrectedText

CorrectedText.Value = Replace(OriginalText.Value, "tagname", Range("D2").Value)

Next OriginalText
'a loop through the original text to replace the word "tagname" with the value of cell D4

Columns(2).Clear 'clear column 2 for the Corrected Text
Range("A24:A30").Offset(, 1).Value = CorrectedText
'copy corrected text in these cells
End Sub

I get runtime error 424, object required.

Scott Craner

Just to put all of it together, this is how I would do it.

Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim i As Long
Dim cel As Range

Set correctedText = Range("B24")
Set OriginalText = Range("H4:H10")

OriginalText.Replace "tagname", Range("d4")
correctedText.Resize(OriginalText.Rows.Count).Value = OriginalText.Value
OriginalText.Replace Range("d4"), "tagname"

End Sub

Or if you really want the loop:

Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim i As Long
Dim cel As Range

Set correctedText = Range("B24")
Set OriginalText = Range("H4:H10")
i = 0
For Each cel In OriginalText
    correctedText.Offset(i).Value = Replace(cel.Value, "tagname", Range("d4"))
    i = i + 1
Next cel

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replacing an empty cell in one column based on corresponding value from another column?

From Dev

Copy the cell value to another cell if

From Dev

Change value in a cell based on value in another cell

From Dev

Replacing table value based on another table value

From Dev

Replacing a factor value in data frame by another value

From Dev

Excel copy cell value to another cell if cell value changes

From Dev

Excel copy cell value to another cell if cell value changes

From Dev

Add a value in a cell based on changes to another cell

From Dev

Delete value of a cell once used by another cell

From Dev

Format another cell based on this cell value

From Dev

Calculate the value of a cell based on another cell in Excel

From Dev

Use vba to copy value of cell to another cell

From Dev

MYSQL replace a cell based on the value of another cell

From Dev

Insert function in a cell if another cell changes value

From Dev

Fillup cell depending on value of another cell

From Dev

Cell value based on number of digits in another cell

From Dev

Fill a cell based on the value in another

From Dev

Insert another cell value into an IF function

From Dev

OpenCV replacing specific pixel values with another value

From Dev

Change colour of a cell based on the value in the cell and text in another cell

From Dev

Change the formatting of a cell depending on the value of that cell and another cell

From Dev

If cell value changes, copy value to another worksheet

From Dev

Value of a cell in the same row as the max value in another

From Dev

Replacing null values in powerquery column with value from cell above

From Dev

Excel - How to use the value of a cell as the row value of another cell?

From Dev

Get a cell value from a row based on another cell value

From Dev

Exclude cell value from rank range based on value in another cell

From Dev

Search for cell value then use it to find another value in cell array

From Dev

Clear Excel Cell if Value less than another Cell value

Related Related

  1. 1

    Replacing an empty cell in one column based on corresponding value from another column?

  2. 2

    Copy the cell value to another cell if

  3. 3

    Change value in a cell based on value in another cell

  4. 4

    Replacing table value based on another table value

  5. 5

    Replacing a factor value in data frame by another value

  6. 6

    Excel copy cell value to another cell if cell value changes

  7. 7

    Excel copy cell value to another cell if cell value changes

  8. 8

    Add a value in a cell based on changes to another cell

  9. 9

    Delete value of a cell once used by another cell

  10. 10

    Format another cell based on this cell value

  11. 11

    Calculate the value of a cell based on another cell in Excel

  12. 12

    Use vba to copy value of cell to another cell

  13. 13

    MYSQL replace a cell based on the value of another cell

  14. 14

    Insert function in a cell if another cell changes value

  15. 15

    Fillup cell depending on value of another cell

  16. 16

    Cell value based on number of digits in another cell

  17. 17

    Fill a cell based on the value in another

  18. 18

    Insert another cell value into an IF function

  19. 19

    OpenCV replacing specific pixel values with another value

  20. 20

    Change colour of a cell based on the value in the cell and text in another cell

  21. 21

    Change the formatting of a cell depending on the value of that cell and another cell

  22. 22

    If cell value changes, copy value to another worksheet

  23. 23

    Value of a cell in the same row as the max value in another

  24. 24

    Replacing null values in powerquery column with value from cell above

  25. 25

    Excel - How to use the value of a cell as the row value of another cell?

  26. 26

    Get a cell value from a row based on another cell value

  27. 27

    Exclude cell value from rank range based on value in another cell

  28. 28

    Search for cell value then use it to find another value in cell array

  29. 29

    Clear Excel Cell if Value less than another Cell value

HotTag

Archive