-1

Let's say I have a column in excel where many cells are populated with values from sheets and a formula. For example, a cell has '82'!G7*2*F3^2*$F$6. (where 82 is the name of the sheet values are coming from. I want to write a for or loop that goes through all the cells and change all the 82s to something else, 85 for example. and also the $F$s to $H$.

What's the code to find these certain parts to then be able to replace them within the formula or whatever it is that is in a cell?

2
  • 1
    Have you tried recording the Find & Replace action performed directly on the worksheet? (Ctrl+H)
    – user4039065
    Jul 8, 2015 at 17:32
  • Yes, but it doesn't work. The macro seems to run, but no changes. In fact it does but I cannot control it. I could write Columns("Z:Z").Select ActiveCell.Replace What:="A", Replacement:="B", LookAt:=xlPart, _ SearchOrder:=xlByColumns, MatchCase:=True, SearchFormat:=False, _ ReplaceFormat:=False and it works but it I change "A" and "B" to something like Range("X1").Value it does not anymore. I also forgot to mention that I want the string that is to be found, and its replacement to be input parameters.
    – Jenny P
    Jul 8, 2015 at 17:56

1 Answer 1

2

For example looping through column A using i as the counter:

Cells(i,1).Formula = Replace(Cells(i,1).Formula,"82","85")
Cells(i,1).Formula = Replace(Cells(i,1).Formula,"$F$","$H$")
1
  • Thanks, this one finally worked. Still it wouldn't work if I used column numbers. I had to change the number to the name of the column.
    – Jenny P
    Jul 8, 2015 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.