I'm a beginner in Excel vba and I'm working on code that can help me to create a comment and highlight the cell automatically when a certain supplier id is entered. I normally do it manually but found that a vba will save a lot of time when there are loads of data to be entered.
I know that I can do it with If Or statement to create multiple conditions, but when there's new supplier Id, I need to go back to code and add a new condition into the IF Statement.
So, I'm thinking to create a sheet called "Variables" and I can save the variables inside and add a new variable into the sheet. By using the IF statement and recall the conditions from a range of cells that are stored in "Variable" sheet, but it just able to set the A2 cell as the condition and didn't set the others' in A3, A4, A5 as multiple conditions.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xCell As Range, Rg As Range
Dim Supplier As Range, Rg1 As Range
Dim Vendor_id As Range, Rg2 As Range
Dim i As Long, startRow As Long
Dim LastRow As Long
startRow = 2
LastRow = 10
On Error Resume Next
Set Rg1 = Application.Intersect(Target, Range("C2:C65536")) 'Set Range for Supplier
Set Rg2 = Application.Intersect(Target, ThisWorkbook.Sheets("Variables").Range("A2:A4")) 'Set Ranges for supplier in Variables sheet as condition
Set Rg3 = Application.Intersect(Target, Range("B2:B65536")) 'Range for Alert Window
If Not Rg1 Is Nothing Then
For Each Supplier In Rg1
For i = startRow To LastRow
If Supplier.Value = ThisWorkbook.Sheets("Variables").Range("A" & i) Then
Supplier.Interior.Color = 24567 'Automatic fill in the cells with orange colour if Supplier id is met the criteria
ActiveCell.Offset(RowOffset:=-1, ColumnOffset:=2).Activate 'Select and activate the cell of the desired column on the respective row
'Insert the file path Hyperlink into the selected cell and change the font type of the Hyperlink
With ActiveSheet.Hyperlinks.Add(Range(ActiveCell.Address), Address:="https://www.automateexcel.com/excel/", TextToDisplay:="Checking needed")
.Range.Font.Bold = True
.Range.Font.Underline = False
.Range.Font.Color = vbMagenta
End With
Else
Supplier.Interior.Color = 16777215 'Clear the Colour in the cells if the value is nothing
End If
Exit Sub
Next i
Exit Sub
Next Supplier
End If
If Not Rg3 Is Nothing Then
For Each xCell In Rg3 'Create a dialog to ask Check for certain parts
If xCell.Value = "Cardboard" Or xCell.Value = "Toolkit" Then
Dim int2 As Integer
int2 = MsgBox("Please send an email to notify", vbOKOnly + vbExclamation, "Quality Alert")
xCell.Interior.Color = 2552550 'Highlight the cells with Yellow Colour
Else
xCell.Interior.Color = 16777215 'Clear the Colour in the cellsFOdfdfsdsdfsd if the value is nothing
Exit Sub
End If
Next
End If
End Sub
I thought I could use the For loop using increment by 1 on the Variables Range in order to create multiple conditions on every loop, but it didn't play out.
I've been searching through the whole website to find a solution and some site says that .Value just only able to read 1 value at a time and not possible to read multiple range of values.
Is it possible to select a range of cells in the worksheet to become the multiple conditions for IF statement?