Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在数组中循环并写入错误消息_Arrays_Excel_Vba_For Loop - Fatal编程技术网

Arrays 在数组中循环并写入错误消息

Arrays 在数组中循环并写入错误消息,arrays,excel,vba,for-loop,Arrays,Excel,Vba,For Loop,我有一个表a:G,其中有特定的必填列(a,C,D,F,G),我在其中突出显示单元格并在G中编写消息。列F是一个日期,我也在检查它是否您需要指定一个合适的值 以下是代码中的一些小更改: Option Base 1 Sub ValidateArrayColumns() Dim errormsg() As Variant Dim Drng As Long Dim Row As Single Dim Column As Single Dim tmpDate As Variant Dim IsErr

我有一个表
a:G
,其中有特定的必填列(
a
C
D
F
G
),我在其中突出显示单元格并在
G
中编写消息。列
F
是一个日期,我也在检查它是否 我的最终目标是在
G
列中编写多条错误消息,但我还没有做到

非常感谢您的帮助

Option Base 1

Sub ValidateArrayColumns()

Dim errormsg() As Variant
Dim Drng As Long
Dim Row As Single
Dim Column As Single
Dim tmpDate As Variant
Dim IsError As Boolean
Dim arrReq(5) As Variant
Dim i As Single

arrReq(1) = Worksheets("Sheet2").Cells(Row, 1)
arrReq(2) = Worksheets("Sheet2").Cells(Row, 3)
arrReq(3) = Worksheets("Sheet2").Cells(Row, 4)
arrReq(4) = Worksheets("Sheet2").Cells(Row, 6)
arrReq(5) = Worksheets("Sheet2").Cells(Row, 7)

    Drng = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row

    i = 1

    For Row = 2 To Drng
        For Column = 1 To 7
            If Column = arrReq(i) Then
                For i = 1 To arrReq(5)
                    If Cells(Row, arrReq(i)) = "" Then       'Required fields
                        Cells(Row, arrReq(i)).Interior.ColorIndex = 6
                        IsError = True
                    End If
                Next i
            End If
        Next Column

            'Checks Date
            tmpDate = Cells(Row, 4).Value
            If tmpDate = "" Then
                Cells(Row, 4).Interior.ColorIndex = 6
                IsError = True
            ElseIf tmpDate < Date Then
                Cells(Row, 4).Interior.ColorIndex = 4
                IsError = True
            End If

            'Writes error message
            If IsError = True Then
                Cells(Row, 8).Value = "Highlighted fields contain errors"
            End If

            IsError = False
    Next Row

End Sub
选项库1
子列()
Dim errormsg()作为变量
暗图和长图一样长
单列
将列设置为单个
Dim tmpDate作为变体
作为布尔值的Dim-IsError
Dim arrReq(5)作为变型
我是单身吗
arrReq(1)=工作表(“表2”)。单元格(第1行)
arrReq(2)=工作表(“表2”)。单元格(第3行)
arrReq(3)=工作表(“表2”).单元格(第4行)
arrReq(4)=工作表(“表2”).单元格(第6行)
arrReq(5)=工作表(“表2”).单元格(第7行)
Drng=工作表(“Sheet2”)。单元格(Rows.Count,“A”)。结束(xlUp)。行
i=1
对于行=2至Drng
对于列=1到7
如果列=arrReq(i),则
对于i=1至arrReq(5)
如果单元格(行,arrReq(i))=“Then”必填字段
单元格(行,arrReq(i)).Interior.ColorIndex=6
IsError=True
如果结束
接下来我
如果结束
下一栏
“检查日期
tmpDate=单元格(第4行)。值
如果tmpDate=”“,则
单元格(第4行)。Interior.ColorIndex=6
IsError=True
ElseIf tmpDate<当时的日期
单元格(第4行)。Interior.ColorIndex=4
IsError=True
如果结束
'写入错误消息
如果IsError=True,则
单元格(第8行)。Value=“突出显示的字段包含错误”
如果结束
IsError=False
下一排
端接头

声明row变量后,默认的row=0

工作表(“表2”)。单元格(第1行)
相当于
工作表(“表2”)。单元格(0,1)
这会导致错误->您需要指定一个合适的值

以下是代码中的一些小更改:

 Option Base 1

Sub ValidateArrayColumns()

