2017-01-20 20 views
2

機械にpingを実行するスクリプトを作成しています。このスクリプトは、私は、列Bの色が最大場合は緑に変更し、必要な列Bにテキスト値に基づいてExcelでセルの色を設定する - VBA

をホスト名を持つテキストファイルに検索し、&列にping(UpまたはDown)の状況をホスト名を返します。ダウンすれば赤。

問題なくコード:私は色(多くの問題)のために試してみました何

'# 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 

私は何を得る:

What I get

私が欲しいもの:

What I want

+2

:それは(代わりに、問題のコードのあなたの最終ラインの).Selectを使用するよりもはるかに良い方法があるようもののWithの私の使用で下記をご覧'Sub'' End Sub'を除いて' Sub ColorCells() 'を除いて、私の側では動作しません。あなたがしたいことは、 'Range'を使うときに' Sheet'を参照することです。さもなければ、それはアクティブシート上でアクションを実行すると信じています。あなたが残したコードはありますか? –

+0

@ Jean-PierreOosthuizenコードは抜き去られていません。私はちょうど一緒に物事をつなぎ、これは私が目標を達成するために持っている最も近いです。私はVBAの初心者です。 – Vandal

+3

コードには多くの問題があり、開始する場所が分からない場合があります ケースを選択LCase(左(cel.Value、1)) ケース「ダウン」 値を小文字にすると決して "ダウン "と首都D SOは小さく、行ごとに開始し、コードを実行して内容を追加します – dgorti

答えて

4

あなたが「アップ」または「ダウン」を挿入すると、単に次のように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 
0
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 objExcel.Range("A1:B1") 
    .Interior.ColorIndex = 19 
    .Font.ColorIndex = 11 
    .Font.Bold = True 
End With 

objExcel.Cells.EntireColumn.AutoFit 
関連する問題