VB6 - Select value from grid and highlight same value in grid 2

Posted on 2013-02-01
Last Modified: 2013-02-02
Hello all

I would like to be able to select a row in my MSHFlexgrid1 and from that specific row, it would search for the value in column 4 into the MSHFlexgrid2 column 1. If there is a perfect match, it would select the MSHFlexgrid2 row and put the column: MSHFlexGrid2.CellBackColor = &H80FF& in the column 1.

if i select MSHFlexgrid1 row 5, in column 4 i have the value: TEST1

If in my MSHFlexgrid2 row 27 column 1 i also have the value: TEST1, it would select that row and add the backcolor in the cell : MSHFlexGrid2.CellBackColor = &H80FF&

How can i do that?

Thanks again for your help

Dim LastMatch
Dim myRow
On Error Resume Next
    For myRow = LastMatch To MSHFlexGrid2.Rows
        If MSHFlexGrid2.TextMatrix(myRow, 1) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 4) Then
            MSHFlexGrid2.TopRow = myRow
            MSHFlexGrid2.Row = myRow
            MSHFlexGrid2.Col = 1
            MSHFlexGrid2.CellBackColor = &H80FF&
            Exit Sub
            MSHFlexGrid2.CellBackColor = &H80000005
        End If
    Next myRow
End If

Open in new window

Question by:Wilder1626
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Accepted Solution

eemit earned 500 total points
ID: 38846463

Private Sub Form_Load()
  MSHFlexGrid1.Row = 0
End Sub

Private Sub MSHFlexGrid1_RowColChange()
  On Error GoTo Err_Handler
  Dim myRow As Long
  Dim sSearch As String
  With MSHFlexGrid1
      sSearch = .TextMatrix(.Row, 4)
  End With
  Call ResetColumnBColor
  With MSHFlexGrid2
      For myRow = 1 To .Rows - 1
          If .TextMatrix(myRow, 1) = sSearch Then
              .TopRow = myRow
              .Row = myRow
              .Col = 1
              .CellBackColor = &H80FF&
          End If
      Next myRow
  End With
  Exit Sub

  Debug.Print "ERROR (MSHFlexGrid1_RowColChange): " & CStr(Err.Number) & ", " & Err.Description

End Sub

Private Sub ResetColumnBColor()
  Dim myRow As Long
  With MSHFlexGrid2
      For myRow = 1 To .Rows - 1
          .Row = myRow
          .Col = 1
          .CellBackColor = &H80000005
      Next myRow
  End With
End Sub

Open in new window

LVL 11

Author Closing Comment

ID: 38846541
Thank you so much

This is perfect

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question