在这段代码中,我试图让用户连续选择一个范围。如果该行包含“ HOL”,则消息框显示将显示一条消息。
当用户选择一个包含“ HOL”的单元格时,代码现在的方式是:当用户连续选择多个单元格时,将出现此消息,并出现错误运行时错误13。这是我遇到问题的if语句
我尝试了不同的范围选择方法,但是我对编码还不够熟悉,无法理解我的错误。
' Highlight_SKL Macro
' This macro will highlight leave dates for entry
Dim rng As Range
Set rng = Range(Selection.Address)
If MsgBox("Are you sure you want to submit day of SKL", vbYesNo) = vbNo Then Exit Sub
If InStr(Range(Selection.Address), "HOL") Then MsgBox ("You are entering a SKL date on a Federal Holiday")
With Selection.Interior
rng = "=1"
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 250
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
当用户选择包含“ HOL”的行时,会出现一个消息框,让他们知道。
答案 0 :(得分:0)
使用通配符MATCH来选择一个或多个单元格。
If Not IsError(application.match("*HOL*", Selection, 0)) Then _
MsgBox "You are entering a SKL date on a Federal Holiday"
答案 1 :(得分:0)
下面的代码用于每个循环,以循环选择的每个单元,以避免错误。
Option Explicit
Sub test()
Dim rng As Range, cell As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range(Selection.Address) '<- Change sheet name if need
If MsgBox("Are you sure you want to submit day of SKL", vbYesNo) = vbNo Then Exit Sub
For Each cell In rng
If InStr(cell, "HOL") Then MsgBox ("You are entering a SKL date on a Federal Holiday")
With cell
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 250
.TintAndShade = 0
.PatternTintAndShade = 0
End With
.Value = 1
End With
Next cell
End Sub
答案 2 :(得分:0)
如果要使用类似的方法进行检查:
If Not Range(Selection.Address).Find(What:="HOL", LookAt:=xlWhole) Is Nothing Then
MsgBox ("You are entering a SKL date on a Federal Holiday")
Else
'Your code
End If