Highlight after searching multiple tables - excel

This question is long.
I have SKUs in 12 tables in say sheet1, and the table header is the material required for that product.
I have a list of materials required on a seperate sheet (Sheet2).
I have created a dropdown for the SKUs in Sheet2 which highlights the material required for that SKU.
A single SKU can occur in multiple tables.
I have taken this approach because it was easier to update the SKUs in the tables than the materials required.
I have already made a code for highlighting the material required, and this code already runs whenever I select SKU from the drop down.
I would like the following code to search in multiple tables for the same SKU highlight all the materials required for that SKU.
Can this be done?
Any suggestion and help is greatly appreciated as I have searched a lot for something similar.
Below is the code for highlight:
Sub Hilight(RNG As range, Hilight As Boolean)
With RNG.Interior
If Hilight Then
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(100, 250, 150)
.TintAndShade = 0
.PatternTintAndShade = 0
Else
.Pattern = xlNone
.PatternTintAndShade = 0
End If
End With
End Sub
And here is How I have been using this Sub
Dim L8Product As String
Dim L11Product As String
Dim L12Product As String
Dim L10Product As String
Dim i As Long
Dim L8Rnge As range
Dim L11Rnge As range
Dim L12Rnge As range
Dim L10Rnge As range
L11Product = range("Line11_P")
L12Product = range("Line12_P")
L10Product = range("Line10_P")
L8Product = range("Line8_P")
If Not Intersect(Target, Me.range("Line8_P, Line11_P, Line12_P, Line10_P")) Is Nothing Then
Hilight range("Line8_Hilight_Mon, Line8_Prep_Mon, Line11_Hilight_Mon, Line12_Hilight_Mon, Line10_Hilight_Mon"), False
'Below Code searches in the Table and then highlights the appropriate cells
If Trim(L8Product) <> "" Then
With Sheets("Products").range("KP_Table") 'searchs in the KP Table on Sheet Overtime_Pos_Table
'The code below will search the KP table for the product that you will select from the Line 8 drop down
Set L8Rnge = .Find(what:=L8Product, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not L8Rnge Is Nothing Then
Hilight range("KP_Hilight_Mon, Line8_Prep_Mon"), True 'Hilights the cells for the KP and the Prep material required
Else: With Sheets("Products").range("Osgood_Table")
Set L8Rnge = .Find(what:=L8Product, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not L8Rnge Is Nothing Then
Hilight range("Osgood_Hilight_Mon, Line8_Prep_Mon"), True 'Hilights the cells for the Osgood and the Prep material required
End If
End With
End If
End With
Else: Hilight range("Line8_Hilight_Mon, Line8_Prep_Mon"), False
End If
End If

Related

Only select value according to highlighted cells

I am trying a code which would select a value from a Data Validation Dropdown list only if the cell, which is two blocks to its left, is highlighted. But I am not able to figure out how to do this. Can anyone help please? Thanks
I have the following code which is wrong but just as an example:
Sub AssignBided()
Worksheets("Monday").Select
With Worksheets("Monday")
If Hilight.range("B12") = True Then
range("B12").Activate
ActiveCell.Offset(0, -2).Select
.Selection.Value = "ABC"
End If
End With
End Sub
The code to highlight cells is as follows:
Sub Hilight(RNG As range, Hilight As Boolean)
With RNG.Interior
If Hilight Then
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(100, 250, 150)
.TintAndShade = 0
.PatternTintAndShade = 0
Else
.Pattern = xlNone
.PatternTintAndShade = 0
End If
End With
End Sub
The hilight Sub is used as follows:
Dim L8Product As String
Dim i As Long
Dim L8Rnge As range
L8Product = range("Line8_P_Mon")
'Line 8 Resource requirement code
'Determine if change was made in cell B39
If Not Intersect(Target, Me.range("Line8_P_Mon")) Is Nothing Then
Hilight range("Line8_Hilight_Mon"), False
'Below Code searches in the KP and Osgood Table and then highlights the
appropriate cells
If Trim(L8Product) <> "" Then
With Sheets("Products").range("KP_Table")
'searchs in the KP Table on Sheet Overtime_Pos_Table
'The code below will search the KP table for the product that you will select from the Line 8 drop down
Set L8Rnge = .Find(what:=L8Product, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not L8Rnge Is Nothing Then
Hilight range("KP_Hilight_Mon"), True
'Hilights the cells for the KP and the Prep material required
Else: With Sheets("Products").range("Osgood_Table")
Set L8Rnge = .Find(what:=L8Product, _
after:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=False)
If Not L8Rnge Is Nothing Then
Hilight range("Osgood_Hilight_Mon"), True
'Hilights the cells for the Osgood and the Prep material required
End If
End With
End If
End With
Else: Hilight range("Line8_Hilight_Mon"), False
End If
End If
Hope the question is clear. Thank you in advance.
You can create a simple function to check for highlighting...
'use a constant to store the highlight color, since it's used
' in multiple places in your code
Const HIGHLIGHT_COLOR = 9894500 'RGB(100, 250, 150)
Sub AssignBided()
With Worksheets("Monday")
If IsHighlighted(.Range("B12")) Then
.Range("B12").Offset(0, 2).Value = "ABC" 'changed offset from -2...
End If
End With
End Sub
'Is a cell highlighted? EDIT: changed the function name to IsHighlighted
Function IsHighlighted(c As Range)
IsHighlighted = (c.Interior.Color = HIGHLIGHT_COLOR)
End Function
Sub Hilight(RNG As Range, Hilight As Boolean)
With RNG.Interior
If Hilight Then
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = HIGHLIGHT_COLOR '<< use contant here
.TintAndShade = 0
.PatternTintAndShade = 0
Else
.Pattern = xlNone
.PatternTintAndShade = 0
End If
End With
End Sub

Excel VBA Looping Fill for certain cell values

I'm doing an assignment in VBA. I recorded a macro that found cells with the search criteria 'central', then I colored it blue-green and got the following Macro:
Sub Color()
' Color Macro
' Color a region
'
' Keyboard Shortcut: Ctrl+m
'
Cells.Find(What:="central", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6723891
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End sub
There are 23 occurances of the word 'central' so I thought I could add for k=1 to 23 above the line that starts with cells.find(what...), then add Next K above end with but when I try I get the error
next without for
Bruce Wayne already told you why you got that error
but if you want your macro to find and process all occurrences of "central" in your currently active sheet no matter how many of them, then you can wrap Find() method inside a loop that goes on until all wanted occurrences are found, like follows (explanation in comments):
Dim f As Range
Dim firstAddress As String
With ActiveSheet.UsedRange 'reference currently active sheet used range
Set f = .Find(What:="central", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'search referenced range for first occurrence of "central"
If Not f Is Nothing Then ' if found...
firstAddress = f.Address ' store first occurrence cell
Do
With f.Interior 'reference found cell "Interior" property
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6723891
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Set f = .FindNext(f) ' search for the next "central" occurrence
Loop While f.Address <> firstAddress ' loop till you wrap back to initial occurrence
End If
End With

Range.find method to find cells with specific colorindex

I want to find cells with the same colorindex with a range
So I recorded a macro to find cells with a color
and applied to its code like this.
But it doesn't work.
How can I find cells with a specific colorindex?
For Each r In rngC
lcolorID = r.Offset(, 1).Interior.ColorIndex
With wsD.UsedRange
With Application.FindFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = lcolorID
End With
Set c = .Find(What:="", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:= xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, MatchByte:=False, _
SearchFormat:=True)
If Not c Is Nothing Then
Debug.Print c.Address
End If
End With
Next r
If Cell.Interior.ColorIndex = yourColorIndex Then 'using your color variable
'your actions here
End If
Place the above if condition in a for loop where you go through all the rows and it should work! And replace "Cell" by the specific cell you are currently handling in the project using the loop you are using to go through your sheet. If your sheet name is "Sheet" then use:
Sheets("Sheet").Cell(rowindex, columnindex).Interior.ColorIndex
Where rowindex and columnindex are the indexes your are looping through.
Try Application.FindFormat.Clear before With Application.FindFormat.Interior.
You may have some formatting already set which may prevent you in matching desired cells.

How to Highlight rows containing particular keyword in VBA?

I'm working with MS Excel 2010 VBA.i want to write a code for highlighting the rows which contains the keyword canada i have written the following code,
Cells.Find(What:="Canada", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Select
With Selection.Font
.Color = -16776961
.TintAndShade = 0
End With
i want to use the code in a loop to highlight all the rows which contains the keyword "canada".How can i do it using VBA?
Example:
Sub Highlight()
Dim vCell As Range
'Loop through every used cell in the active worksheet
For Each vCell In ActiveSheet.UsedRange
'If the cells value contains "Canada" change its font to red
If InStr(vCell.Value, "Canada") Then
vCell.Font.Color = -16776961
End If
Next
End Sub
Here is a link to microsofts office site explaining how loops work:
http://office.microsoft.com/en-us/training/get-in-the-loop-with-excel-macros-RZ001150634.aspx?section=11

Find and format excel macro

I need a way in excel to go through my entire workbook and find all the words with a matching case and add them into italics.
I have cells with data like so:
Percentage of CTE concentrators who have met the proficient or advanced level on the
statewide high school mathematics assessment administered by the state under ESEA and
who, in the reporting year, left secondary education.
I need to change all the "ESEA" into italics.
Is there a way to do this in excel or do I need a macro?
Here's code that will do this for you:
Sub Macro1()
Dim sFirstAddress As String, rgFound As Range
Const sSearch As String = "ESEA"
Set rgFound = Cells(1, 1)
Do While Not rgFound Is Nothing
Set rgFound = Cells.Find(What:=sSearch, After:=rgFound, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, _
SearchFormat:=False)
If rgFound.Address = sFirstAddress Then Exit Do
If InStr(rgFound.Value, sSearch) > 0 Then
If Len(sFirstAddress) = 0 Then sFirstAddress = rgFound.Address
rgFound.Characters(InStr(rgFound.Value, sSearch), Len(sSearch)).Font.FontStyle = "Italic"
End If
Loop
End Sub

Resources