此解决方案创建一个名为“PhoneLog” 的工作表来保存“From web” 函数的累积结果。
此过程假定 “From web” 函数的结果位于名为 “WebFrom” 的工作表中,范围为 A:E,从第 1 行开始(根据需要更改)
此过程必须位于保存“来自网络”函数结果的同一工作簿中。
第一次在更新“From web”功能之前运行这个程序,以便将实际结果添加到“PhoneLog”中。此后,在 “From web” 函数之后立即运行此过程。
如果在工作簿中找不到 “PhoneLog” 工作表,此过程将创建它。然后它将 “WebFrom” 工作表中的所有新记录添加到 “PhoneLog”(根据需要更改)。
Option Explicit
Sub Phone_Log()
Const kWebFrom As String = "WebFrom" 'change as required
Const kPhoneLog As String = "PhoneLog" 'change as required
Dim wshWeb As Worksheet, wshLog As Worksheet
Dim blwshNew As Boolean
Dim rWeb As Range, rLog As Range
Dim aWeb As Variant, vItm As Variant
Dim lRow As Long, l As Long
Rem Set Worksheets
With ThisWorkbook
Set wshWeb = .Worksheets(kWebFrom)
On Error Resume Next
Set wshLog = .Worksheets(kPhoneLog)
On Error GoTo 0
If wshLog Is Nothing Then
blwshNew = True
Set wshLog = .Worksheets.Add(After:=wshWeb)
wshLog.Name = kPhoneLog
End If: End With
Rem Set FromWeb Array
With wshWeb
If Not (.AutoFilter Is Nothing) Then .Cells(1).AutoFilter
Set rWeb = .Cells(1).CurrentRegion
End With
With rWeb
.AutoFilter Field:=1, Criteria1:="<>"
Set rWeb = .Cells.SpecialCells(xlCellTypeVisible)
aWeb = .Offset(1).Resize(-1 + .Rows.Count).SpecialCells(xlCellTypeVisible).Value2
.AutoFilter
End With
Rem Set Log Array
With wshLog
If blwshNew Then
Rem Set Log - First Time
rWeb.Copy
.Cells(1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
.Cells(1).CurrentRegion.Columns.AutoFit
Else
Rem Add New Records into Log Range
Set rLog = .Cells(1).CurrentRegion
With rLog
lRow = .Rows.Count
For l = 1 To UBound(aWeb)
vItm = WorksheetFunction.Index(aWeb, l, 0)
'Use this line if running the "FromWeb" function for one IP address only
'If WorksheetFunction.CountIfs(.Columns(1), vItm(1), _
.Columns(2), vItm(2), .Columns(5), vItm(5)) = 0 Then
'Use this line if running the "FromWeb" function for several IP addresses
If WorksheetFunction.CountIfs(.Columns(1), vItm(1), _
.Columns(2), vItm(2), .Columns(4), vItm(4), .Columns(5), vItm(5)) = 0 Then
lRow = 1 + lRow
.Rows(lRow).Value = vItm
End If: Next: End With
Rem Format Log Range
Set rLog = .Cells(1).CurrentRegion
With rLog
.Rows(2).Copy
.Offset(1).Resize(-1 + .Rows.Count).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
.Columns.AutoFit
End With
Rem Sort Log Range
With .Sort
.SortFields.Clear
.SortFields.Add Key:=rLog.Columns(1), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
.SortFields.Add Key:=rLog.Columns(2), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
'Use also this line if running the "FromWeb" function for several IP addresses
.SortFields.Add Key:=rLog.Columns(4), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SetRange rLog
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With: End If: End With
End Sub
建议阅读以下页面以更深入地了解所使用的资源:
Excel Objects,
For Each...Next Statement,
If...Then...Else Statement,
On Error Statement,
Option Explicit Statement,
Range Object (Excel),
Range.CurrentRegion Property (Excel),
Range.Offset Property (Excel),
Range.PasteSpecial Method (Excel),
Range.SpecialCells Method (Excel),
Using Arrays,
Variables & Constants,
With Statement,
Workbook Object (Excel),
Worksheet.AutoFilter Property (Excel),
Worksheet.Sort Property (Excel),
WorksheetFunction Object (Excel).