循环代码范围的单元格,而不是一次验证一个单元格

时间:2015-12-11 07:21:20

标签: excel vba excel-vba

我添加了快照。在这里你可以看到代码适用于C4单元用于手机号码验证。所以,我希望它适用于C4-C16并且显示结果是A26 / B26。如果条件在C4-C16之间的任何地方失败,则B26应为'数字不正确。

ter

此代码适用于指定的一个单元格,但我想更改它以验证单元格范围。

在这段代码中,第一个条件检查C3单元格中的特定标题,这很好但在此之后代码检查数字是从单元格C4中的“44”开始及其长度。

所以,我想更新这个部分,以便代码检查单元格C4到C16并查看长度是否在10到13之间并以'44'开头,如果任何单元格缺少这个条件,那么B27应该显示消息'The用户号码不行。

import configparser
my_config_parser = configparser.SafeConfigParser()
my_config_parser.read('name of your configuration file')
payload = {
    'action': my_config_parser.get('DEFAULT','action'),
    'email': my_config_parser.get('DEFAULT','email'),
    'password': my_config_parser.get('DEFAULT','password')
}

2 个答案:

答案 0 :(得分:0)

试试这个,让我知道:

Sub Validate13()
Dim mymsg As String
Dim celltext1 As String
Dim condition1 As Boolean
Dim condition2 As Boolean

celltext1 = ActiveSheet.Range("C3").Text
celltext2 = ActiveSheet.Range("E4").Text



condition1 = False
i = 4
Do

    If InStr(1, Range("C" & i).Text, "44") = 1 Then
        condition1 = True

            If condition1 = True And i = 16 Then
                 Range("$B$27").Value = "The subscriber number starts with 44 and is OK."
                Range("$A$27").Interior.Color = vbGreen
            End If

    Else
        Range("$B$27").Value = "The subscriber number is not OK. Please check."
        Range("$A$27").Interior.Color = vbYellow
        condition1 = False
    End If

    i = i + 1
Loop Until condition1 = False Or i = 17


condition2 = False
i = 4
Do

        If Len(Range("C" & i).Text) > 10 And Len(Range("C" & i).Text) < 13 Then
                condition2 = True

                    If condition1 = True And i = 16 Then
                        Range("$B$28").Value = "The subscriber number length is " & Len(Range("C" & i).Text) & " and is OK ."
                        Range("$A$28").Interior.Color = vbGreen
                    End If

        Else
            Range("$B$28").Value = "The subscriber number Length not OK. Please check."
            Range("$A$28").Interior.Color = vbYellow
            condition2 = False

        End If

    i = i + 1
Loop Until condition2 = False Or i = 17



If InStr(1, celltext1, "mz_013_reg-life_") = 1 Then
 Range("$B$26").Value = "The Header is OK."
 Range("$A$26").Interior.Color = vbGreen
Else
 Range("$B$26").Value = "The header is not OK. Please check."
 Range("$A$26").Interior.Color = vbYellow
End If


If Len(celltext2) > 13 And Len(celltext2) < 15 Then
 Range("$B$29").Value = "The timestamp length is " & Len(celltext2) & " and is OK ."
 Range("$A$29").Interior.Color = vbGreen
Else
 Range("$B$29").Value = "The timestanmp not OK. Please check."
 Range("$A$29").Interior.Color = vbYellow
End If


End Sub

答案 1 :(得分:0)

此代码适用于我:

Sub Validate13()
Dim mymsg As String
Dim celltext As String
Dim celltext1 As String
Dim cl As Range
Dim checkRng As Range
Dim int1 As Integer
Dim int2 As Integer
Dim int3 As Integer

celltext1 = ActiveSheet.Range("C3").Text

Set checkRng = ActiveSheet.Range("C4", ActiveSheet.Range("C4").End(xlDown))
If checkRng.Rows.Count > 13 Then
    Set checkRng = ActiveSheet.Range("C4:C16")
End If

If InStr(1, celltext1, "mz_013_reg-life_") = 1 Then
 Range("$B$26").Value = "The Header is OK."
 Range("$A$26").Interior.Color = vbGreen
Else
 Range("$B$26").Value = "The header is not OK. Please check."
 Range("$A$26").Interior.Color = vbYellow
End If


For Each cl In checkRng


    If Not Left(cl.Value, 2) = "44" Then
        int1 = int1 + 1
    End If

    If Len(cl.Value) < 10 Or Len(cl.Value) > 13 Then
        int2 = int2 + 1
    End If

    If Len(cl.Offset(0, 2).Value) < 13 Or Len(cl.Offset(0, 2).Value) > 15 Then
        int3 = int3 + 1
    End If

Next cl

If int1 > 0 Then
    Range("$B$27").Value = "The subscriber number is not OK. Please check."
    Range("$A$27").Interior.Color = vbYellow
Else
     Range("$B$27").Value = "The subscriber number starts with 44 and is OK."
     Range("$A$27").Interior.Color = vbGreen
End If
If int2 > 0 Then
    Range("$B$28").Value = "The subscriber number Length not OK. Please check."
    Range("$A$28").Interior.Color = vbYellow
Else
    Range("$B$28").Value = "The subscriber number length is OK."
    Range("$A$28").Interior.Color = vbGreen
End If
If int3 > 0 Then
   Range("$B$29").Value = "The timestanmp not OK. Please check."
   Range("$A$29").Interior.Color = vbYellow
Else
   Range("$B$29").Value = "The timestamp length is OK."
   Range("$A$29").Interior.Color = vbGreen
End If
End Sub

唯一的问题是你不知道哪个细胞是罪人,如果它显示不好,但我猜你可以找到一个解决方法。