要检查工作表中是否存在特定列的VBA

时间:2018-03-08 05:43:51

标签: excel vba excel-vba

在进入主程序之前,我正在检查sheet1中是否存在某些特定的列,但问题出在原始数据表列中,如“Employee Name”& “指定”在该表中,但是当运行下面的代码时,它会给出一条消息“未找到”并且它正在循环中出来,请你帮我解决这个问题。

Dim rngX As Range
Sub Colvalidation1()

Set rngX = Worksheets("Sheet1").Range("A1:S1").Find("Work Geography", lookat:=xlPart)
If Not rngX Is Nothing Then
Call Colvalidation2
Else
    MsgBox "Work Geography - Column Not Found"
End If
End Sub

Sub Colvalidation2()

Set rngX = Worksheets("Sheet1").Range("A1:S1").Find("Work Country",
lookat:=xlPart)
If Not rngX Is Nothing Then
Call Colvalidation3
Else
    MsgBox "Work Country - Column Not Found"
End If
End Sub

Sub Colvalidation3()

Set rngX = Worksheets("Sheet1").Range("A1:S1").Find("Project #", lookat:=xlPart)
If Not rngX Is Nothing Then
Call Colvalidation4
Else
    MsgBox "Project # - Column Not Found"
End If
End Sub

Sub Colvalidation4()

Set rngX = Worksheets("Sheet1").Range("A1:S1").Find("Project Name", lookat:=xlPart)
If Not rngX Is Nothing Then
Call Colvalidation5
Else
    MsgBox "Project Name - Column Not Found"
End If
End Sub

Sub Colvalidation5()

Set rngX = Worksheets("Sheet1").Range("A1:S1").Find("Employee #", lookat:=xlPart)
If Not rngX Is Nothing Then
Call Colvalidation8
Else
    MsgBox "Employee # - Column Not Found"
End If
End Sub

Sub Colvalidation6()

Set rngX = Worksheets("Sheet1").Range("A1:S1").Find("Employee Name", lookat:=xlPart)
If Not rngX Is Nothing Then
Call Colvalidation7
Else
    MsgBox "Employee Name - Column Not Found"
End If
End Sub

Sub Colvalidation7()

Set rngX = Worksheets("Sheet1").Range("A1:S1").Find("Designation", lookat:=xlPart)
If Not rngX Is Nothing Then
Call Colvalidation8
Else
    MsgBox "Designation - Column Not Found"
End If
End Sub  

2 个答案:

答案 0 :(得分:2)

您不需要重复代码

使用Array保留您的列标题以进行搜索,并使用一位帮助SubFunction为每个人调用

如果你想在第一个找到的列标题处停止,则会执行以下操作:

Option Explicit

Dim rngX As Range
Sub main()
    Dim vals As Variant, val As Variant
    vals = Array("Work Geography", "Work Country", "Project #", "Project Name", "Employee #", "Employee Name")

    Dim rngToSearchIn As Range
    Set rngToSearchIn = Worksheets("Sheet1").Range("A1:S1")

    For Each val In vals
        If Colvalidation(rngToSearchIn, val) Then Exit For
    Next
    If Not rngX Is Nothing Then MsgBox "found " & val & " in " & rngX.Address
End Sub

Function Colvalidation(rng As Range, value As Variant) As Boolean
    Set rngX = rng.Find(what:=value, lookat:=xlPart, LookIn:=xlValues)
    Colvalidation = Not rngX Is Nothing
    If Not Colvalidation Then MsgBox value & " - Column Not Found"
End Function

如果您要处理所有列标题,那么以下操作可以:

Option Explicit

Dim rngX As Range

Sub main()
    Dim vals As Variant, val As Variant
    vals = Array("Work Geography", "Work Country", "Project #", "Project Name", "Employee #", "Employee Name")

    Dim rngToSearchIn As Range
    Set rngToSearchIn = Worksheets("Sheet1").Range("A1:S1")

    For Each val In vals
        Colvalidation rngToSearchIn, val
    Next
End Sub

Sub Colvalidation(rng As Range, value As Variant)
    Set rngX = rng.Find(what:=value, lookat:=xlPart, LookIn:=xlValues)
    If rngX Is Nothing Then
        MsgBox value & " - Column Not Found"
    Else
        MsgBox "found " & value & " in " & rngX.Address
    End If
End Sub

答案 1 :(得分:1)

请试试这个

    Sub Find_Columns()

    Dim rngToSearch As Range
    Dim WhatToFind As Variant
    Dim iCtr As Long

    Set rngToSearch = ThisWorkbook.Worksheets("Sheet1").Range("A1:S1")

    WhatToFind = Array("Project Name", "Employee #", "Employee Name") 'add all Column header that you want to check

    With rngToSearch
        For iCtr = LBound(WhatToFind) To UBound(WhatToFind)
            If WorksheetFunction.CountIf(rngToSearch, WhatToFind(iCtr)) > 0 Then ' Check if column is preset or not
                    MsgBox WhatToFind(iCtr) & " Column Found" ' Pop-up msg if column is exist
            Else
                    MsgBox WhatToFind(iCtr) & " Column Not Found" ' Pop-up msg if column is Not Found
            End If
        Next
    End With

    End Sub