循环使用带有多个验证的内置宏的字符串

时间:2018-02-26 15:54:36

标签: excel excel-vba loops iteration vba

我对VBA很新鲜。因此难以从代码中获取一些基本的东西。

我有一个为两个字符串写的宏。第一个字符串是一个字符串,其中包含我正在验证的值,第二个字符串在第一个字符串之后,带有验证消息。 我需要做的是构建一个循环,如果它们被数据填充,它将重写下一个字符串的写入宏。附图。

E.G。现在我填充了字符串= A7:G7和A8:G8,我需要填充下一个字符串A9:G9和A10:G10

excel sample

代码示例:

'Field 2 (E7) should not exceed 52 characters, it should not be blank'
    If Len(oCell) < 52 And Len(oCell) <> 0 Then
        ThisWorkbook.Sheets("Tool").Range("E8").Value = ""

    ElseIf Len(oCell) > 52 And Len(oCell) <> 0 Then
        ThisWorkbook.Sheets("Tool").Range("E8").Value = "Exceeding"

    Else: ThisWorkbook.Sheets("Tool").Range("E8").Value = "Cannot be blank"


End If
        End With

enter image description here

我希望这是有道理的。谢谢

enter image description here

enter image description here

[在此输入图像说明] [2]

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你会尝试制作一个循环(你的例子特别使用单元格E8)并且不确定如何实现这一点。循环遍历行的基本For循环结构如下:

Dim i as Long, lr as Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'Dynamically finds last row, based on column 1 (A)
For i = 8 to lr Step 1
    'Do something using i
Next i

我们现在可以修复您的代码以使用i,例如:

Sheets("Tool").Range("E" & i).Value = ""

Sheets("Tool").Cells(i, "E").Value = ""

在查看你的例子时,我有点困惑......什么是oCell(特别是哪一列),输出应该在哪里?

从你的例子来看,我看起来似乎正在评估E7并且输出是在E8中...你是通过列还是下行?需要有关源数据的更多信息。

修改

Dim i as Long, lr as Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'Dynamically finds last row, based on column 1 (A)
For i = 8 to lr Step 2
    If len(Cells(i-1,5)) < 52 AND len(Cells(i-1,5)) <> 0 then
        Sheets("Tool").Cells(i,5).Value = ""
    ElseIf len(Cells(i-1,5)) > 52 then
        Sheets("Tool").Cells(i,5).Value = "Exceeding"
    End If
        Sheets("Tool").Cells(i,5).Value = "Cannot be blank"
Next i

编辑2:此代码向下列E

Dim i As Long, lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row 'Dynamically finds last row, based on column 1 (A)
For i = 7 To lr Step 2
    Select Case Len(Cells(i, 5))
        Case 0
            Cells(i + 1, 5).Value = "Cannot be blank"
        Case Is < 52
            Cells(i + 1, 5).Value = ""
        Case Is > 52
            Cells(i + 1, 5).Value = "Exceeded"
    End Select
Next i

编辑3:此代码在第7行右侧

Dim i As Long, lc As Long
lc = Cells(5, Columns.Count).End(xltoLeft).Column
For i = 4 To lc Step 1 'looks like you're going 1 by 1 across row 7
    Select Case Len(Cells(7, i))
        Case 0
            Cells(8, i).Value = "Cannot be blank"
        Case Is < 52
            Cells(8, i).Value = ""
        Case Is > 52
            Cells(8, i).Value = "Exceeded"
    End Select
Next i

编辑4:此代码向下并向右移动

Dim i As Long, j As Long, lr As Long, lc As Long
lr = Cells(Rows.Count, 4).End(xlUp).Row 'Based on last row in column D
'lc = Cells(5, Rows.Count).End(xlToLeft).Column 'Based on last column in row 5 'Not needed, commented out, known end in Column 68
For i = 7 To lr Step 2
    For j = 4 to 68 Step 1 'From D (4) to BP (68)
        Select Case Len(Cells(i, j))
            Case 0
                Cells(i + 1, j).Value = "Cannot be blank"
            Case Is < 52
                Cells(i + 1, j).Value = ""
            Case Is > 52
                Cells(i + 1, j).Value = "Exceeded"
        End Select
    Next j
Next i

答案 1 :(得分:0)

由于我找到了解决问题的最终解决方案,我决定在这里分享,以防有人也会面对它。

我试图在源代码中添加循环,因此我不必重复操作。 我的案例中最好的循环是While,因为我不确切地知道有多少次我需要重申这个动作。

While 'My conditions'
Dim endPoint, validPoint as Long 
   'Code'
endpoint = endpoint + 1
validPoint = validPoint +2
Wend

我创造了条件 endpoint =&#39;使用我的数据&#39; validPoint =&#39;带有我的验证消息&#39;

所以循环将遍历数据然后验证数据,直到例如检查所有数据。

我的代码示例:

'Field 1 (D7) can only be populated with NEW or CANC'

If ThisWorkbook.Sheets("Tool").Range("D" & endPoint).Value = "NEW" Or ThisWorkbook.Sheets("Tool").Range("D" & endPoint).Value = "CANC" Then
    ThisWorkbook.Sheets("Tool").Range("D" & validPoint).Value = ""

ElseIf (ThisWorkbook.Sheets("Tool").Range("D" & endPoint).Value <> "NEW" Or ThisWorkbook.Sheets("Tool").Range("D" & endPoint).Value <> "CANC") And (ThisWorkbook.Sheets("Tool").Range("D" & endPoint).Value) <> 0 Then
    ThisWorkbook.Sheets("Tool").Range("D" & validPoint).Value = "error, wrong format"

Else: ThisWorkbook.Sheets("Tool").Range("D" & validPoint).Value = "Cannot be blank"

End If

我很感激stackoverflow用户给予的热情和帮助。谢谢大家!