我是 Excel vba 的初学者,我正在编写可以帮助我创建评论并在输入特定供应商 ID 时自动突出显示单元格的代码。我通常手动执行此操作,但发现当有大量数据要输入时,vba 会节省大量时间。
我知道我可以使用 If Or 语句来创建多个条件,但是当有新的供应商 ID 时,我需要返回代码并将新条件添加到 IF 语句中。
因此,我正在考虑创建一个名为“变量”的工作表,我可以将变量保存在其中并向工作表中添加一个新变量。通过使用 IF 语句并从存储在“变量”表中的一系列单元格中调用条件,但它只能将 A2 单元格设置为条件,而没有将 A3、A4、A5 中的其他单元格设置为多个条件。
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
我想我可以使用 For 循环,在变量范围上使用递增 1,以便在每个循环上创建多个条件,但它没有发挥作用。
我一直在搜索整个网站以找到解决方案,有些网站说 .Value 一次只能读取 1 个值,无法读取多个值范围。
是否可以选择工作表中的单元格范围成为IF语句的多个条件?
最佳答案
将供应商名称存储在他们自己的工作表中的想法是正确的。最好的办法是将确定供应商是否需要特殊处理的逻辑封装到它自己的函数中。这将有助于清理您的主要方法,并将所有相关逻辑组合在一起。
下面我举个例子。我创建了一个函数 DoesSupplierRequireComment
,它接受供应商名称,并根据供应商是否在工作表 Variables
的 A 列中返回 True/False
。然后,您可以简单地从您的 main 方法中调用该函数,并根据需要进行操作。 (在下面的示例中,我从 Sub Test
调用函数并仅显示一个消息框)。看看您是否可以将逻辑调整到您的代码中,并跟进任何问题。
Function DoesSupplierRequireComment(supplierName As String) As Boolean
Dim isRequired As Boolean
Dim searchRange As Range
Dim lastRow As Long
'Initialize variable to false
isRequired = False
With ThisWorkbook.Sheets("Variables")
'Determine the range that stores the variables.
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set searchRange = .Range("A1:A" & lastRow)
'Check if the passed in argument is found in the variables sheet
If Not searchRange.Find(supplierName) Is Nothing Then
isRequired = True
End If
End With
DoesSupplierRequireComment = isRequired
End Function
Sub Test()
If DoesSupplierRequireComment("LOL") Then
MsgBox "Requires special treatment"
Else
MsgBox "Does Not"
End If
End Sub
关于excel - 如何在 Excel VBA 中将一系列单元格设置为 If 语句的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64285783/