代码之家  ›  专栏  ›  技术社区  ›  Kevin

当单元格区域没有特定长度时显示msgbox

  •  0
  • Kevin  · 技术社区  · 7 年前

    我有一个程序,在该程序中扫描一个标签,该值进入一个单元格,但出于安全原因,有必要检查该值的长度是否始终相同(例如:8,label=12345-78),字符串中有一个“-”。我想让程序做的是,当扫描的标签上有一个不同于此的数字时,显示一个消息框,指示该数字无效,然后删除单元格的内容。我真的很感激你帮我。

    这是迄今为止我掌握的代码:

    Private Sub Worksheet_Change(ByVal Target As Range)
    'macro para prohibir longitud que no sea la seleccionada
    
    Dim rango As Range
    
    
    For Each Range In Worksheets("HojadeInspection").Range("I9:I20")
    
    
    If rango.Len(c.Value) <> 8 Then
    
    MsgBox "La longitud del código insertado no es la correcta", vbcrtical
    
    
    End If
    
    
    
    End Sub
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   VBAmazing    7 年前
    Dim i As Integer
    
    'I is the column I used, switch it to meet your needs
    
    i = Worksheets("Sheet1").Range("I:I").Cells. _
         SpecialCells(xlCellTypeConstants).Count
    
    If Not Len(Range("I" & CStr(i))) = 8 Then
    
    MsgBox "Your Message Here", vbCritical
    End If
    
    End Sub
    

    (从原始响应中编辑的代码)

        2
  •  0
  •   Michał Turczyn    7 年前

    试试这个:

    Private Sub Worksheet_Change(ByVal Target As Range)
        'macro para prohibir longitud que no sea la seleccionada
        Dim forCell As Variant
    
        For Each forCell In Worksheets("HojadeInspection").Range("I9:I20")
            If Not forCell.Value Like "[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9]" Then
                MsgBox "La longitud del código insertado no es la correcta", vbcrtical
            End If
        Next
    End Sub
    
    推荐文章