Find, and replace with offset a cell

TitoElan

New Member
Joined
Jun 10, 2022
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi there. I did one macro to find a value, then change a cell's value with an offset. Just a string. It is working. Now I just wanted to use the same macro, but I dont want to replace the content from the offset matching cells with a string but with a formula. And I have tried for hours to find a solution without success. Maybe I can get some help here. I would appreciate it. The code (last try) looks like this
That code finds well the cells, but without a relative formula and I need It.
If I try to use myFormula = "=IF(RC[-9]>0,IF(RC[-8]=1,VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,3,FALSE),VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,2,FALSE)),"""")" as formula nothing happends. Kinda sad. It is like almost done but I cant get it working.

VBA Code:
Sub Schaltfläche4_Klicken()
Dim find_value, replace_value As String
Dim i As Long
Dim myFormula As String
Dim Rng As Range
Dim Rng_2 As Range
Dim c As Range

On Error Resume Next

myFormula = "=IF(RC[-9]>0,IF(RC[-8]=1,VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,3,FALSE),VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,2,FALSE)),"""")"
find_value = InputBox("Wählen Sie den Profiltyp zB. QRR oder ROHR...")

 

i = 0
For Each Rng In Worksheets("Tabelle1").Range("E6:E28")
 
    If Rng.Value Like "*" & find_value & "*" Then
        Rng.Offset(0, 9).Formula = "=WENN(E17>0;WENN(F17=1;SVERWEIS(E17;matpreis!$A$1:$G$2567;3;FALSCH);SVERWEIS(E17;matpreis!$A$1:$G$2567;2;FALSCH));"""")": i = i + 1
        
    End If


        '=WENN(E17>0;WENN(F17=1;SVERWEIS(E17;matpreis!$A$1:$G$2567;3;FALSCH);SVERWEIS(E17;matpreis!$A$1:$G$2567;2;FALSCH));"""")
        '=IF(RC[-9]>0,IF(RC[-8]=1,VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,3,FALSE),VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,2,FALSE)),"""")
    

Next

If i = 1 Then
MsgBox i & " Zelle geändert"
ElseIf i > 1 Then
MsgBox i & " Zellen geändert"
Else
MsgBox "Keine Zellen geändert"
End If



End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi & welcome to MrExcel.
How about
VBA Code:
        Rng.Offset(0, 9).FormulaR1C1 = myFormula
 
Upvote 0
Thank you very much for your answer. I have this variable because I already tried that. Nothing happens
But if I use the
VBA Code:
Rng.Offset(0, 9).FormulaR1C1 = myFormula
And I delete the "=" from the formula like this
VBA Code:
myFormula = "IF(RC[-9]>0,IF(RC[-8]=1,VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,3,FALSE),VLOOKUP(RC[-9],matpreis!R1C1:R2567C7,2,FALSE)),"""")"
It works like a string, but at least it does something. I think the problem with the code is that Excel can't find the cells with the VBA structure.
 
Upvote 0
If you remove this line On Error Resume Next do you get any error messages?
 
Upvote 0
If you remove this line On Error Resume Next do you get any error messages?
Error 1004 - application defined or object defined Error
At this point

VBA Code:
Rng.Offset(0, 9).FormulaR1C1 = myFormula
 
Upvote 0
What happens if you use
VBA Code:
myFormula = "=WENN(E17>0;WENN(F17=1;SVERWEIS(E17;matpreis!$A$1:$G$2567;3;FALSCH);SVERWEIS(E17;matpreis!$A$1:$G$2567;2;FALSCH));"""")"
 
Upvote 0
Solution
What happens if you use
VBA Code:
myFormula = "=WENN(E17>0;WENN(F17=1;SVERWEIS(E17;matpreis!$A$1:$G$2567;3;FALSCH);SVERWEIS(E17;matpreis!$A$1:$G$2567;2;FALSCH));"""")"
The code correctly finds the cells to change and writes the formula but as an absolute formula. Without errors
 
Upvote 0
In that case just that formula so it uses r1c1 references.
 
Upvote 0
Just change the range references from A1 notation to R1C1 notation as you've done for the English version of the formula.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top