Excel VBA - 需要使用for循环,if语句和消息框构建一行代码的帮助

时间:2023-01-18 12:47:16

i need help in constructing a code that would basically display a message box that would act as an error message or notification message of sorts. Here's a sample code that I was thinking of that I know will work but not exactly the output I was looking for

我需要帮助构建一个代码,该代码基本上会显示一个消息框,该消息框将充当各种错误消息或通知消息。这是我想到的一个示例代码,我知道它可以工作,但不完全是我正在寻找的输出

lr = Sheet1.Cells(Rows.Count, 1).End(xlUP).Row

for a = 1 to lr
   if Sheet1.Cells(a,1) = Date then 
      ` A whole new set of codes here that's not really an issue so i'll save time and just skip writing it down

   else MsgBox "No Data for Today!"
   end if
next a

I know this will work and display a msgbox with the words "No Data for Today!", but the thing is, everytime the for-loop goes for another pass, it will again display the msgbox. What I want to happen is it should only display the msgbox ONCE and only AFTER it has checked all the cells and verified that the data on those cells are NOT equal to the current date.

我知道这会工作并显示一个带有“今天没有数据!”字样的msgbox,但事实是,每当for循环进行另一次传递时,它将再次显示msgbox。我想要发生的是它应该只显示msgbox ONCE,并且只有在检查了所有单元格并验证这些单元格上的数据不等于当前日期之后。

Any help would be greatly appreciated. :)

任何帮助将不胜感激。 :)

EDIT:

Just a brief explanation of what i'm trying to achieve. So basically, the code I provided checks every row of column A to if the data inside of it is equal to the current date right? So what I want to happen is, I wan't it to check all the cells in that range and display a MsgBox IF NONE of the cells in that range is equal to the current date, and IF THEY ARE EQUAL to the current date, then it should not display the MsgBox. I tried running the code below from @JGreenwell and it did display the right MsgBox IF all the data on the range of cells ARE NOT EQUAL to the current date, HOWEVER, if I add data in the range that is EQUAL to the current date, it still displays the msgbox even if there's at least one data that's equal to the current date.

只是简要解释我想要实现的目标。所以基本上,我提供的代码检查A列的每一行,如果它内部的数据等于当前日期对吗?所以我想要发生的是,我不想检查该范围内的所有单元格并显示一个MsgBox IF NONE,该范围内的单元格等于当前日期,如果它们与当前日期相等,那么它不应该显示MsgBox。我尝试从@JGreenwell运行下面的代码,它确实显示了正确的MsgBox如果单元格范围内的所有数据都不等于当前日期,但是,如果我在当前日期的EQUAL范围内添加数据,即使至少有一个数据等于当前日期,它仍会显示msgbox。

I hope this is clearer than my original question. And I really do appreciate all the tips/advice/and help that you guys are telling me. :)

我希望这比我原来的问题更清楚。我真的很感谢你们告诉我的所有提示/建议和帮助。 :)

Dim dataFlag As Boolean
dataFlag = False 'create and set boolean type flag

for a = 1 to lr
    if Sheet1.Cells(a,1) = Date then
        ' A whole new set of codes here that's not really an issue so i'll save time and just skip writing it down

    else 
        dataFlag = True
    end if
next a

If dataFlag Then
    MsgBox "No Data for Today!"
End If

2 个解决方案

#1


0  

lr = Sheet1.Cells(Rows.Count, 1).End(xlUP).Row

for a = 1 to lr
   if Sheet1.Cells(a,1) = Date then
      ` A whole new set of codes here that's not really an issue so i'll save time and just skip writing it down

   else 
       UnFound = 1
   end if
next a
If UnFound = 1 Then MsgBox "No Data for Today!"

#2


0  

You can set a flag instead and just display the message if the flag is set.

您可以设置一个标志,只是在设置了标志时显示消息。

Dim dataFlag As Boolean
dataFlag = False 'create and set boolean type flag

for a = 1 to lr
    if Sheet1.Cells(a,1) = Date then
        ' A whole new set of codes here that's not really an issue so i'll save time and just skip writing it down

    else 
        dataFlag = True
    end if
next a

If dataFlag Then
    MsgBox "No Data for Today!"
End If

Flags are a standard programming practice that allows a condition to be associated with an algorithm (set of instructions). For your case, a simple Boolean type works but this can also be combined with other data structures and/or multiple conditions: like setting a flag to one of several values and taking different actions based on what value the flag contains.

标志是一种标准编程实践,它允许条件与算法(指令集)相关联。对于您的情况,一个简单的布尔类型可以工作,但是这也可以与其他数据结构和/或多个条件组合:比如将标志设置为多个值之一,并根据标志包含的值采取不同的操作。

#1


0  

lr = Sheet1.Cells(Rows.Count, 1).End(xlUP).Row

for a = 1 to lr
   if Sheet1.Cells(a,1) = Date then
      ` A whole new set of codes here that's not really an issue so i'll save time and just skip writing it down

   else 
       UnFound = 1
   end if
next a
If UnFound = 1 Then MsgBox "No Data for Today!"

#2


0  

You can set a flag instead and just display the message if the flag is set.

您可以设置一个标志,只是在设置了标志时显示消息。

Dim dataFlag As Boolean
dataFlag = False 'create and set boolean type flag

for a = 1 to lr
    if Sheet1.Cells(a,1) = Date then
        ' A whole new set of codes here that's not really an issue so i'll save time and just skip writing it down

    else 
        dataFlag = True
    end if
next a

If dataFlag Then
    MsgBox "No Data for Today!"
End If

Flags are a standard programming practice that allows a condition to be associated with an algorithm (set of instructions). For your case, a simple Boolean type works but this can also be combined with other data structures and/or multiple conditions: like setting a flag to one of several values and taking different actions based on what value the flag contains.

标志是一种标准编程实践,它允许条件与算法(指令集)相关联。对于您的情况,一个简单的布尔类型可以工作,但是这也可以与其他数据结构和/或多个条件组合:比如将标志设置为多个值之一,并根据标志包含的值采取不同的操作。