无法根据条件if…elseif…else打印正确的消息。

huangapple go评论185阅读模式
英文:

Unable to print correct message based on conditional if...elseif...else

问题

以下是代码部分的翻译:

  1. Sub ViewResults()
  2. '统计空的“Base”工作表数量
  3. Dim i As Integer
  4. Dim b_empty As Integer
  5. b_empty = 0
  6. If IsEmpty(Worksheets("Calypso (Zeiss) Base")) = True Then
  7. b_empty = b_empty + 1
  8. End If
  9. If IsEmpty(Worksheets("Camio (LK) Base")) = True Then
  10. b_empty = b_empty + 1
  11. End If
  12. If IsEmpty(Worksheets("Camio (Nikon) Base")) = True Then
  13. b_empty = b_empty + 1
  14. End If
  15. If IsEmpty(Worksheets("GOM (Blue Light) Base")) = True Then
  16. b_empty = b_empty + 1
  17. End If
  18. If IsEmpty(Worksheets("PC-DMIS (Global) Base")) = True Then
  19. b_empty = b_empty + 1
  20. End If
  21. If IsEmpty(Worksheets("Quindos (HTA) Base")) = True Then
  22. b_empty = b_empty + 1
  23. End If
  24. '统计为True的空工作表数量
  25. Dim j As Integer
  26. Dim c_empty As Integer
  27. c_empty = 0
  28. If IsEmpty(Worksheets("Calypso (Zeiss) Corr")) = True Then
  29. c_empty = c_empty + 1
  30. End If
  31. If IsEmpty(Worksheets("Camio (LK) Corr")) = True Then
  32. c_empty = c_empty + 1
  33. End If
  34. If IsEmpty(Worksheets("Camio (Nikon) Corr")) = True Then
  35. c_empty = c_empty + 1
  36. End If
  37. If IsEmpty(Worksheets("GOM (Blue Light) Corr")) = True Then
  38. c_empty = c_empty + 1
  39. End If
  40. If IsEmpty(Worksheets("PC-DMIS (Global) Corr")) = True Then
  41. c_empty = c_empty + 1
  42. End If
  43. If IsEmpty(Worksheets("Quindos (HTA) Corr")) = True Then
  44. c_empty = c_empty + 1
  45. End If
  46. '检查非空的“Base”和“Corr”工作表数量是否正确
  47. If b_empty > 5 Then
  48. MsgBox "Base Data seems to be missing. Please Import Base Data.", vbOKOnly, "Missing Base Data"
  49. End If
  50. If b_empty < 5 Then
  51. MsgBox "Too many Base sheets contain data. Please try again to Import Base Data.", vbOKOnly, "Too Much Base Data"
  52. End If
  53. If b_empty = 5 And c_empty > 5 Then
  54. MsgBox "Corr Data seems to be missing. Please Import Corr Data.", vbOKOnly, "Missing Corr Data"
  55. End If
  56. If b_empty = 5 And c_empty < 5 Then
  57. MsgBox "Too many Corr sheets contain data. Please try again to Import Corr Data.", vbOKOnly, "Too Much Corr Data"
  58. End If
  59. If b_empty = 5 And c_empty = 5 Then
  60. Worksheets("Data Correlation").Activate
  61. End If
  62. On Error GoTo EmailSupport
  63. Exit Sub
  64. End Sub

希望这对你有所帮助。如果有任何其他翻译需求,请随时告诉我。

英文:

My current code is below. In other Subs, I have .UsedRange.Delete available for each sheet depending on circumstances. However, even when all "Base" and "Corr" sheets are empty, I always get the MsgBox for "Too many Base sheets..."

Does anyone see what I'm missing?