Dim errormsg() As Variant
Dim Drng As Long
Dim Row As Single
Dim Column As Single
Dim tmpDate As Variant
Dim IsError As Boolean
Dim arrReq(5) As Variant
Dim i As Single
Row = 1 '///
With Worksheets("Sheet2")
    arrReq(1) = .Cells(Row, 1)
    arrReq(2) = .Cells(Row, 3)
    arrReq(3) = .Cells(Row, 4)
    arrReq(4) = .Cells(Row, 6)
    arrReq(5) = .Cells(Row, 7)

    Drng = .Cells(Rows.Count, "A").End(xlUp).Row
End With
    i = 1

    For Row = 2 To Drng
        For Column = 1 To 7
            If Column = arrReq(i) Then
                For i = 1 To arrReq(5)
                    If Cells(Row, arrReq(i)) = vbNullString Then 'Required fields
                        Cells(Row, arrReq(i)).Interior.ColorIndex = 6
                        IsError = True
                        Exit For '///
                    End If
                Next i
            End If
        Next Column

            'Checks Date
            tmpDate = Cells(Row, 7).Value
            If tmpDate = vbNullString Then
                Cells(Row, 7).Interior.ColorIndex = 6
                IsError = True
            ElseIf tmpDate < Date Then
                Cells(Row, 7).Interior.ColorIndex = 4
                IsError = True
            End If

        'Writes error message
        If IsError = True Then
            Cells(Row, 8).Value = "Highlighted fields contain errors"
        End If

        IsError = False

    Next Row

End Sub
选项库1
子列()
Dim errormsg()作为变量
暗图和长图一样长
单列
将列设置为单个
Dim tmpDate作为变体
作为布尔值的Dim-IsError
Dim arrReq(5)作为变型
我是单身吗
行=1'///
带工作表(“表2”)
arrReq(1)=.个单元格(第1行)
arrReq(2)=.个单元格(第3行)
arrReq(3)=.个单元格(第4行)
arrReq(4)=.个单元格(第6行)
arrReq(5)=.个单元格(第7行)
Drng=.Cells(Rows.Count,“A”).End(xlUp).Row
以
i=1
对于行=2至Drng
对于列=1到7
如果列=arrReq(i),则
对于i=1至arrReq(5)
如果单元格(行,arrReq(i))=vbNullString,则“必填字段”
单元格(行,arrReq(i)).Interior.ColorIndex=6
IsError=True
退出///
如果结束
接下来我
如果结束
下一栏
“检查日期
tmpDate=单元格(第7行)。值
如果tmpDate=vbNullString,则
单元格(第7行)。Interior.ColorIndex=6
IsError=True
ElseIf tmpDate<当时的日期
单元格(第7行)。Interior.ColorIndex=4
IsError=True
如果结束
'写入错误消息
如果IsError=True,则
单元格(第8行)。Value=“突出显示的字段包含错误”
如果结束
IsError=False
下一排
端接头

阅读代码注释,并根据需要进行调整

Option Explicit

Option Base 1

Private Sub ValidateRange()

    Dim evalSheet As Worksheet
    Dim evalRange As Range
    Dim evalRow As Range
    Dim evalCell As Range

    Dim evalSheetName As String
    Dim evalColumns As String

    Dim firstRow As Long
    Dim lastRowColumn As String
    Dim lastRow As Long
    Dim relativeCol As Long
    Dim counter As Long
    Dim columnCommments As Long

    Dim errorType As Long

    Dim errorCounter As Long
    Dim errorDescrip As String
    Dim errorConcat As String

    Dim validationRule(5) As Variant

    ' Adjust the parameters to fit your needs
    evalSheetName = "Sheet2"
    evalColumns = "A:G"
    lastRowColumn = "A"                          ' Column where it's going to be searched for the last non empty row
    firstRow = 2                                 ' Skip headers

    columnCommments = 8


    ' Define the rules like column number, validation type, error description

    validationRule(1) = Array(1, "Non empty")
    validationRule(2) = Array(3, "Non empty")
    validationRule(3) = Array(4, "Non empty")
    validationRule(4) = Array(6, "Non empty")
    validationRule(5) = Array(7, "Greater than today")

    ' Set a reference to the sheet where the validation takes place
    Set evalSheet = ThisWorkbook.Worksheets(evalSheetName)

    ' Find the last row with a value in a specific column
    lastRow = evalSheet.Cells(evalSheet.Rows.Count, lastRowColumn).End(xlUp).Row

    ' Define the range to be validated
    Set evalRange = Intersect(evalSheet.Range(evalColumns), evalSheet.Rows(firstRow & ":" & lastRow))

    ' Search per row
    For Each evalRow In evalRange.Rows

        ' Reset error counter
        errorCounter = 0

        ' Reset error comments
        evalSheet.Cells(evalRow.Row, columnCommments).Value = vbNullString

        ' Loop through all cells and check if they are required and empty
        For Each evalCell In evalRow.Cells

            ' Reset error description
            errorDescrip = vbNullString


            ' Cell column is relative to the column where the range begins
            relativeCol = (evalCell.Column - evalRange.Column + 1)

            ' Get the validation result per cell
            errorType = IsCellValidAndReturnErrorType(evalCell, relativeCol, validationRule)

            Select Case errorType
            Case 0
                ' Reset format
                evalCell.Interior.ColorIndex = 0
            Case 1
                errorDescrip = errorDescrip & " " & "Cell cannot be empty"
                evalCell.Interior.ColorIndex = 6
            Case 2
                errorDescrip = errorDescrip & " " & "Cell should be a date"
                evalCell.Interior.ColorIndex = 4
            Case 3
                errorDescrip = errorDescrip & " " & "Cell should be greater than today"
                    evalCell.Interior.ColorIndex = 3
            Case Else

            End Select


            If errorType <> 0 Then

                If errorCounter >= 1 Then
                    errorConcat = " | "
                Else
                    errorConcat = vbNullString
                End If

                evalSheet.Cells(evalRow.Row, columnCommments).Value = evalSheet.Cells(evalRow.Row, columnCommments).Value & errorConcat & evalCell.Address & " has error: " & errorDescrip
                errorCounter = errorCounter + 1
            End If

        Next evalCell

    Next evalRow

