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
LVL 11
Wilder1626Asked:
Who is Participating?
 
Brook BraswellConnect With a Mentor Application Development ManagerCommented:
Wilder,
   In your code here the problem is that if you Do find a match... then you "want" to keep going but if you do not you want to mark it.
  Your code only falls out of the Loop when NO match is found...
  I propose you change so that you do not mark until you either Find a match or you finish looping through all to see that you do not find a match.
Dim r1 As Long, r2 As Long, i As Long
Dim Text1 As String
Dim Text2 as String
Dim bFound as boolean
For r1 = 1 To MSHFlexGrid1.Rows - 1
   bFound = false 
   For r2 = 1 To MSHFlexGrid2.Rows - 1
      Text1 = UCase(Trim(MSHFlexGrid1.TextMatrix(r1, 5)))
      Text2 = UCase(Trim(MSHFlexGrid2.TextMatrix(r2, 10)))
      If Val(Text1) = Val(Text2) Then
         bFound = False
         Exit For
      else
         bFound = True
      End If
   Next r2
   if bFound then 
      MSHFlexGrid1.TextMatrix(r1, 14) = "NEEDS TO BE REMOVED"
      MSHFlexGrid1.Row = r1
      MSHFlexGrid1.Col = 14
      MSHFlexGrid1.CellBackColor = &H80FF&
   Else
      MSHFlexGrid1.TextMatrix(r1, 14) = ""
   End If
Next r1

Open in new window

0
 
Brook BraswellApplication Development ManagerCommented:
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

0
 
Brook BraswellApplication Development ManagerCommented:
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

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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

0
 
Brook BraswellApplication Development ManagerCommented:
AHH..
Reverse it back and leave the ucase(trim( in there...
0
 
VBClassicGuyCommented:
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?
0
 
Wilder1626Author Commented:
Hello VBClassicGuy

You touch a point here.

Yes, we are talking about numbers.
0
 
VBClassicGuyConnect With a Mentor Commented:
Then you need to replace this line:

If UCASE(TRIM(MSHFlexGrid2.TextMatrix(r2, 10))) <> test1 Then

with:

If Val(MSHFlexGrid2.TextMatrix(r2, 10)) <> Val(test1) Then
0
 
VBClassicGuyCommented:
Or, use "=" instead of "<>", since it is unclear what you want the result to be. Use what works.
0
 
Wilder1626Author Commented:
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"
0
 
Wilder1626Author Commented:
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
0
 
Wilder1626Author Commented:
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.
0
 
Wilder1626Author Commented:
Thanks to all of you.

Now it works perfectly well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.