【问题标题】:Check for a specific column name given a string then highlight values in the column that doesn't match given value in VBA检查给定字符串的特定列名,然后突出显示列中与 VBA 中给定值不匹配的值
【发布时间】:2019-03-14 00:17:45
【问题描述】:

如果列名“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

【问题讨论】:

  • 为什么需要在 VBA 中执行此操作?这只是条件格式。如果它必须是VBA。宏记录自己进行条件格式并根据需要进行编辑。但是请注意上面的帖子,我们不是来为您工作的
  • 上面试过了,不好意思忘了加代码
  • 使用 Match 返回列号,然后参考下面的范围并计算 101 的数量或任何你想要的。

标签: excel vba


【解决方案1】:

使用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

【讨论】:

  • @user3677042 如果这解决了您的问题,请将其标记为解决方案。
猜你喜欢
  • 2011-09-04
  • 2014-04-19
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多