Forum Discussion
Excel highlight numbers in range by input
- Dec 28, 2018
Sorry, I didn't realize you had this on multiple worksheets in the workbook, changed to a workbook change event. So the code goes in ThisWorkbook. Added to what you had posted and attached.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range
Set TicketRange = Range("B4:F23")
Set DrawRange = Range("H4:L23")
For Each Cell In TicketRange
Set CheckRange = Range("H4:L23").Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not CheckRange Is Nothing And Cell.Value <> "" Then
Cell.Interior.ColorIndex = 4
Else
Cell.Interior.ColorIndex = 0
End If
Next Cell
End Sub
Hi, Here are a couple of options, If you want to have the coloring happen only when you manually run the code stick this in a module:
Sub Color()
Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range
Set TicketRange = Range("B4:F23")
Set DrawRange = Range("H4:L23")
For Each Cell In TicketRange
Set CheckRange = Range("H4:L23").Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not CheckRange Is Nothing And Cell.Value <> "" Then
Cell.Interior.ColorIndex = 4
End If
Next Cell
End Sub
Sub UnColor()
Dim TicketRange As Range
Dim Cell As Range
Set TicketRange = Range("B4:F23")
For Each Cell In TicketRange
Cell.Interior.ColorIndex = 0
Next Cell
End Sub
If you want the code to run every time you change a cells value on the worksheet than stick this in the VBA for your worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range
Set TicketRange = Range("B4:F23")
Set DrawRange = Range("H4:L23")
For Each Cell In TicketRange
Set CheckRange = Range("H4:L23").Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not CheckRange Is Nothing And Cell.Value <> "" Then
Cell.Interior.ColorIndex = 4
Else
Cell.Interior.ColorIndex = 0
End If
Next Cell
End Sub
- NaurisLatvia28Dec 28, 2018Copper Contributor
For me is working only that Macro which i need to run manually.
- JWR1138Dec 28, 2018Iron Contributor
Sorry, I didn't realize you had this on multiple worksheets in the workbook, changed to a workbook change event. So the code goes in ThisWorkbook. Added to what you had posted and attached.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim TicketRange As Range
Dim DrawRange As Range
Dim Cell As Range
Dim CheckRange As Range
Set TicketRange = Range("B4:F23")
Set DrawRange = Range("H4:L23")
For Each Cell In TicketRange
Set CheckRange = Range("H4:L23").Find(What:=Cell.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not CheckRange Is Nothing And Cell.Value <> "" Then
Cell.Interior.ColorIndex = 4
Else
Cell.Interior.ColorIndex = 0
End If
Next Cell
End Sub- NaurisLatvia28Dec 28, 2018Copper Contributor
Oh thanks :)
It's working :)
I'll learn from it, I promise :)
But is it possible to make that the count of coloured numbers shows automatically when it's matching number in lottery range from draw range?
Because when i use Your VBA code, it's working good. But when i enter in draw range, i need to P7 - P11 manually and hit enter.
- NaurisLatvia28Dec 28, 2018Copper Contributor
Thanks for trying to help.
But nothing works for me.
Maybe i can send You a copy of my worksheet?