宏范围/特定单元格msg框

时间:2016-12-15 11:43:03

标签: excel vba macros msgbox

你可以帮我宏吗?我有一个3代码,只有 - 特定值时才显示msg框。但是对我不起作用。 :/我不知道为什么。一些错误,我想制作所有范围,并在显示值msgbox后实现值。有人知道吗?

现在我已经显示错误看图像了

Public Sub found(worksheets)

Dim found As Boolean
Dim c   As Range

found = False
For Each c In worksheets("data").Range("D155,D456,D757,D1058,D1359,D1660,D1961:D1964,D36811,D36813,D38015,D38617,D39219,D39821,D40423,D41025,D52576,D53178,D54984,D55586,D56790,D57392,D58897")

    If c.Value = 8 Then
        found = True
        c.Value = -1
    End If
Next
If found Then MsgBox ("    ZAHLASTE BALENÍ !!!") & vbCrLf & ("BALÍCÍ MNOŽSTVÍ JE 15 KS")

found = False
For Each c In worksheets("data").Range("D29,D31,D33,D35,D37,D39,D41,D43,D45,D47,D49,D51:D57,D59,D61,D63,D65,D67:D83,D85,D87,D89,D91:D95,D97:D101,D103,D105,D107,D109,D110:D111,D41944,D42246:D42250,D45263,D45265,D45267,D45269,D45271,D45273,D45275,D45277,D45279,D45280,D45581,D45882,D46183,D46484,D46785,D47086,D47387")

    If c.Value = 5 Then
        found = True
        c.Value = -1
    End If
Next
If found Then MsgBox ("    ZAHLASTE BALENÍ") & vbCrLf & ("BALÍCÍ MNOŽSTVÍ JE 6 KS")

found = False
For Each c In worksheets("data").Range("D3165,D3466,D3767,D4068,D4369,D4670,D4971,D5272,D5573,D5874,D6175:D10088,D10389,D10690,D41643,D41945,D42251,D42552,D42853,D43154,D43455,D43755,D44057,D44357,D44658,D44959,D48892,D49193,D49494,D49795,D50097,D50397,D50698,D50999,D51308:D51339")

    If c.Value = 8 Then
        found = True
        c.Value = -1
    End If
Next
If found Then MsgBox ("    ZAHLASTE BALENÍ") & vbCrLf & ("BALÍCÍ MNOŽSTVÍ JE 9 KS")

End Sub

在下一张表中,我致电Call Module1.check(worksheets)

这部分代码显示错误。            enter image description here

1 个答案:

答案 0 :(得分:2)

除了应用上述评论之外(特别是使用"D155"代替"D155:D155"等,

以下是在每次操作完成后如何只显示一次msgbox:

Dim found as Boolean

found = false
For Each c In worksheets("data").Range(...)
    If c.Value = 8 Then
        found = True
        c.Value = -1
    End If
Next
If found then MsgBox ("    ZAHLASTE BALENÍ !!!") & vbCrLf & ("BALÍCÍ MNOŽSTVÍ JE 15 KS")

found = false
For Each c In worksheets("data").Range(...)
    If c.Value = 5 Then
        found = True
        c.Value = -1
    End If
Next
If found then MsgBox ("    ZAHLASTE BALENÍ") & vbCrLf & ("BALÍCÍ MNOŽSTVÍ JE 6 KS")

found = false
For Each c In worksheets("data").Range(...)
    If c.Value = 8 Then
        found = True
        c.Value = -1
    End If
Next
If found then MsgBox ("    ZAHLASTE BALENÍ") & vbCrLf & ("BALÍCÍ MNOŽSTVÍ JE 9 KS")

编辑:下一个问题

如果提交的字符串的长度太长,超过255个字符,方法Range将失败。您可以通过将范围的规范分为两部分来解决此问题:

Dim r As Range
Set r = worksheets("data").Range("D29,D31,D33,D35,D37,D39,D41,D43,D45,D47,D49,D51:D57,D59,D61,D63,D65,D67:D83,D85,D87,D89,D91:D95,D97:D101,D103,D105,D107,D109,D110:D111,D41944,D42246:D42250,D45263,D45265,D45267,D45269,D45271,D45273")
Set r = Union(r, worksheets("data").Range("D45275,D45277,D45279,D45280,D45581,D45882,D46183,D46484,D46785,D47086,D47387"))
For Each d in r  ' <-- Proceed from here