End Sub

Private Function IsCellValidAndReturnErrorType(ByVal evalCell As Range, ByVal cellColumn As Long, ByVal validationRule As Variant) As Long

    Dim errorType As Long
    Dim counter As Long
    Dim errorDescrip As String

    For counter = 1 To UBound(validationRule, 1)

        ' Check if cell column has validations
        If cellColumn = validationRule(counter)(1) Then

            ' Check if meets validation rule
            Select Case validationRule(counter)(2)
            Case "Non empty"
                If evalCell.Value = vbNullString Then
                    errorType = 1
                    Exit For
                End If
            Case "Greater than today"
                If IsDate(evalCell.Value) = False Then
                    errorType = 2
                    Exit For
                ElseIf evalCell.Value < Date Then
                    errorType = 3
                    Exit For
                End If
            Case Else
                errorType = 0
            End Select
        End If

    Next counter

    IsCellValidAndReturnErrorType = errorType

End Function
选项显式
选项基数1
专用子验证程序()
将评估表设置为工作表
Dim evalRange As范围
变暗evalRow As范围
变暗evalCell As范围
Dim evalSheetName作为字符串
Dim evalColumns作为字符串
第一排一样长
将lastRowColumn设置为字符串
最后一排一样长
暗相对论
昏暗的柜台一样长
暗淡的柱状文字如长
Dim errorType尽可能长
暗淡的错误计数器
Dim ErrorDescription为字符串
Dim errorConcat作为字符串
Dim验证规则(5)作为变量
'调整参数以满足您的需要
evalSheetName=“Sheet2”
evalColumns=“A:G”
lastRowColumn=“A”列,将在其中搜索最后一个非空行
firstRow=2'跳过标题
ColumnComments=8
'定义列号、验证类型、错误描述等规则
validationRule(1)=数组(1,“非空”)
validationRule(2)=数组(3,“非空”)
validationRule(3)=数组(4,“非空”)
validationRule(4)=数组(6,“非空”)
validationRule(5)=数组(7,“大于今天”)
'设置对进行验证的工作表的引用
Set evalSheet=此工作簿。工作表(evalSheetName)
'查找在特定列中具有值的最后一行
lastRow=evalSheet.Cells(evalSheet.Rows.Count,lastRowColumn)。结束(xlUp)。行
'定义要验证的范围
Set evalRange=Intersect(evalSheet.Range(evalColumns),evalSheet.Rows(firstRow&“:”&lastRow))
'每行搜索
对于evalRange.行中的每个evalRow
'重置错误计数器
errorCounter=0
'重置错误注释
evalSheet.Cells(evalRow.Row,ColumnComments).Value=vbNullString
'循环遍历所有单元格,并检查它们是否为必填项和空
对于evalRow.Cells中的每个evalCell
'重置错误说明
errorDescrip=vbNullString
'单元格列相对于范围开始的列