Have formula in VBA be based on offsets?

Knockoutpie

Board Regular
Joined
Sep 10, 2018
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Is there any way to re-write this formula to basically say instead of A5, Offset -2 rows from "brand"

The goal here is to have the macro find the cell with text "Brand" in row 3, once found, offset down 2 cells and apply conditonal formatting to the range of data for that column (not the first 4 rows)

VBA Code:
    Rows(3).Find("Brand", LookIn:=xlValues, lookat:=xlWhole).Offset(-2)

Cells.Find(What:="EndCustName").Activate

ActiveCell.Offset(-2, 0).Select

ActiveCell.Value = "Import"


    Range("A5").Select

    Range(Selection, Selection.End(xlDown)).Select

    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _

        "=ISERROR(XLOOKUP($A5,Brands!$A:$A,Brands!$A:$A))"

    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

    With Selection.FormatConditions(1).Interior

        .PatternColorIndex = xlAutomatic

        .Color = 255

        .TintAndShade = 0

    End With

    Selection.FormatConditions(1).StopIfTrue = False
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
If should be possible base things on an offset. One example.
VBA Code:
    Dim rngFound As Range
    Set rngFound = Rows(3).Find("Brand", LookIn:=xlValues, lookat:=xlWhole)
    If Not rngFound Is Nothing Then
        'Found "Brand"
        With rngFound.Offset(2)  '+2 is down, -2 is up
            '
            ' your code here
            '
        End With
    Else
        'Did not find "Brand"
        MsgBox "'Brand' not found" 'msgbox optional
    End If
 
Upvote 0
If should be possible base things on an offset. One example.
VBA Code:
    Dim rngFound As Range
    Set rngFound = Rows(3).Find("Brand", LookIn:=xlValues, lookat:=xlWhole)
    If Not rngFound Is Nothing Then
        'Found "Brand"
        With rngFound.Offset(2)  '+2 is down, -2 is up
            '
            ' your code here
            '
        End With
    Else
        'Did not find "Brand"
        MsgBox "'Brand' not found" 'msgbox optional
    End If
That part I understand, this part I don't.

Basically, conditional formatting is applying a formula to lookup the values from a 2nd sheet "brand" if the value is not found, it highlights the unfound cell in red (because it's spelled incorrectly)

The formula is set to start on A5 (lookup value), but I'd like it to be 2 cells below where "brand" is found.

"=ISERROR(XLOOKUP($A5,Brands!$A:$A,Brands!$A:$A))"

you see here the formula for conditional formatting is set on A5, but is there a way to have it as "active cell" or something like that?
 
Upvote 0
There is nothing to prevent you from using VBA to build a formula with an offset built in, but if all you want to do is highlight cells that the XLOOKUP function does not return a match for, there are easier ways that complicated offset formulas. Instead take advantage of the [if_not_found] optional parameter to establish a standard not found return value and base your conditional formatting off that.


1668640524790.png
 
Upvote 0

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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