簡體   English   中英

檢查給定字符串的特定列名,然后突出顯示與 VBA 中給定值不匹配的列中的值

[英]Check for a specific column name given a string then highlight values in the column that doesn't match given value in VBA

如果列名“FileNumber”存在,我需要在圖片中查找給定的列名。 如果它確實存在,我想查看列中的數字是否都是給定的數字(例如它必須是“101”); 如果不正確,我想突出顯示該數字(此處突出顯示“102”)

在此處輸入圖片說明

我怎樣才能在 VBA 中實現這一點?

Sub FindColumns()  
    Dim rngToSearch As Range
    Dim lookToFind As Variant
    Dim iCtr As Long

    Set rngToSearch = ThisWorkbook.Worksheets("Sheet").Range("A1:C1")

    lookToFind = Array("Filename", "FileNumber", "Author") 'add all Column header that you want to check

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

使用Application.WorksheetFunction.Match查找您要查找的名稱的列號。 然后對列進行檢查。

下面是一個例子:

Option Explicit

Public Sub ValidateData()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet")

    Dim ColumnNames() As Variant
    ColumnNames = Array("Filename", "FileNumber", "Author") 'add all Column header that you want to check

    Dim Headers As Variant 'read all headers into an array
    Headers = ws.Range("A1", ws.Cells(1, ws.Columns.Count).End(xlToLeft)).Value

    Dim HeaderColumn As Long 'this is the column number where the header was found

    Dim ColName As Variant
    For Each ColName In ColumnNames 'loop through your list of names
        HeaderColumn = 0 'initialize
        On Error Resume Next 'next line throws error if it does not match
        HeaderColumn = Application.WorksheetFunction.Match(ColName, Headers, 0)
        On Error GoTo 0 're-activate error reporting

        If HeaderColumn <> 0 Then
            'header name was found
            MsgBox ColName & " Column found"

            'perform different checks on each column
            Select Case ColName
            Case "FileNumber"
                CheckFileNumberColumn ws.Range(ws.Cells(2, HeaderColumn), ws.Cells(ws.Rows.Count, HeaderColumn).End(xlUp))

            'Case "Author"  'add  other cases as needed
                'CheckAuthorColumn ws.Range(ws.Cells(2, HeaderColumn), ws.Cells(ws.Rows.Count, HeaderColumn).End(xlUp))

            End Select
        Else
            'header name was not found
            MsgBox ColName & " Column not found"
        End If
    Next ColName
End Sub

'this is the procedure to check the FileNumber column
Private Sub CheckFileNumberColumn(DataToValidate As Range)
    Dim iRow As Long
    For iRow = 1 To DataToValidate.Rows.Count
        If DataToValidate.Cells(iRow, 1).Value <> 101 Then
            DataToValidate.Cells(iRow, 1).Interior.Color = RGB(255, 0, 0)
        End If
    Next iRow
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM