Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

search in 2 MSHflexgrid

Hello all,

I have an issue with my code. Not that it give me an error but the result wanted is not there.

I need to ask, for every row of grid 1 to find the value in column 5 in the grid2 in column10.

if it exist, it clear the column 14 from grid one of the same row as the result.

If it does not exist, then it put: MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"


Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1


   test1 = MSHFlexGrid1.TextMatrix(r1, 5)
   
   For r2 = 1 To MSHFlexGrid2.Rows - 1
      If MSHFlexGrid2.TextMatrix(r2, 10) = test1 Then
            MSHFlexGrid1.TextMatrix(r1, 14) = ""
            Else
               MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
               MSHFlexGrid1.Row = r1
               MSHFlexGrid1.Col = 14
               MSHFlexGrid1.CellBackColor = &H80FF&
               Exit For
           
      End If
   Next r2
Next r1

Open in new window


Now, it always put "NEEDS TO BE REMOVED" even if the result exist.

Thanks for your help
Avatar of Brook Braswell
Brook Braswell
Flag of United States of America image

Wilder,
  I thought your point was to put Removed when the value was found...

if that is the case then your if is backwards...

If MSHFlexGrid2.TextMatrix(r2, 10) <> test1 Then
   MSHFlexGrid1.TextMatrix(r1, 14) = ""
Else
   MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
   MSHFlexGrid1.Row = r1
   MSHFlexGrid1.Col = 14
   MSHFlexGrid1.CellBackColor = &H80FF&
Exit For
           
      End If

Open in new window

also...bear in mind that vb6 is case sensitive when comparing values...
put a UCASE(TRIM()) around your values when comparing...

Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1
   test1 = UCASE(TRIM(MSHFlexGrid1.TextMatrix(r1, 5)))
   If UCASE(TRIM(MSHFlexGrid2.TextMatrix(r2, 10))) <> test1 Then
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
      Exit For
   End If
   Next r2
Next r1

Open in new window

Avatar of Wilder1626

ASKER

The problem now is that it never put remove even if the text in cell 5 is not in grid 2.

AHH..
Reverse it back and leave the ucase(trim( in there...
Brook's last answer should work, unless your question is asked correctly. If so, just replace the "<>" in his code with "=", and it will work as your new question states.

*** UNLESS ***

We are talking numbers here, which is something we all neglected to ask. Sometimes, programmers will put numerical values in a FlexGrid's cells, and if they don't match character-for-character, you will not get a match. In other words, if you want a cell that contains "23.00" to match a cell that reads "23", there is more work to do. I hope I explained that well enough.

Are we dealing with numbers here?
Hello VBClassicGuy

You touch a point here.

Yes, we are talking about numbers.
SOLUTION
Avatar of VBClassicGuy
VBClassicGuy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or, use "=" instead of "<>", since it is unclear what you want the result to be. Use what works.
I did a test with only 1 line of data

Private Sub Command8_Click()
Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1
For r2 = 1 To MSHFlexGrid2.Rows - 1
   test1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text2 = UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10)))
   If UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 11))) = test1 Then
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
      
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
      Exit For
      Exit For
   End If
   Next r2
Next r1
End Sub

Open in new window


Text1 = 040155031
Text2 = 040155031

Still give me "NEEDS TO BE REMOVED"
Oh oh oh

Now i have something with
Dim r1 As Long, r2 As Long, i As Long
Dim test1 As String
For r1 = 1 To MSHFlexGrid1.Rows - 1
For r2 = 1 To MSHFlexGrid2.Rows - 1
   test1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
   Text2 = UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10)))
   If UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10))) = test1 Then
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
      
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
      Exit For
      Exit For
   End If
   Next r2

Open in new window


That look's good for now.

I will give another try
I think i know the problem but i don't know how to fix it.

The problem is that if for example, i have only 1 row in each grid, with the same value, it will work.

If i also have only 1 row per grid but both are different, then it will also put the message REMOVED.

Now the problem is that, if again first row in grid = first row in grid 2 but in grid 2, there is other row with value that don't match the first row in grid 1, then it will add "REMOVE" message.

So it keep's searching even it there was a perfect match.

How can we fix this in the code?

Thanks for your help.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to all of you.

Now it works perfectly well.