【问题标题】:VBA Format cell fill color based on content file type基于内容文件类型的 VBA 格式单元格填充颜色
【发布时间】:2017-06-25 23:29:34
【问题描述】:

我正在尝试读取 Excel 列中的文档列表,并根据文件类型更改单元格的颜色。但我做不到。有什么解决办法吗?

Public Sub Master()

       Dim TdCel As Range, FCell As Range

       Set TdCel = Range("A1:A25")

       For Each FCell In TdCel

          If FCell.Text = "*.pdf" Then
                FCell.Interior.ColorIndex = 10
            ElseIf FCell.Value = "*.*.doc" Then
                FCell.Interior.ColorIndex = 9
            ElseIf FCell.Value = "*.jpg" Then
                FCell.Interior.ColorIndex = 8
            Else
                FCell.Interior.Color = vbWhite
          End If
       Next

    End Sub

【问题讨论】:

  • 您遇到什么错误?是什么导致了这个问题?作为最初的建议,我建议使用 instr,并坚持使用 .value(这是默认属性,因此对 state 来说是多余的)。用法是 if instr(1,fcell,".pdf") > 0 then
  • 你为什么不直接使用Conditional Formatting

标签: vba excel colors conditional-formatting


【解决方案1】:

一些改进:

  1. 定义您工作的工作表(在Set wS = ... 行中更改工作表名称
  2. 使用With
  3. 在小写单元格的值 (LCase()) 上使用 Select CaseLike

工作解决方案:

Public Sub Master_JoaoTS()
    Dim wS As Worksheet
    Dim TdCel As Range, FCell As Range, CellVal As String
    Set wS = ThisWorkbook.Sheets("Sheet's Name")
    Set TdCel = wS.Range("A1:A25")

    For Each FCell In TdCel
        With FCell
            CellVal = LCase(.Value)
            With .Interior
                Select Case True
                    Case CellVal Like "*.pdf"
                        .ColorIndex = 10
                    Case CellVal Like "*.doc*"
                        .ColorIndex = 9
                    Case CellVal Like "*.jpg"
                        .ColorIndex = 8
                    Case Else
                        .Pattern = xlNone
                End Select
            End With '.Interior
        End With 'FCell
    Next FCell
End Sub

【讨论】:

    【解决方案2】:

    编辑来缩短代码(并使@R3uK更不受欢迎......)

    你可以使用Switch()函数

    Public Sub Master_JoaoTS()
        Dim FCell As Range
        Dim docType As String
        Dim clrIndex As Variant
    
        For Each FCell In Worksheets("myWorksheetName").Range("A1:A25").SpecialCells(xlCellTypeConstants, xlTextValues)
            With FCell
                docType = LCase(Right(.Value, Len(.Value) - InStrRev(.Value, ".")))
                clrIndex = Switch(docType = "pdf", 10, _
                                 docType = "doc", 9, _
                                 docType = "jpg", 8)
                If IsNull(clrIndex) Then clrIndex = 2
                .Interior.ColorIndex = clrIndex
            End With
        Next FCell
    End Sub
    

    【讨论】:

    • 不错!没见过,有时候用得着! :)
    • @R3uK,经过编辑使它变得更好......希望你能欣赏它!
    • 我喜欢它! :) 但我不能再投票了!顺便说一句,如果你在 Word 中有一些概念(虽然不是 VBA ......):stackoverflow.com/questions/42116944/…
    • @R3uK,我会尽快看看。虽然不承诺任何结果
    • 别担心,这不是那么紧急,但我讨厌这个词,尤其是当事情应该正常工作但他们很痛苦的时候!^^
    【解决方案3】:

    这是一个与原始代码相距不远的解决方案,使用 InStr 函数查找子字符串“.pdf”、“.doc”和“.jpg”。

    Public Sub Master()
    
      Dim TdCel As Range, FCell As Range
    
      Set TdCel = Range("A1:A25")
    
      For Each FCell In TdCel
    
        If InStr(FCell, ".pdf") Then
          FCell.Interior.ColorIndex = 10
        ElseIf InStr(FCell, ".doc") Then
          FCell.Interior.ColorIndex = 9
        ElseIf InStr(FCell, ".jpg") Then
          FCell.Interior.ColorIndex = 8
        Else
          FCell.Interior.Color = vbWhite
        End If
    
      Next
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多