【问题标题】:Set cell color in Excel based on text value - VBA根据文本值在 Excel 中设置单元格颜色 - VBA
【发布时间】:2017-06-05 00:50:48
【问题描述】:

我正在编写一个脚本来在机器上运行ping。该脚本查找具有主机名的文本文件,并在 A 列中返回主机名,并在 B 列中返回 ping 的状态(向上或向下)。

如果向上,我需要将 B 列的颜色更改为绿色,如果向下,则更改为红色。

没有问题的代码:

'# call excel applicationin visible mode

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True

objExcel.Workbooks.Add

intRow = 2


'# Define Labels 

objExcel.Cells(1, 1).Value = "Machine Name"

objExcel.Cells(1, 2).Value = "Results"


'# Create file system object for reading the hosts from text file


Set Fso = CreateObject("Scripting.FileSystemObject")

Set InputFile = fso.OpenTextFile("MachineList.Txt")

'# Loop thru the text file till the end 

Do While Not (InputFile.atEndOfStream)

HostName = InputFile.ReadLine

'# Create shell object for Pinging the host machines

Set WshShell = WScript.CreateObject("WScript.Shell")

Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)


objExcel.Cells(intRow, 1).Value = HostName

'# use switch case for checking the machine updown status

Select Case Ping

Case 0 objExcel.Cells(intRow, 2).Value = "up"

Case 1 objExcel.Cells(intRow, 2).Value = "down"

End Select


intRow = intRow + 1

Loop

'# Format the excel

objExcel.Range("A1:B1").Select

objExcel.Selection.Interior.ColorIndex = 19

objExcel.Selection.Font.ColorIndex = 11

objExcel.Selection.Font.Bold = True

objExcel.Cells.EntireColumn.AutoFit

我尝试过的颜色(很多问题):

Sub ColorCells()
     Dim cel As Range
     Application.ScreenUpdating = False
     Application.EnableEvents = False
     For Each cel In Range("B2:B90")
         Select Case LCase(Left(cel.Value, 1))
             Case "up"
                 cel.Interior.Color = vbGreen
             Case Else
                 cel.Interior.ColorIndex = xlColorIndexNone
         End Select
     Next cel
     Application.EnableEvents = True
     Application.ScreenUpdating = True
 End Sub

我得到了什么:

我想要什么:

【问题讨论】:

  • 我马上就看到你的代码不是以Sub End Sub 开头或结尾的,除了Sub ColorCells() 所以它甚至在我这边都不起作用。您还想做的是在使用Range 时引用Sheet,否则我相信它会在活动工作表上执行操作。您是否遗漏了一些代码?
  • @Jean-PierreOosthuizen 没有遗漏任何代码。我刚刚拼凑了一些东西,这是我最接近实现目标的方式。我是 VBA 新手。
  • 你的代码有很多问题,甚至很难知道从哪里开始 Select Case LCase(Left(cel.Value, 1)) Case "Down" 如果你小写一个值,它永远不会等于“Down”,大写 D SO 从小处开始,逐行运行代码并添加内容
  • Case LCase(Left(cel.Value, 1)) 只会检查第一个字母。尝试Case LCase(Left(cel.Value, 2)) 检查前两个字母。
  • 你可以做的是在Case 0Case 1添加颜色变化线,所以基本上它会添加“向上”或“向下”文本,然后直接改变单元格的颜色后。否则,如果只是为了视觉指示,您可以为那些查看内容的单元格设置条件格式,然后相应地更改为颜色。

标签: vba excel colors


【解决方案1】:

要在插入“向上”或“向下”时更改单元格的颜色,只需将 Select Case Ping 替换为以下内容:

Select Case Ping

Case 0
    objExcel.Cells(intRow, 2).Value = "up"
    objExcel.Cells(intRow, 2).Interior.Color = vbGreen
Case 1
    objExcel.Cells(intRow, 2).Value = "down"
    objExcel.Cells(intRow, 2).Interior.Color = vbRed

End Select

【讨论】:

    【解决方案2】:
    Select Case Ping
    
    Case 0 objExcel.Cells(intRow, 2).Value = "up"
        objExcel.Cells(intRow, 2).Interior.Color = vbGreen
    
    Case 1 objExcel.Cells(intRow, 2).Value = "down"
        objExcel.Cells(intRow, 2).Interior.Color = vbRed
    
    End Select
    

    这将是如何在 select Case 语句中实现颜色。请看下面我对With 的使用,因为它比使用.Select 更好(而不是问题中的最后几行代码):

    With objExcel.Range("A1:B1")
        .Interior.ColorIndex = 19
        .Font.ColorIndex = 11
        .Font.Bold = True
    End With
    
    objExcel.Cells.EntireColumn.AutoFit
    

    【讨论】: