Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:16:01+00:00 2026-06-14T16:16:01+00:00

My code sets list validation on a cell using the range given by an

  • 0

My code sets list validation on a cell using the range given by an OFFSET formula that depends on that cell’s address.

Because this validation gets set programmatically on cells that already have contents, I want a function to determine whether the existing contents of the cell violate validation.

I ran into problems testing this, using the following code:

Sub test()
    Dim sht As Worksheet
    Dim rng As Range
    Dim formula As String
    Dim rangeName As String
    Set sht = ThisWorkbook.Sheets("Sheet1")
    With sht
        .Range("a1").Value = "a"
        .Range("a2").Value = "aa"
        Set rng = .Range("b1")
        formula = "=OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),0,-1,2,1)"
        rng.Validation.Add Type:=xlValidateList, Formula1:=formula
        rangeName = Replace(formula, "=", "")
        Set ResultRange = .Range(rangeName).Find(rng.Value, lookat:=xlWhole)
        If ResultRange Is Nothing Then
            Debug.Print "violates validation"
        Else
            Debug.Print "validated"
        End If
    End With
End Sub

This test should assign drop-down validation to cell B1 with the contents of cells A1 and A2, then check whether or not B1‘s contents match either of those cells. But it fails at the Set ResultRange line with this error:

Method 'Range' of object '_Global' failed.

I think the problem is that when I call Range(rangeName), Excel doesn’t know which cell to apply the ROW() and COLUMN() to, so I need to somehow calculate the address that formula resolves to for this cell–in this case, $A$1:$A$2, so that I can assign that value to RangeName.

So, given this OFFSET() formula that I’m passing dynamic parameters to, how do I get the range that this formula returns for the range specified by rng?

EDIT: As the formula may change depending on different circumstances, I’m looking for a solution that, given an OFFSET() formula starting at a certain range, returns the range given by that formula. A hard-coded solution would not be acceptable, as there are several different OFFSET() formulas like this in the sheet and it will be impractical to change the code of the validation test every time one of the formulas changes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:16:03+00:00Added an answer on June 14, 2026 at 4:16 pm

    This isn’t pretty, but will do what you like you must have already defined rng, and rng must not be in column A:

    rangeName = .Range(.Cells(rng.Row, rng.Column - 1), .Cells(rng.Row + 1, rng.Column - 1)).Address
    

    As another note, your validation will by default ignore blanks so you may want to update your if to check for blanks:

    If ResultRange Is Nothing And rng.Value <> "" then
        Debug.Print "violates validation"
    Else
        Debug.Print "validated"
    End If
    

    EDIT as requested, I went ahead and made the OffsetFormula optional because it will assume the passed range has the formula that needs parsed:

    Function ReturnRange(initialRange As Range, Optional OffsetFormula As String) As Range
        Dim parameters
        Dim upBound As Long
        Dim topLeftCell As Range
        Dim bottomRightCell As Range
        On Error Resume Next
        'If an error occurs result will be Nothing
        If OffsetFormula = "" Then OffsetFormula = initialRange.Formula
        If UCase(Left(OffsetFormula, 7)) = "=OFFSET" Then
            parameters = Split(Left(OffsetFormula, Len(OffsetFormula) - 1), ",")
            'Using upbound variable to avoid constant calls to Ubound(parameters)
            upBound = UBound(parameters)
            Set topLeftCell = initialRange.Offset(parameters(upBound - 3), parameters(upBound - 2))
            Set bottomRightCell = topLeftCell.Offset(parameters(upBound - 1) - 1, parameters(upBound) - 1)
            Set ReturnRange = Range(topLeftCell, bottomRightCell)
        End If
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like this: public class Polynomial { List<Term> term =
the code below sets up a List object in the main controller class that
This code still sets the field to today's date <script type=text/javascript> $(document).ready(function () {
I have some code that sets up a dictionary with some defualt values for
I have some code that sets the lang var to the site default language.
I have a code that sets a new repeating alarm (on production I'll use
I have the following code that sets a background if user has uploaded to
i have this simple question please. I have this part of code which sets
Below is the code i used. I have 2 sets of list boxes. If
I have a little code that takes a list of objects, and only outputs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.