[英]How to use named ranged as reference for validation
Hoping I can get help here, I am currently using the Dim Long in my VBA code but since I am referring to multiple columns the code became quite long.希望我能在这里得到帮助,我目前在我的 VBA 代码中使用 Dim Long,但由于我指的是多列,代码变得很长。 Now, I wanted to try named range reference instead but i cannot make it work.现在,我想尝试命名范围引用,但我无法使其工作。 This is my current code:这是我当前的代码:
Dim i As Long
For i = 8 To 500
'if details is incomplete
If Range("AA" & i).Value > 0 Then
If Range("AB" & i).Value = "Error" Or Range("AC" & i).Value = "Error" Or Range("AD" & i).Value = "Error" _
Or Range("AE" & i).Value = "Error" Or Range("AF" & i).Value = "Error" Or Range("AG" & i).Value = "Error" _
Or Range("AH" & i).Value = "Error" Or Range("AI" & i).Value = "Error" Or Range("AJ" & i).Value = "Error" _
Or Range("AK" & i).Value = "Error" Or Range("AL" & i).Value = "Error" Or Range("AM" & i).Value = "Error" _
Or Range("AN" & i).Value = "Error" Or Range("AO" & i).Value = "Error" Or Range("AP" & i).Value = "Error" _
Or Range("AQ" & i).Value = "Error" Or Range("AR" & i).Value = "Error" Or Range("AS" & i).Value = "Error" _
Or Range("AT" & i).Value = "Error" Or Range("AU" & i).Value = "Error" Or Range("AV" & i).Value = "Error" _
Or Range("AW" & i).Value = "Error" Or Range("AX" & i).Value = "Error" Or Range("AY" & i).Value = "Error" Then
MsgBox "One of the mandatory field is not provided, please check all cells highlighted in yellow & make sure details is provided."
End If
Endif
I named range AA = "Validation" & range AB:AY = "Details" how can i declare it and use named range instead of writing each columns one by one?我命名范围 AA = "Validation" & range AB:AY = "Details" 我如何声明它并使用命名范围而不是一一写入每一列?
As @Ike suggests - use the COUNTIF formula.正如@Ike 所建议的那样 - 使用 COUNTIF 公式。 Can be used on the worksheet or within VBA.可用于工作表或 VBA 中。 If you want to return the addresses of each error then Find
might be a better route.如果您想返回每个错误的地址,那么Find
可能是更好的路由。
Sub Test()
Dim Result As Long
Result = Errors(Sheet1.Range("AB8:AY500"))
If Result > 0 Then
MsgBox "There are " & Result & " errors in the range."
End If
End Sub
Public Function Errors(Target As Range) As Long
Errors = WorksheetFunction.CountIf(Target, "Error")
End Function
Conditional formatting can handle this.条件格式可以解决这个问题。 I have demonstrated for a smaller range.我已经证明了一个较小的范围。 Feel free to apply it for your required range.随意将其应用于您所需的范围。
NON VBA非 VBA
Formula used: =AND($AA8>0,AB8="Error")
使用的公式: =AND($AA8>0,AB8="Error")
VBA VBA
You can use conditonal formatting in VBA as well.您也可以在 VBA 中使用条件格式。
Is this what you are trying?这是你正在尝试的吗? I have commented the code.我已经评论了代码。 So let me know if you have any questions.如果您有任何问题,请告诉我。
Option Explicit
Sub Sample()
Dim i As Long
Dim ws As Worksheet
Dim CondTrue As Boolean
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Check if there is even one cell which satisfies our condition
For i = 8 To 500
If .Evaluate("=AND(AA" & i & ">0,COUNTIF(AB" & i & ":AY" & i & ",""Error"")>0)") = True Then
CondTrue = True
Exit For
End If
Next i
'~~> If found then apply conditional formatting
If CondTrue Then
With .Range("AB8:AY500")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=AND($AA8>0,AB8=""Error"")"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
'~~> Show message box
MsgBox "One of the mandatory field is not provided, please check all cells highlighted in yellow & make sure details is provided."
Else
MsgBox "All Good!"
End If
End With
End Sub
IN ACTION (VBA)在行动(VBA)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.