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
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
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
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
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.
Is there a way that I can call a block of code and get the value that I am looking for and store it as the variable I want and then use it in a different sub?
For example. I want to search all the values in row 1 starting at A1 and going till the there is a blank value. I want to find the cell with the value "Frequency" in it and then return the column index number.
Sub findFrequency()
'find "Frequency" colum and save the column index number
Dim fFreq As String
Dim FreqCol As Variant
Range("A1").Select
fFreq = "Frequency"
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = fFreq Then
FreqCol = ActiveCell.Column
Exit Do
End If
ActiveCell.Offset(0, 1).Select
Loop
End Sub
Now ideally I can write a different sub and use the value from the above code.
Sub Execute()
Call findFrequency
Cells(5, FreqCol).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub
I get an error when I run this because the value of FreqCol is not set to anything from running the Call findFrequency line.
Try this:
Function findFrequency()
'find "Frequency" colum and save the column index number
Dim fFreq As String
Dim FreqCol As Variant
Range("A1").Select
fFreq = "Frequency"
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = fFreq Then
findFrequency = ActiveCell.Column
Exit Do
End If
ActiveCell.Offset(0, 1).Select
Loop
End Function
Sub Execute()
Dim FreqCol As Long
FreqCol = findFrequency()
Cells(5, FreqCol).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub
Why loop or select a cell to find a value? Simply use .Find
Function findFrequency() As Long
Dim ws As Worksheet
Dim aCell As Range
Set ws = ActiveSheet
With ws
Set aCell = .Columns(1).Find(What:="Frequency", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then findFrequency = aCell.Column
End With
End Function
Also what should happen if the "Frequency" is not found. You need to cater to that as well.
Sub Execute()
Dim FreqCol As Long
FreqCol = findFrequency
If FreqCol = 0 Then
MsgBox "Frequency not found"
Exit Sub
End If
With Cells(5, FreqCol).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub
The code would to be like this.
Main Main sub is findFrequency, subprocedure is Sub Execute(rng As Range)
Sub findFrequency()
'find "Frequency" colum and save the column index number
Dim fFreq As String
Dim FreqCol As Variant
Range("A1").Select
fFreq = "Frequency"
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = fFreq Then
FreqCol = ActiveCell.Column
Execute Cells(5, FreqCol)
Exit Do
End If
ActiveCell.Offset(0, 1).Select
Loop
End Sub
Sub Execute(rng As Range)
With rng.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End Sub
let me tell you that you should not apologize for asking a basic question about VBA: nobody deserves to spend time with VBA and I'm glad you are more acquainted with other programming languages.
I write my answer for all people not familiar with VBA.
VBA passes arguments by default by reference so you can write your code in this way:
Sub findFrequency(FreqCol as Long)
...
End SUb
Sub Execute()
Dim FreqCol As Long
findFrequency FreqCol
...
End SUb