Find And Select Multiple Cells

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
Hello every body

In Range("I2") I have avalue ..
I want to use vba to find this value in the range ("A1:H50") as a whole

I want the code to be in Worksheet_Change so as to when typing a new value in I2 the cells that match this value to be selected
 
Hi YasserKhalil,

Here is another to try.

Regards,
Howard


Put this in the sheet module.

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
'/ by[B] Garry MS Public Prog[/B].

If Intersect(Target, Range("I2")) Is Nothing Then Exit Sub
If IsNumeric(Target.Value) = False Then
MsgBox "Must be a number!"
Range("I2").Select
Exit Sub
End If
  If Target = Range("I2") Then FindMyVal Target.Value
End Sub


Put this in a Standard module.

Code:
Option Explicit

Sub FindMyVal(ValToFind&)
'/ by [B]Garry MS Public Prog.[/B]

Dim n&, k&, sz$, rng As Range

  n = WorksheetFunction.CountIf(ActiveSheet.Range("A1:H50"), ValToFind)
  If n = 0 Then Exit Sub

  With ActiveSheet.Range("A1:H50")
    If n = .Cells.Count Then .Select: Exit Sub

    For n = 1 To .Cells.Count

      If .Cells(n) = ValToFind And rng Is Nothing Then
        Set rng = .Cells(n)
      ElseIf .Cells(n) = ValToFind And Not rng Is Nothing Then
        Set rng = Union(rng, .Cells(n))
      End If
    Next 'n
  End With 'ActiveSheet.Range("A1:H50")
  rng.Select
End Sub
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You're welcome.

Please note that I am not the author of these codes.

See the names under the sub titles.

Regards,
Howard
 
Upvote 0

Forum statistics

Threads
1,223,444
Messages
6,172,171
Members
452,445
Latest member
walkman99

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top