+ Reply to Thread
Results 1 to 6 of 6

How to equal two cells if they have different values

Hybrid View

  1. #1
    Registered User
    Join Date
    10-05-2018
    Location
    Athens,Greece
    MS-Off Ver
    Office 365
    Posts
    7

    How to equal two cells if they have different values

    I have a database with some systems. I have made a marco that loop through the data and if they meet certain criteria are copied to differenct sheet. Something like this

    lrow = Sheets("database").Range("database").Rows.Count + 1
    For i = 3 To lrow
      If Sheets("Database").cells(i, "J").Value >= Sheets("Results").Range("C2").Value And _
         Sheets("Database").cells(i, "K").Value <= Sheets("Results").Range("D2").Value And _
         Sheets("Database").cells(i, "L").Value <= Sheets("Results").Range("E2").Value And _
         Sheets("Database").cells(i, "N").Value = Sheets("Results").Range("G2").Value Then
         Worksheets("Database").Rows(i).EntireRow.Copy
         Worksheets("Results").Range("I" & i + 12).End(xlUp).Offset(1, -8).PasteSpecial xlPasteValuesAndNumberFormats
    My problem is that cell G2 might have the word "marine" and the cells N can have one of the words "all", "marine", "brackish". I want,for example, the word "marine" to be included in the word "all" so that the macro will select the system that have the word all if the criteria is marine.

    I hope I explained it correctly.
    Last edited by mariosex; 10-09-2018 at 12:54 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,983

    Re: How to equal two cells if they have different values

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,493

    Re: How to equal two cells if they have different values

    Maybe like this
    sub test()
    Dim lrow as long,x,s$,arr
    
    With sheets("database")
      x = .range("C2",.[N10000].end(3)).value
    End with
    
    With sheets("Result")
     For i = 3 To ubound(x)
      If ((x(i,10)>=.[c2])*(x(i,11)<=.[d2].value) _
         *(x(i, 12)<=.[E2].Value) * _
         (x(i, 14) = .[g2].value)) Then
           s = iif(s="",i,s & "," & i)
           arr = split(s,",")
      End if
     Next i
    End with
         .Range("I12").Offset(1, -8).resize(ubound(arr),ubound(x,2)) = application.index(x,application.transpose(arr),0)
    End sub
    Last edited by daboho; 10-09-2018 at 07:55 PM.

  4. #4
    Registered User
    Join Date
    10-05-2018
    Location
    Athens,Greece
    MS-Off Ver
    Office 365
    Posts
    7

    Re: How to equal two cells if they have different values

    I am not sure if this is what I am looking. Maybe I didn't express it correctly. To make it more clear.
    The cell G2 has the world "marine"
    The cells in column N has one of these words: "marine", "all", "brackish"
    For example N4="marine" , N5="all"
    I want that if the code finds the word "all" to match it with criteria "marine"
    Last edited by alansidman; 10-10-2018 at 12:03 AM.

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,493
    Still not understand what you want,give sample workbook or picture
    Maybe from this coding can you modifay
    sub test()
    Dim lrow as long,x,s$,arr
    Dim k(1 to 4),c2,d2,e2,g2
    
    With sheets("database")
      x = .range("C2",.[N10000].end(3)).value
    End with
    
    With sheets("Result")
      c2= .[c2].value : d2=.[d2].value :e2= .[e2].value :g2 =.[g2].value
     For i = 3 To ubound(x)
       k(1) = x(i,10) >= c2
       k(2) = x(i,11) <= d2
       k(3) = x(i,12) <= e2
       k(4) = x(i,14) = g2
      If k(1) * k(2) * k(3) * k(4) then
           s = iif(s="",i,s & "," & i)
      End if
     Next i
          arr = split(s,",")
         .Range("I12").Offset(1, -8).resize(ubound(arr),ubound(x,2)) = application.index(x,application.transpose(arr),0)
    End with
    End sub
    Last edited by alansidman; 10-10-2018 at 12:04 AM.

  6. #6
    Registered User
    Join Date
    10-05-2018
    Location
    Athens,Greece
    MS-Off Ver
    Office 365
    Posts
    7

    Re: How to equal two cells if they have different values

    Here are images from my workbook
    sheet1(database).PNG
    sheet2(results).PNG

    At sheet1 I have the database and at sheet2 I have the criteria. I want to paste from sheet1 to sheet2 the systems that meet these criteria. I have managed to do it with the numeric data but I have problem with the linguistic data because I want, for example, the world "marine" to be included in the world "all" and when the code finds the world "all" to select the system if the criteria is "marine".
    Last edited by mariosex; 10-10-2018 at 02:06 PM.

+ 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] Macro to check values on two cells should be equal
    By Blokeman in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-04-2014, 09:07 AM
  2. Multiple Cells values - formula to see which cells added together equal zero?
    By rodders0223 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-15-2013, 11:09 AM
  3. [SOLVED] Compare iF cells has equal values then.............
    By Oceans in forum Excel General
    Replies: 9
    Last Post: 11-07-2012, 09:13 AM
  4. [SOLVED] If cells Values are equal..........then
    By Oceans in forum Excel General
    Replies: 2
    Last Post: 11-07-2012, 05:47 AM
  5. [SOLVED] Skip loop if cells equal same values
    By adam2308 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-02-2012, 01:19 PM
  6. Matching cells with equal values
    By LukeBhatia in forum Excel General
    Replies: 4
    Last Post: 09-14-2011, 11:19 AM
  7. checked the equal values from cells
    By kar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2006, 04:48 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