VBA - 存储内存以检查组合

时间:2015-06-09 03:37:32

标签: excel vba

我有一个这种模式的数据库,每个省平均有1000行(我只需要一些省份可能是1或2):

Town Code: 3201 Locality Code: 010 Locality Desc: Kuta SubLocalityCode: 005 SubLocality Desc: Legian

我想验证我的excel文件,该文件具有与以下相同的列: 城镇代码,地点代码,地点描述,SublocalityCode,SubLocality Desc

代码应验证组合是否正确。例如3202 010 Kuta 005 Seminyak是错误的,因为005与3201作为城镇代码,010作为地点应该是勒吉安。错误的线条将显示为红色。有没有关于数据库大小的有效方法?

任何帮助将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:0)

Excel VBA允许您遍历电子表格中的每一行。

我认为您尚未设置电子表格以将数据标记为Table,因此我们必须使用传统的Range

Option Explicit

Sub Validate()

    Dim rng As Range
    Dim row As Range
    Dim cell As Range ' There is no `Cell` type in Excel, everything is a `Range`

    Set rng = Range("A2:E999")
    For Each row in rng.Rows

        For Each cell in row.Cells
            Select Case cell.Column
                Case 1 ' A: Town code
                    If Not TownCodeIsValid( cell.Value ) Then
                        MsgBox "Cell " & cell.Address & " is not valid."
                    End If
                Case 2 ' B: Locality code
                    If Not LocalityCodeIsValid( cell.Value ) Then
                        MsgBox "Cell " & cell.Address & " is not valid."
                    End If
                Case 3 ' C: Locality desc
                    If Not LocalityDescIsValid( cell.Value ) Then
                        MsgBox "Cell " & cell.Address & " is not valid."
                    End If
                Case 4 ' D: Sublocality code
                    If Not SublocalityCodeIsValid( cell.Value ) Then
                        MsgBox "Cell " & cell.Address & " is not valid."
                    End If
                Case 5 ' E: Sublocality desc
                    If Not SublocalityDescIsValid( cell.Value ) Then
                        MsgBox "Cell " & cell.Address & " is not valid."
                    End If
            End Select
        Next
    Next
End Sub

Function TownCodeIsValid(townCode As String) As Boolean
    ' Your validation code here
End Function

' and repeat for the other columns