Continuation of Excel Formula

zalik22

Board Regular
Joined
Dec 14, 2010
Messages
122
Office Version
  1. 365
Platform
  1. Windows
Hi, I am trying to enter the following code so the following formula copies in column E but keep getting an error. The formula is about 3 times longer, I shortened it for the post.

With Sheets("data")
.Range("E9:E" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula ="
IF(ISERROR(SEARCH("*aaa*",f9,1))=FALSE,"aaa",
IF(ISERROR(SEARCH("*bbb*",f9,1))=FALSE,"bbb",
IF(ISERROR(SEARCH("*ccc*",f9,1))=FALSE,"ccc",
IF(ISERROR(SEARCH("*ddd*",f9,1))=FALSE,"ddd","error"))))"

Thanks!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
The easiest way to do this is to turn on the Macro Recorder, and record yourself entering the formula in cell F9 (or if the formula is already there, turn on the Macro Recorder, go to that cell, press F2 to edit the formula, and then press enter).

This will give you the formula in relative reference format (.FormulaR1C1), which is the best way of doing it when applying the formula to a multi-cell range.
So, your code would look something like:
Code:
[COLOR=#333333]With Sheets("data")[/COLOR]
[COLOR=#333333]    .Range("E9:E" & .Cells(.Rows.Count, "A").End(xlUp).Row).[/COLOR][COLOR=#ff0000]FormulaR1C1[/COLOR][COLOR=#333333] = "[I]your recorded formula here[/I]"
[/COLOR]End With
 
Upvote 0
Try
Code:
    With Sheets("data")
        With .Range("E9:E" & .Cells(.Rows.Count, "A").End(xlUp).Row)
            .FormulaR1C1 = _
                "= IF(ISERROR(SEARCH(""*aaa*"",RC[1],1))=FALSE,""aaa"", " & _
                " IF(ISERROR(SEARCH(""*bbb*"",RC[1],1))=FALSE,""bbb"", " & _
                " IF(ISERROR(SEARCH(""*ccc*"",RC[1],1))=FALSE,""ccc"", " & _
                " IF(ISERROR(SEARCH(""*ddd*"",RC[1],1))=FALSE,""ddd"",""error""))))"
            Application.Calculate
            Do While Application.CalculationState <> xlDone: DoEvents: Loop
            .Value = .Value
        End With
    End With

The easiest way I have found to get a cell formula into VBA is to get it working in the cell then use the macro recorder to translate it into VBA. It may take a bit of editing, but it is easier than composing in in the VBE.
 
Last edited:
Upvote 0
It works, but for some reason E9 to E15 is blank, even though there is data in Column A. Any ideas?
 
Upvote 0
Stop the code so the formulae are still live and examine each portion of the formula clicking on parts of the formula in the formula bar and then clicking fx (at the left end of the formula bar) to see what the part evaluates to.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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