【发布时间】:2019-12-28 17:20:06
【问题描述】:
我已经构建了一个工具,可以在宏运行时生成另一个电子表格。为了将该工作表成功上传到系统中,需要以特定方式对其进行格式化。为了防止人们没有正确使用该工具并弄乱新电子表格的格式,我进行了一系列检查。
每个人检查一个单元格是否有一个特定的字符串(“请设置!”),如果有,则弹出一个 msgbox 并停止宏。这个想法是它通过每个检查,直到它遇到一个失败,或者全部通过并运行代码以生成新工作表。目前有点啰嗦,我正在寻找一个循环让它更整洁。
If Worksheets("Input").Range("F7").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("F9").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("F13").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("F17").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("F21").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("L9").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("L13").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("L17").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
End If
If Worksheets("Input").Range("L21").Value = "Please Set!" Then
MsgBox "Please complete all the fields!", vbCritical
Exit Sub
这之后还有一个 else 和生成工作表的代码。
一切都按预期工作,但上面的代码非常庞大,我知道有更好的方法。我研究了不同的循环,但我是 VBA 的新手,所以我不确定哪一个最适合将它浓缩成。任何帮助都会很棒! :)
【问题讨论】:
标签: excel vba loops if-statement