+ Reply to Thread
Results 1 to 17 of 17

Setting range in vba based on vlookup criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Setting range in vba based on vlookup criteria

    Hi,

    Consider the following Table;

    ----- A------------B
    1---Blue ----------23
    2---Black----------45
    3---Grey ---------32
    4---Pink ----------60

    I set range as follows;
    Set rngData = .Range("B3").Resize(1, Sheet1.Range("E5"))
    I am just concerned with red part of above code. Istead of manually setting it as B3 I want it to be a cell in column B in front of "Grey" in column A, thus it would automatically become B3. It is something like vlookup.

    I hope I explained my problem for your understanding, but if not, kindly let me know so I may redraft my problem.

    Thank you all
    Last edited by caabdul; 02-07-2014 at 02:10 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Setting range in vba based on vlookup criteria

    Try something like this...

    Set rngData = .Range("A:A").Find(What:="Grey", LookIn:=xlValues, LookAt:=xlWhole, _
                   SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not rngData Is Nothing Then
        Set rngData = rngData.Offset(, 1).Resize(1, Sheet1.Range("E5"))
    Else
        MsgBox "No match for 'Grey' found in column A. ", , "No Match Found"
    End If
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    Will this work?

    Set rngData = .Range("A:A").Find(What:="Grey", LookIn:=xlValues, LookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Offset(, 1).Resize(1, Sheet1.Range("E5"))

  4. #4
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    AlphaFrog

    Thank you for help, but I am a bit confused. Is MsgBox necessary? because there will be no chance that "Grey" is not in column A.
    Can you please trim this code.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Setting range in vba based on vlookup criteria

    Quote Originally Posted by caabdul View Post
    AlphaFrog

    Thank you for help, but I am a bit confused. Is MsgBox necessary? because there will be no chance that "Grey" is not in column A.
    Can you please trim this code.
    Set rngData = .Range("A:A").Find(What:="Grey", LookIn:=xlValues, LookAt:=xlWhole, _
                   SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
                   .Offset(, 1).Resize(1, Sheet1.Range("E5"))

  6. #6
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    I face this error:

    Run_time Error '91':
    Object variable or with block variable not set
    Last edited by caabdul; 02-06-2014 at 08:45 PM.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Setting range in vba based on vlookup criteria

    That's what the If statement was for; test for no match.

    Does the cell with Blue have a trailing space; e.g. "Blue "? If yes, you could change LookAt:=xlPart, but that would match any cell that contains Blue e.g.; Bluejay.

  8. #8
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    I don't know why but this code is working fine with grey but when I change it to "Blue" it is not working.

  9. #9
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    It is the actual code:

    Set rngData5 = .Range("C:C").Find(What:="TOTAL Runs & Scores", LookIn:=xlValues, LookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True) _
    .Offset(, 1).Resize(1, Sheet34.Range("E5"))
    I have set rngData1, rngData2, rngData3, rngData4 and rngData6 also. All these are working fine. Only rngData5 displays above error.

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Setting range in vba based on vlookup criteria

    You changed the MatchCase:=True. Does the case match exactly?

  11. #11
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    Quote Originally Posted by AlphaFrog View Post
    You changed the MatchCase:=True. Does the case match exactly?
    Yes case match exactly. I have tried with both true and false MatchCase. Both don't work.
    Trying LookAt:=xlPart also not work

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Setting range in vba based on vlookup criteria

    Do you have a Sheet34 in the same workbook?

    Sheet34.Range("E5")


    What value is in Sheet34.Range("E5") ?

  13. #13
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    Kindly check this file, code isn't working in it at all.
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Setting range in vba based on vlookup criteria

    It should be H1 not H12

    Sheet1.Range("H12")

  15. #15
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    Quote Originally Posted by AlphaFrog View Post
    It should be H1 not H12

    Sheet1.Range("H12")
    sorry it must be H1, then it works.

  16. #16
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Setting range in vba based on vlookup criteria

    I formatted column C as TEXT, It worked.

    One more question please,

    I have range in vba code as
    range.("C6:C14")
    When I insert a new row, the range must set to range.("C6:C15").

    Can it be accompalished? If yes, what is this procedure called so I may have some google search about it.

    Thank you

  17. #17
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Setting range in vba based on vlookup criteria

    Create a name range for C6:C14 (called MyRange in this example)

    Reference the named range in the code
    Range("MyRange")

    If you insert a row within the named range, it will automatically adjust.

    You could do the same with a Table.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Setting a range based on variables
    By DFrank231 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-22-2013, 04:23 PM
  2. Replies: 6
    Last Post: 09-04-2012, 10:35 AM
  3. setting criteria in a range
    By canberry in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-27-2009, 04:58 PM
  4. Setting Counta range based on a date
    By Weasel in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-02-2009, 04:37 PM
  5. Setting Range based on LastCell
    By matt4003 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-21-2008, 07:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1