Code Updated

  1. Sub ViewResults()
  2. &#39;Counts number of empty &quot;Base&quot; sheets&#39;
  3. Dim i As Integer
  4. Dim b_empty As Integer
  5. b_empty = 0
  6. If IsEmpty(Worksheets(&quot;Calypso (Zeiss) Base&quot;)) = True Then
  7. b_empty = b_empty + 1
  8. End If
  9. If IsEmpty(Worksheets(&quot;Camio (LK) Base&quot;)) = True Then
  10. b_empty = b_empty + 1
  11. End If
  12. If IsEmpty(Worksheets(&quot;Camio (Nikon) Base&quot;)) = True Then
  13. b_empty = b_empty + 1
  14. End If
  15. If IsEmpty(Worksheets(&quot;GOM (Blue Light) Base&quot;)) = True Then
  16. b_empty = b_empty + 1
  17. End If
  18. If IsEmpty(Worksheets(&quot;PC-DMIS (Global) Base&quot;)) = True Then
  19. b_empty = b_empty + 1
  20. End If
  21. If IsEmpty(Worksheets(&quot;Quindos (HTA) Base&quot;)) = True Then
  22. b_empty = b_empty + 1
  23. End If
  24. &#39;Counts how many cempties are True&#39;
  25. Dim j As Integer
  26. Dim c_empty As Integer
  27. c_empty = 0
  28. If IsEmpty(Worksheets(&quot;Calypso (Zeiss) Corr&quot;)) = True Then
  29. c_empty = c_empty + 1
  30. End If
  31. If IsEmpty(Worksheets(&quot;Camio (LK) Corr&quot;)) = True Then
  32. c_empty = c_empty + 1
  33. End If
  34. If IsEmpty(Worksheets(&quot;Camio (Nikon) Corr&quot;)) = True Then
  35. c_empty = c_empty + 1
  36. End If
  37. If IsEmpty(Worksheets(&quot;GOM (Blue Light) Corr&quot;)) = True Then
  38. c_empty = c_empty + 1
  39. End If
  40. If IsEmpty(Worksheets(&quot;PC-DMIS (Global) Corr&quot;)) = True Then
  41. c_empty = c_empty + 1
  42. End If
  43. If IsEmpty(Worksheets(&quot;Quindos (HTA) Corr&quot;)) = True Then
  44. c_empty = c_empty + 1
  45. End If
  46. &#39;Test for correct number of non-empty base&amp;corr sheets&#39;
  47. If b_empty &gt; 5 Then
  48. MsgBox &quot;Base Data seems to be missing. Please Import Base Data.&quot;, vbOKOnly, &quot;Missing Base Data&quot;
  49. End If
  50. If b_empty &lt; 5 Then
  51. MsgBox &quot;Too many Base sheets contain data. Please try again to Import Base Data.&quot;, vbOKOnly, &quot;Too Much Base Data&quot;
  52. End If
  53. If b_empty = 5 And c_empty &gt; 5 Then
  54. MsgBox &quot;Corr Data seems to be missing. Please Import Corr Data.&quot;, vbOKOnly, &quot;Missing Corr Data&quot;
  55. End If
  56. If b_empty = 5 And c_empty &lt; 5 Then
  57. MsgBox &quot;Too many Corr sheets contain data. Please try again to Import Corr Data.&quot;, vbOKOnly, &quot;Too Much Corr Data&quot;
  58. End If
  59. If b_empty = 5 And c_empty = 5 Then
  60. Worksheets(&quot;Data Correlation&quot;).Activate
  61. End If
  62. On Error GoTo EmailSupport
  63. Exit Sub

I originally had .cells.clear on all sheets in another Sub, but thought maybe there were residual objects, so changed all cases of .cells.clear to .usedrange.delete.

UPDATE: The code has been changed to ask the worksheets individually if they are empty and to update the count if True.
Still getting the same MsgBox from condition 2

答案1

得分: 1

IsEmpty函数在以下情况下返回True:如果变量未初始化或明确设置为Empty;否则返回False。如果表达式包含多个变量,则始终返回False。IsEmpty仅对变体类型的变量返回有意义的信息。

bempties(i)被定义为一个对象,并且您已经用活动工作簿中的工作表引用填充了它,因此它永远不会为空。您可以检查If bempties(i) is nothing then,但是您已将每个实例设置为工作表,因此它们也永远不会为空。运行检查后,b_empty将始终等于0。

bempties的上限始终为6,因为您将其设置为6。

b_empty < (UBound(bempties) - 1)0 < 5始终为真,并显示该消息。

您是想要检查工作表是否为空吗?即工作表上没有数据?此代码可能有所帮助。

