Vba 当宏在一张工作表中运行时,将数据复制到另一张工作表

Vba 当宏在一张工作表中运行时,将数据复制到另一张工作表,vba,excel,macros,Vba,Excel,Macros,我正在为ping测试仪编写代码 在表1中,它持续对设备进行ping操作,并在B列中显示ping时间。当任何设备无法访问时,它在下一列中显示上次ping时间和无法访问的持续时间。但当该设备变得可访问时,它会将可访问的持续时间(报告)发送到下一页,并开始显示该设备可访问 我想在sheet1中运行宏时打开报告工作表 如果我使用select(如代码中所示),它将强制我输入sheet1,但如果我打开sheet2,则ping时间将开始在sheet2中输入 Sub Do_ping() With Acti

我正在为ping测试仪编写代码

在表1中,它持续对设备进行ping操作,并在B列中显示ping时间。当任何设备无法访问时,它在下一列中显示上次ping时间和无法访问的持续时间。但当该设备变得可访问时,它会将可访问的持续时间(报告)发送到下一页,并开始显示该设备可访问

我想在sheet1中运行宏时打开报告工作表

如果我使用select(如代码中所示),它将强制我输入sheet1,但如果我打开sheet2,则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
Sub Do_ping()
使用Active工作簿。工作表(1)
工作表(“表1”)。选择
行=2
做
如果.Cells(第1行)为“”,则
如果不可连接(.Cells(row,1),2100)=True,则
工作表(“表1”)。选择
如果单元格(第3行).Value=nul,则
单元格(第1行).Interior.Color=RGB(0,255,0)
单元格(第1行)。Font.FontStyle=“bold”
单元格(第1行)。Font.Size=14
单元格(第2行).Interior.Color=RGB(0,255,0)
单元格(第2行)。值=时间
其他的
工作表(“表1”)。选择
单元格(第1行)。复制图纸(“图纸2”)。范围(“A”和行数)。结束(xlUp)。偏移量(1,0)
单元格(第2行)。复制图纸(“图纸2”)。范围(“B”和行数)。结束(xlUp)。偏移量(1,0)
单元格(第5行)。复制图纸(“图纸2”)。范围(“c”和行数)。结束(xlUp)。偏移量(1,0)
单元格(第1行).Interior.Color=RGB(0,255,0)
单元格(第1行)。Font.FontStyle=“bold”
单元格(第1行)。Font.Size=14
单元格(第2行).Interior.Color=RGB(0,255,0)
单元格(第2行)。值=时间
单元格(第5行)。ClearContents
如果结束
“鸣笛
其他:
'单元格(第2行)。公式=“=NOW()-”&CDbl(NOW())
'单元格(第1行).Interior.Color=RGB(255,0,0)
工作表(“表1”)。选择
单元格(第3行)。Value=DateDiff(“d”,单元格(第2行),Now())
“时差。首先在单元格中设置格式。
单元格(第4行)。NumberFormat=“hh:mm:ss”
“/计算并更新
单元格(第4行)。Value2=Now()-单元格(第2行)
单元格(第5行)。值=小时(单元格(第4行)。值2)*3600+分钟(单元格(第4行)。值2)*60+秒(单元格(第4行)。值2)
如果单元格(第5行).Value>120,则
工作表(“表1”)。选择
单元格(第1行)。Interior.ColorIndex=3
单元格(第2行)。Interior.ColorIndex=3
单元格(第3行)。Interior.ColorIndex=3
单元格(第4行)。Interior.ColorIndex=3
其他的
工作表(“表1”)。选择
单元格(第1行)。Interior.ColorIndex=40
单元格(第2行)。Interior.ColorIndex=40
单元格(第3行)。Interior.ColorIndex=40
单元格(第4行)。Interior.ColorIndex=40
如果结束
如果结束
如果结束
行=行+1
循环直到。单元格(第1行)=“”
以
端接头

您应该去掉代码中的
选择
,更好地利用

假设工作簿中的第一张工作表是“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
Sub Do_ping()
带工作表(“表1”)
行=2
做
如果.Cells(第1行)为“”,则
如果不可连接(.Cells(row,1),2100)=True,则
如果.Cells(第3行).Value=nul,那么变量“nul”是否已定义?
.Cells(第1行).Interior.Color=RGB(0,255,0)
.Cells(第1行)。Font.FontStyle=“bold”
.Cells(第1行)。Font.Size=14
.Cells(第2行).Interior.Color=RGB(0,255,0)
.单元格(第2行)。值=时间
其他的
。单元格(第1行)。复制图纸(“图纸2”)。范围(“A”和图纸(“图纸2”)。行。计数)。结束(xlUp)。偏移量(1,0)
。单元格(第2行)。复制图纸(“图纸2”)。范围(“B”和图纸(“图纸2”)。行。计数)。结束(xlUp)。偏移量(1,0)
。单元格(第5行)。复制图纸(“图纸2”)。范围(“c”和图纸(“图纸2”)。行。计数)。结束(xlUp)。偏移量(1,0)
.Cells(第1行).Interior.Color=RGB(0,255,0)
.Cells(第1行)。Font.FontStyle=“bold”
.Cells(第1行)。Font.Size=14
.Cells(第2行).Interior.Color=RGB(0,255,0)
.单元格(第2行)。值=时间
.单元格(第5行).清除内容
如果结束
“鸣笛
其他的
'单元格(第2行)。公式=“=NOW()-”&CDbl(NOW())
'单元格(第1行).Interior.Color=RGB(255,0,0)
.Cells(第3行).Value=DateDiff(“d”,.Cells(第2行),Now())
“时差。首先在单元格中设置格式。
.Cells(第4行).NumberFormat=“hh:mm:ss”
“/计算并更新
.Cells(第4行).Value2=Now()-.Cells(第2行)
.Cells(第5行).值=小时(.Cells(第4行).值2)*3600+分钟(.Cells(第4行).值2)*60+秒(.Cells(第4行).值2)
如果.Cells(第5行).Value>120,则
.Cells(第1行).Interior.ColorIndex=3
.Cells(第2行).Interior.ColorIndex=3
.Cells(第3行).Interior.ColorIndex=3
.Cells(第4行).Interior.ColorIndex=3
其他的
.Cells(第1行).Interior.ColorIndex=40
.Cells(第2行).Interior.ColorIndex=40
.Cells(第3行).Interior.ColorIndex=40
.Cells(第4行).Interior.ColorIndex=40
如果结束
如果结束
如果结束
行=行+1
循环直到。单元格(第1行)=“”
以
端接头
注意:我强烈建议您将
选项Explicit
作为所有代码模块的第一行-我怀疑您的变量
nul
应该是
Null
,并且
 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