- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个脚本,它查看整个列并查找 1 - 9 之间的值,如果它遇到一个数字,它会抛出一个消息框,如果没有,它当前会抛出 10 个消息框,我知道这是因为第二个框仍然包含循环。
我曾尝试将其置于循环之外,但没有成功,任何指针都可以很好地让 Else: MsgBox "All locations correctly entered"显示一次!
Sub Scoring()
Dim FindString As String
Dim rng As Range
Dim startVal As Integer, endVal As Integer
startVal = 1
endVal = 9
For i = startVal To endVal
FindString = CStr(i)
With Sheets("Scoring").Range("S:S")
Set rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
MsgBox "There are one or more risks that do not contain the minimum information required for import, please ammend these and try again.", True
Exit For
Else: MsgBox "All locations correctly entered"
End If
End With
Next i
End Sub
最佳答案
您可以引入一个存储true 或false 的 bool 类型变量。默认情况下,任何 bool 变量都是 false,因此 found
默认等于 false(您没有明确说明 found = false
但它是可选的)。所以,当rng
不是空的时候,你只需要将它的值改为true。在退出循环之前添加了 found = true
。
合乎逻辑的是,除非某件事是真的,否则它总是假的。因此,当值匹配时,您会切换变量状态。
在宏代码的底部有一行检查 found
是否为 false。如果是,则将显示一个消息框,而不是 10 个以上。
希望对你有帮助
Sub Scoring()
Dim FindString As String
Dim rng As Range
Dim startVal As Integer, endVal As Integer, i As Long
startVal = 1
endVal = 9
Dim found As Boolean
For i = startVal To endVal
FindString = CStr(i)
With Sheets("Scoring").Range("S:S")
Set rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
MsgBox "There are one or more risks that do not contain the minimum information required for import, please ammend these and try again.", True
found = True
Exit For
End If
End With
Next i
If Not found Then MsgBox "All locations correctly entered"
End Sub
关于vba - MsgBox出现多次...重新排列宏所以只显示一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19049526/
我正在尝试编写 VBScript,并且正在使用 Randomize 和 MsgBox 等函数。我很好奇使用 () 和不使用它们有什么区别。例如: 随机化 - 此行有效。 Randomize() - 此
哪种方法更好以及为什么。 最佳答案 不要使用是/否问题,而是使用带有自定义按钮的问题: The file blah.txt has been modified. Would you like to s
我想要一个工作表更改宏,只要将大于 8 的值放入范围 (F14:J26) 中的单元格之一,并且将大于 300 的值放入单元格 C37 中,该宏就会弹出一个消息框。 我的问题是单元格 C37 不是手动填
我有一个 VBA 脚本,内容如下: 1,选择特定工作表时将运行 2、判断条件是否为真 3、如果有,显示MsgBox Private Sub Worksheet_Activate() Dim curre
我有一组可以删除的形状。 但是,如果其中至少一个被删除,那么我会收到一个错误,即找不到指定名称下的对象。 基本上我想发表评论,告诉我所有的形状都已经被删除了。 到目前为止,我的代码如下所示: Sub
在我的 VBA 项目中,我偶尔会弹出 MsgBox 以通知用户在子例程运行后某些内容已“完成”或“更新”。 如果没有 MsgBox,它似乎运行良好,但插入一个似乎给我一个错误。 不确定是否有必要在此处
当我在他的 post about using SQL in Excel 中看到这个时,我正在查看 Chandoo 优秀网站上的一个代码示例。 : MsgBox "I was not able to f
我有一个显示在 msgBox 中的数组通过 Join()功能。如果我使用 Join,我想知道如何删除出现的尾随逗号在 resultsFinal大批。 Function test2(Var As Ran
是否可以使此代码的 msgbox 仅出现一次?我的问题是,如果用户插入数据,即从第 501 行到第 510 行,消息框将出现 9 次,我只想拥有一次。这样做的原因是因为代码在每个单元格中查找以验证是否
我有一个有趣的问题 - 我正在尝试编写 VBA 代码,它将获取单元格的内容并将其放入 msgbox。问题是我不知道如何将回车符放入这个字符串中。 我的代码看起来像这样。 Dim myMsg as st
有宏观问题。 VBA初学者。有以下情况: Column D Column E 3 2 我只是想确保用户没有在 D 列中输入任何超过 E 列的内容。如果是这样,则会弹出一个消息
我正在编写一个宏,它比较两组值并警告用户是否存在任何差异。当发现差异时,我希望用户输入他们的评论。为了让他们更容易理解我在问什么,我想在我用来获取评论的 InputBox 中显示所有数字。我想获得的示
我想知道是否有人可以帮助我将下面的内容变成一个消息框。基本上我希望它首先检查以下范围是否有 查看 如果他们这样做,则会出现一个 msgbox,解释违反了哪条规则。我曾尝试创建变量,但不确定如何在 ms
我有以下设置来复制列表并粘贴到工作表(数据)。我希望它在成功时显示一条消息,告诉我它是从哪一行开始粘贴的。但是,errmsg 反而显示。 提前致谢 Dim current As String
我正在尝试编写一个使用 的 vbscript谷歌演讲 到消息的发音。 我必须使用 将代码保存到 Notepad++ 无 BOM 的 UTF8 编码 并且发音不错,但是消息框的显示对重音字符不好。 如何
我正在尝试创建一个代码,如果 G 列单元格中的内容不等于 0,则显示 MsgBox。代码仅适用于一个单元格,但不适用于整个范围 (G20:G100)。你能帮忙吗?谢谢 Private Sub Work
我的数据如下。 更新问题 Sub Solution() Dim shData As Worksheet Set shData = Sheets("Sheet1") 'or other r
我希望每次单元格包含特定文本时都会显示一条弹出消息。每当“红色级别”一词出现在任何一个单元格(I22、I23、I34、I35、I36)中时,我希望出现一个 MsgBox。我在上述所有单元格中使用数据验
这会导致语法错误: Sub test() MsgBox("hello world", vbOKCancel) ' syntax error at this line Exit Sub
我正在尝试编写一个快速的小宏,要求用户输入,然后将其复制到特定单元格(Sheet1 中的 B14)。这是我到目前为止所得到的: Option Explicit Sub updatesheet()
我是一名优秀的程序员,十分优秀!