VBA - 代码中的更改

时间:2017-10-27 16:13:17

标签: vba excel-vba excel

我有一个如下所示的代码: - ))

2 个答案:

答案 0 :(得分:0)

删除以下行(并更新您的评论!评论和代码中的列名称不相同):

    'condition for Column A (cell not empty & characters in cell are alphabet)
    For i = 1 To Len(cel)
    If Not (Not IsEmpty(cel) And Asc(UCase(cel)) > 64 And Asc(UCase(cel)) < 91) Then
            colCStr = colCStr & "," & cel.row
            Exit For
        End If
    Next i

答案 1 :(得分:0)

此版本不使用Offset,因此更新更容易(也更有效)

Option Explicit

Public Sub CheckColDandE()
    Dim ws As Worksheet, lr As Long, arr As Variant, r As Long
    Dim dOk As Boolean, eOk As Boolean, dErr As String, eErr As String

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    lr = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    arr = ws.Range("D2:E" & lr)

    For r = 1 To lr - 1

        dOk = IsNumeric(arr(r, 1)) And arr(r, 1) > 9 And arr(r, 1) < 1000

        eOk = IsNumeric(arr(r, 2))
        eOk = eOk And (arr(r, 2) > 999999 And arr(r, 2) < 100000000 Or arr(r, 2) = 0)

        If Not dOk Then dErr = dErr & r + 1 & ", "
        If Not eOk Then eErr = eErr & r + 1 & ", "
    Next

    With ws.Range("D" & lr + 1 & ":E" & lr + 1)
        .Value2 = vbNullString
        If Len(dErr) > 0 Then .Cells(1) = "Rows with Errors: " & Left(dErr, Len(dErr) - 2)
        If Len(eErr) > 0 Then .Cells(2) = "Rows with Errors: " & Left(eErr, Len(eErr) - 2)
    End With
End Sub