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
Related
I'm working on a excel vba code to import and manipulate some data from CSV-file. Suddenly a part of my code didn't work any more though it had worked without problems before.
It is about range.select and afterward with selection.Interior.Pattern = xlSolid
I have tried to copy the same small part of the code to a different workbook and here it work just perfect.
Dim iPhase As Integer
iPhase = Application.WorksheetFunction.CountIf(Range("A:A"), "Phase")
Dim h As Integer
h = 1
Range("A6").Select
Do Until h > iPhase
Cells.Find(What:="Phase", after:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveSheet.Range(ActiveCell, ActiveCell.Offset(0, 16)).Select
With selection.Interior
.Pattern = xlSolid
.Interior.PatternColorIndex = xlAutomatic
.Interior.ThemeColor = xlThemeColorAccent6
.Interior.TintAndShade = 0
.Interior.PatternTintAndShade = 0
End With
With selection.Font
.Bold = True
End With
h = h + 1
Loop
I get a compile error: Expected function or variable #"selection.interior"
The comments already identify the issues with your code; but here is an alternative using Filter and SpecialCells to select the visible data. Comments are contained in the code.
Sub FliterWithConditionalFormatting()
Dim rng As Range
'properly defing and reference your workbook and worksheet, change as requiried
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1").CurrentRegion
'The WITH..END WITH statement allows you to shorten your code and avoid using SELECT and ACTIVATE
With rng
.AutoFilter Field:=1, Criteria1:="Phase", Operator:=xlAnd 'filter the rng
'set the range, to conditionally format only the visible data, skipping the header row
With .Range(Cells(2, 1), Cells(rng.Rows.Count, 17)).SpecialCells(xlCellTypeVisible)
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = xlThemeColorAccent6
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With .Font
.Bold = True
End With
End With
.AutoFilter 'Remove the filter
End With
End Sub
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
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
When running this code I get a error that is "loop without do". I want if "case vbno" is selected then it returns to the original input box. If it the user selects "case vbyes" I want it to highlight then cell then loop back to do and return to original input box. If cancel is selected I want it to exit completely.
Sub find_highlight3()
Dim w As Variant
Dim FoundCell As Range
Dim ans As String
Do
w = InputBox("What to find?")
Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
With Selection.Interior
Select Case MsgBox("Hellow", vbYesNoCancel)
Case vbNo
Loop
Case vbYes
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
Loop
Case vbCancel
End Select
End With
End Sub
The following code should do what you want, while still maintaining the integrity of each "block" of code.
Sub find_highlight3()
Dim w As Variant
Dim FoundCell As Range
Dim ans As String
Do
w = InputBox("What to find?")
Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Select Case MsgBox("Hellow", vbYesNoCancel)
Case vbNo
Case vbYes
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Case vbCancel
Exit Do
End Select
Loop
End Sub
Note: Your Activate statement will fail if the Find does not match anything (because Nothing.Activate is invalid), but that is a question for another day.
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