注意 - 我使用了UsedRange来查找最后一个单元格 - 请参考我的上一个链接上的被接受的答案,了解更好的方法以及为什么不要使用UsedRange

  1. Public Sub ViewResults()
  2. Dim wrkSht As Worksheet
  3. Dim BaseCount As Long, BaseEmpty As Long
  4. Dim CorrCount As Long, CorrEmpty As Long
  5. 'ThisWorkbook是包含此代码的工作簿。
  6. For Each wrkSht In ThisWorkbook.Worksheets
  7. 'BaseCorr是否出现在工作表名称中?
  8. If InStr(wrkSht.Name, "Base") > 0 Then
  9. BaseCount = BaseCount + 1
  10. '有更好的方法来查找工作表中的最后一个使用的单元格,您仍然需要检查A1是否包含数据。
  11. If wrkSht.UsedRange.Address = "$A$1" Then BaseEmpty = BaseEmpty + 1
  12. ElseIf InStr(wrkSht.Name, "Corr") > 0 Then
  13. CorrCount = CorrCount + 1
  14. If wrkSht.UsedRange.Address = "$A$1" Then CorrEmpty = CorrEmpty + 1
  15. End If
  16. Next wrkSht
  17. MsgBox "Base sheets: " & BaseCount & vbCr & _
  18. "Empty base sheets: " & BaseEmpty & vbCr & vbCr & _
  19. "Corr sheets: " & CorrCount & vbCr & _
  20. "Empty Corr sheets: " & CorrEmpty, vbOKOnly + vbInformation
  21. End Sub

进一步阅读:

英文:

IsEmpty

> IsEmpty returns True if the variable is uninitialized, or is
> explicitly set to Empty; otherwise, it returns False. False is always
> returned if expression contains more than one variable. IsEmpty only
> returns meaningful information for variants.

bempties(i)is defined as an object and you've filled it with references to worksheets in the currently active workbook, so it will never be empty. You could check If bempties(i) is nothing then but you've set each instance to a worksheet, so they'll never be nothing either. After running the check b_empty will always equal 0.

The upper bound of bempties will always be 6 as you've set it to that.
b_empty &lt; (UBound(bempties) - 1) or 0 &lt; 5 will always be true and display the message.

Are you trying to check if the sheet is empty? i.e. No data on the sheet?
This code might help.

Note - I've used UsedRange to find the last cell - see the accepted answer on my last link for better ways and why not to use UsedRange.

  1. Public Sub ViewResults()
  2. Dim wrkSht As Worksheet
  3. Dim BaseCount As Long, BaseEmpty As Long
  4. Dim CorrCount As Long, CorrEmpty As Long
  5. &#39;ThisWorkbook is the book containing this code.
  6. For Each wrkSht In ThisWorkbook.Worksheets
  7. &#39;Does Base or Corr appear in the sheet name?
  8. If InStr(wrkSht.Name, &quot;Base&quot;) &gt; 0 Then
  9. BaseCount = BaseCount + 1
  10. &#39;There are better ways to find the last used cell in sheet and you&#39;ll still need to check if A1 contains data.
  11. If wrkSht.UsedRange.Address = &quot;$A$1&quot; Then BaseEmpty = BaseEmpty + 1
  12. ElseIf InStr(wrkSht.Name, &quot;Corr&quot;) &gt; 0 Then
  13. CorrCount = CorrCount + 1
  14. If wrkSht.UsedRange.Address = &quot;$A$1&quot; Then CorrEmpty = CorrEmpty + 1
  15. End If
  16. Next wrkSht
  17. MsgBox &quot;Base sheets: &quot; &amp; BaseCount &amp; vbCr &amp; _
  18. &quot;Empty base sheets: &quot; &amp; BaseEmpty &amp; vbCr &amp; vbCr &amp; _
  19. &quot;Corr sheets: &quot; &amp; CorrCount &amp; vbCr &amp; _
  20. &quot;Empty Corr sheets: &quot; &amp; CorrEmpty, vbOKOnly + vbInformation
  21. End Sub

Further reading:
For Each...Next statement
InStr function
Find last used cell in Excel VBA

huangapple
  • 本文由 发表于 2023年8月10日 19:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定