+ Reply to Thread
Results 1 to 7 of 7

Compare a value in a cell to a row/column on another sheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    07-04-2014
    Location
    canada
    MS-Off Ver
    Office Pro Plus 2019
    Posts
    9

    Question Compare a value in a cell to a row/column on another sheet.

    I hope someone is smarter than I with VBA and can help me out with this one.
    Basically I am looking for Excel VBA code to compare 4 cell values from 1 sheet to 4 columns on another sheet, but the 4 values on other sheet must be in same rows. I am but a VBA beginner and I have been trying for days with no luck.
    I want it to go something like this:

    IF A5 (on sheet1) = 40 AND B5 < (A value in column M on sheet2) AND C5 < (a value in column E on sheet 2 but in the same row as the previous) AND D5 < (a value in column F on sheet 2, also in same row as previous 2)
    THEN display model from column C on sheet 2 also from same column into sheet 1 G5

    for example (see attachment) on sheet1 if you enter 40 into A5, 3846 into B5, 1492 into C5 and 1999 into D5 Then "MODEL19" (from sheet2) should show up in G5.
    I had put in a 'button' but it is not needed if there is a better/easier way to do this.

    Can anyone help me with this, please?

    Thank you kindly in advance
    Attached Files Attached Files

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Compare a value in a cell to a row/column on another sheet.

    Looking at your workbook and for your example, there are 48 models that answer to all your criteria. Do you want the first one only? Or there is someting I did not understand. What does the 40 in cell A5? I don't see how you use it.
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

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

    Re: Compare a value in a cell to a row/column on another sheet.

    This formula in G5 will return the first that matches all criteria.

    This is an array formula.
    Confirm with Ctrl+Shift+Enter

    Formula: copy to clipboard
    =IF(AND(A5=40,COUNT(B5:D5)=3), INDEX(Sheet2!C8:C75,MATCH(1,(B5<Sheet2!M8:M75)*(C5<Sheet2!E8:E75)*(D5<Sheet2!F8:F75),0)),"")
    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.

  4. #4
    Registered User
    Join Date
    07-04-2014
    Location
    canada
    MS-Off Ver
    Office Pro Plus 2019
    Posts
    9

    Re: Compare a value in a cell to a row/column on another sheet.

    AlphaFrog, This formula does not seem to work.
    p24leclerc, Yes, I would like the first only.
    The 40 implies using columns M,N and O. As opposed to if one were to enter say 60 then one would need to look in columns S,T and U... But I will deal with that later. "40" is the most common one I would need

    Thank you.
    Last edited by shawnashannon; 07-07-2014 at 02:00 PM. Reason: addition

  5. #5
    Registered User
    Join Date
    07-04-2014
    Location
    canada
    MS-Off Ver
    Office Pro Plus 2019
    Posts
    9

    Re: Compare a value in a cell to a row/column on another sheet.

    Bump, not yet answered.

  6. #6
    Registered User
    Join Date
    07-04-2014
    Location
    canada
    MS-Off Ver
    Office Pro Plus 2019
    Posts
    9

    Re: Compare a value in a cell to a row/column on another sheet.

    Bump, not solved.

  7. #7
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Compare a value in a cell to a row/column on another sheet.

    Sorry for the delay.
    Here is a macro that could help you. I also attached a workbook with the macro in it.
    I commented the macro so you could see what the code stands for.
    Sub Button5_Click()
    Dim C_ell As Range
    Dim sh As Worksheet
    Application.ScreenUpdating = False
    Worksheets ("Sheet2")
    '
    'Select any cell in the table for the AutoFilter to determine the proper range to filter
    Range("C10").Select
    'Check if FILTER mode already on
    If Not ActiveSheet.AutoFilterMode Then
       'If not, then activate AutoFilter
       Selection.AutoFilter
    Else
       'If already activated, then ShowAllData (Kind of reset)
       ActiveSheet.ShowAllData
    End If
    '
    'Create 3 filters
    'They self align to the proper columns based on value in cells A5 in Sheet1
    ActiveSheet.Range("$C$8:$AD$" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=-1 + (Sheets("Sheet1").Range("A5") / 10 * 3), Criteria1:=">" & Sheets("Sheet1").Range("B5"), _
        Operator:=xlAnd
    ActiveSheet.Range("$C$8:$AD$" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=(Sheets("Sheet1").Range("A5") / 10 * 3), Criteria1:=">" & Sheets("Sheet1").Range("C5"), _
        Operator:=xlAnd
    ActiveSheet.Range("$C$8:$AD$" & Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=1 + (Sheets("Sheet1").Range("A5") / 10 * 3), Criteria1:=">" & Sheets("Sheet1").Range("D5"), _
        Operator:=xlAnd
    '
    'Copy first filtered value in Sheet1 cell G5
    Sheets("Sheet1").Range("G5") = ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 1)
    'Turn Autofilter off
    Selection.AutoFilter
    'Get back to Sheet1 to see the result
    Sheets("Sheet1").Select
    Range("G5").Select
    '
    Application.ScreenUpdating = True
    
    End Sub
    Hope this helps
    Attached Files Attached Files

+ 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. Compare and validate one column in one sheet with 2 different columns in other sheet
    By prasanna ponnaganti in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-24-2014, 02:20 PM
  2. Replies: 10
    Last Post: 03-31-2013, 07:40 PM
  3. Replies: 16
    Last Post: 01-20-2013, 09:40 AM
  4. Replies: 5
    Last Post: 09-24-2009, 03:46 AM
  5. Compare Sheet Cell to Sheet Column
    By Brenda in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-04-2006, 03:35 PM

Tags for this Thread

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