2017-01-07 9 views
-1

私はpingテスターのコードを書いています。マクロが1枚のシートで実行されている間に他のシートにデータをコピーします。

1枚目では、デバイスにpingを継続し続け、B列にping時間を表示します。デバイスが到達不能になると、最後のping時間と到達不能時間が次の列に表示されます。しかし、そのデバイスが到達可能になると、到達可能性(レポート)の持続時間を次のシートに送信し、そのデバイスが到達可能であることを示すことを開始する。

マクロがsheet1で実行されているときにレポートシートを開きたいとします。

select(コードのように)を使用している場合はsheet1に強制されますが、これがなければsheeet2を開くとping時間がsheet2に入力され始めます。

Sub Do_ping() 

    With ActiveWorkbook.Worksheets(1) 
    Worksheets("sheet1").Select 

    row = 2 
    Do 
     If .Cells(row, 1) <> "" Then 
     If IsConnectible(.Cells(row, 1), 2, 100) = True Then 
      Worksheets("sheet1").Select 
      If Cells(row, 3).Value = nul Then 
      Cells(row, 1).Interior.Color = RGB(0, 255, 0) 
      Cells(row, 1).Font.FontStyle = "bold" 
      Cells(row, 1).Font.Size = 14 
      Cells(row, 2).Interior.Color = RGB(0, 255, 0) 
      Cells(row, 2).Value = Time 
      Else 
      Worksheets("sheet1").Select 
      Cells(row, 1).copy Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) 
      Cells(row, 2).copy Sheets("sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1, 0) 
      Cells(row, 5).copy Sheets("sheet2").Range("c" & Rows.Count).End(xlUp).Offset(1, 0) 
      Cells(row, 1).Interior.Color = RGB(0, 255, 0) 
      Cells(row, 1).Font.FontStyle = "bold" 
      Cells(row, 1).Font.Size = 14 
      Cells(row, 2).Interior.Color = RGB(0, 255, 0) 
      Cells(row, 2).Value = Time 
      Cells(row, 5).ClearContents 
      End If 
      'Call siren 
     Else: 
      'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now()) 
      'Cells(Row, 1).Interior.Color = RGB(255, 0, 0) 
      Worksheets("sheet1").Select 
      Cells(row, 3).Value = DateDiff("d", Cells(row, 2), Now()) 
      'Time Difference. First set the format in cell. 
      Cells(row, 4).NumberFormat = "hh:mm:ss" 
      '/calculate and update 
      Cells(row, 4).Value2 = Now() - Cells(row, 2) 
      Cells(row, 5).Value = Hour(Cells(row, 4).Value2) * 3600 + Minute(Cells(row, 4).Value2) * 60 + Second(Cells(row, 4).Value2) 
      If Cells(row, 5).Value > 120 Then 
      Worksheets("sheet1").Select 
      Cells(row, 1).Interior.ColorIndex = 3 
      Cells(row, 2).Interior.ColorIndex = 3 
      Cells(row, 3).Interior.ColorIndex = 3 
      Cells(row, 4).Interior.ColorIndex = 3 
      Else 
      Worksheets("sheet1").Select 
      Cells(row, 1).Interior.ColorIndex = 40 
      Cells(row, 2).Interior.ColorIndex = 40 
      Cells(row, 3).Interior.ColorIndex = 40 
      Cells(row, 4).Interior.ColorIndex = 40 
      End If 
     End If 

     End If 
     row = row + 1 
    Loop Until .Cells(row, 1) = "" 
    End With 
End Sub 
+0

)コードと、その作業 サブDo_pingを(変更あなたはマクロが別のワークシートに実行されている間だけのSheet2を開いているしたいですか?またはSheet1でマクロが実行されているときにSheet2でいくつかの「手動」アクションを実行できるようにしたいですか? – Rufus

+0

@Rufus sheet1と2.sheet2の間を切り替えるとデバイスの到達不能レポートのようになります。到達不能の詳細が表示されます –

+3

['.Select' \' .Activate'を使用しない方法(https ://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)それをできるだけ早く適用してください。 – BruceWayne

答えて

0

あなたのコード内でSelectを取り除き、そしてWithブロックをより有効に活用する必要があります。

ブックの最初のシートが "Sheet1"であると仮定すると、次のコードはコードのリファクタリング版であり、Selectステートメントを取り除きます。

Sub Do_ping() 

    With Worksheets("Sheet1") 
    row = 2 
    Do 
     If .Cells(row, 1) <> "" Then 
     If IsConnectible(.Cells(row, 1), 2, 100) = True Then 
      If .Cells(row, 3).Value = nul Then ' has the variable "nul" been defined? 
      .Cells(row, 1).Interior.Color = RGB(0, 255, 0) 
      .Cells(row, 1).Font.FontStyle = "bold" 
      .Cells(row, 1).Font.Size = 14 
      .Cells(row, 2).Interior.Color = RGB(0, 255, 0) 
      .Cells(row, 2).Value = Time 
      Else 
      .Cells(row, 1).copy Sheets("sheet2").Range("A" & Sheets("sheet2").Rows.Count).End(xlUp).Offset(1, 0) 
      .Cells(row, 2).copy Sheets("sheet2").Range("B" & Sheets("sheet2").Rows.Count).End(xlUp).Offset(1, 0) 
      .Cells(row, 5).copy Sheets("sheet2").Range("c" & Sheets("sheet2").Rows.Count).End(xlUp).Offset(1, 0) 
      .Cells(row, 1).Interior.Color = RGB(0, 255, 0) 
      .Cells(row, 1).Font.FontStyle = "bold" 
      .Cells(row, 1).Font.Size = 14 
      .Cells(row, 2).Interior.Color = RGB(0, 255, 0) 
      .Cells(row, 2).Value = Time 
      .Cells(row, 5).ClearContents 
      End If 
      'Call siren 
     Else 
      'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now()) 
      'Cells(Row, 1).Interior.Color = RGB(255, 0, 0) 
      .Cells(row, 3).Value = DateDiff("d", .Cells(row, 2), Now()) 
      'Time Difference. First set the format in cell. 
      .Cells(row, 4).NumberFormat = "hh:mm:ss" 
      '/calculate and update 
      .Cells(row, 4).Value2 = Now() - .Cells(row, 2) 
      .Cells(row, 5).Value = Hour(.Cells(row, 4).Value2) * 3600 + Minute(.Cells(row, 4).Value2) * 60 + Second(.Cells(row, 4).Value2) 
      If .Cells(row, 5).Value > 120 Then 
      .Cells(row, 1).Interior.ColorIndex = 3 
      .Cells(row, 2).Interior.ColorIndex = 3 
      .Cells(row, 3).Interior.ColorIndex = 3 
      .Cells(row, 4).Interior.ColorIndex = 3 
      Else 
      .Cells(row, 1).Interior.ColorIndex = 40 
      .Cells(row, 2).Interior.ColorIndex = 40 
      .Cells(row, 3).Interior.ColorIndex = 40 
      .Cells(row, 4).Interior.ColorIndex = 40 
      End If 
     End If 

     End If 
     row = row + 1 
    Loop Until .Cells(row, 1) = "" 
    End With 
End Sub 

注:私は強くあなたがすべてのあなたのコードモジュールの最初の行としてOption Explicitを含めることをお勧めします - 私はあなたの変数nulNullであるべきであり、Option Explicitの使用は、エラーの種類を強調するだろうと思われます。

0

私は

With Worksheets("Sheet1") 


    row = 2 
    Do 
     If .Cells(row, 1) <> "" Then 
     If IsConnectible(.Cells(row, 1), 2, 100) = True Then 
     'Worksheets("sheet1").Select 
     If Cells(row, 3).Value = nul Then 
     Sheets("sheet1").Cells(row, 1).Interior.Color = RGB(0, 255, 0) 
     Sheets("sheet1").Cells(row, 1).Font.FontStyle = "bold" 
     Sheets("sheet1").Cells(row, 1).Font.Size = 14 
     Sheets("sheet1").Cells(row, 2).Interior.Color = RGB(0, 255, 0) 
     Sheets("sheet1").Cells(row, 2).Value = Time 
     Else 
     'Worksheets("sheet1").Select 
     Sheets("sheet1").Cells(row, 1).copy Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) 
     Sheets("sheet1").Cells(row, 2).copy Sheets("sheet2").Range("B" & Rows.Count).End(xlUp).Offset(1, 0) 
     Sheets("sheet1").Cells(row, 5).copy Sheets("sheet2").Range("c" & Rows.Count).End(xlUp).Offset(1, 0) 
     Sheets("sheet1").Cells(row, 1).Interior.Color = RGB(0, 255, 0) 
     Sheets("sheet1").Cells(row, 1).Font.FontStyle = "bold" 
     Sheets("sheet1").Cells(row, 1).Font.Size = 14 
     Sheets("sheet1").Cells(row, 2).Interior.Color = RGB(0, 255, 0) 
     Sheets("sheet1").Cells(row, 2).Value = Time 
     Sheets("sheet1").Cells(row, 5).ClearContents 
     End If 
     'Call siren 
     Else: 
     'Cells(Row, 2).Formula = "=NOW()-" & CDbl(Now()) 
     'Cells(Row, 1).Interior.Color = RGB(255, 0, 0) 
     'Worksheets("sheet1").Select 
     Sheets("sheet1").Cells(row, 3).Value = DateDiff("d", Cells(row, 2), Now()) 
    'Time Difference. First set the format in cell. 
    Sheets("sheet1").Cells(row, 4).NumberFormat = "hh:mm:ss" 
    '/calculate and update 
    Sheets("sheet1").Cells(row, 4).Value2 = Now() - Cells(row, 2) 
    Sheets("sheet1").Cells(row, 5).Value = Hour(Cells(row, 4).Value2) * 3600 + Minute(Cells(row, 4).Value2) * 60 + Second(Cells(row, 4).Value2) 
    If Cells(row, 5).Value > 120 Then 
    'Worksheets("sheet1").Select 
    Sheets("sheet1").Cells(row, 1).Interior.ColorIndex = 3 
    Sheets("sheet1").Cells(row, 2).Interior.ColorIndex = 3 
    Sheets("sheet1").Cells(row, 3).Interior.ColorIndex = 3 
    Sheets("sheet1").Cells(row, 4).Interior.ColorIndex = 3 
    Else 
    'Worksheets("sheet1").Select 
    Sheets("sheet1").Cells(row, 1).Interior.ColorIndex = 40 
    Sheets("sheet1").Cells(row, 2).Interior.ColorIndex = 40 
    Sheets("sheet1").Cells(row, 3).Interior.ColorIndex = 40 
    Sheets("sheet1").Cells(row, 4).Interior.ColorIndex = 40 
    End If 
     End If 

     End If 
     row = row + 1 
    Loop Until .Cells(row, 1) = "" 
    End With 
End Sub 

Function IsConnectible(sHost, iPings, iTO) 
    ' Returns True or False based on the output from ping.exe 
    ' sHost is a hostname or IP 
    ' iPings is number of ping attempts 
    ' iTO is timeout in milliseconds 
    ' if values are set to "", then defaults below used 

    Dim nRes 
    If iPings = "" Then iPings = 1 ' default number of pings 
    If iTO = "" Then iTO = 550  ' default timeout per ping 
    With CreateObject("WScript.Shell") 
    nRes = .Run("%comspec% /c ping.exe -n " & iPings & " -w " & iTO _ 
      & " " & sHost & " | find ""TTL="" > nul 2>&1", 0, True) 
    End With 
    IsConnectible = (nRes = 0) 

End Function 
関連する問題