我正在編寫腳本在機器上運行ping
。該腳本看起來與主機名的文本文件,並返回列的主機名,如果一個&的ping
的基於文本值在Excel中設置單元格顏色 - VBA
我需要的列B顏色的狀態(向上或向下)在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新手。 – Vandal
您的代碼有很多問題,很難甚至知道從哪裏開始 Select Case LCase(Left(cel.Value,1)) Case「Down」 如果您的小寫字母的值不會等於「向下「與大寫D SO開始小,逐行並運行代碼並添加東西 – dgorti