MS Excel,宏附加额外字符

MS Excel,宏附加额外字符

使用宏,我使用一个简单的函数将两组独立的列用冒号连接起来,例如 =A2&":"&B2

一切正常,直到最后一行,宏才会添加两个额外的单元格,每个单元:格中都只有 。看起来宏自动选择了包含数据的字段,并将空单元格内容与 连接起来:

我检查了宏好几次,但还是不知道是什么原因造成的。有人能帮我检查一下吗?

Sub NetworkStatisticsFilter()
'

' NetworkStatisticsFilter Macro
' Format and Filter Data from Get-NetStatTCP to simplify port discovery
'

'
'Suppress alerts
'
    Application.DisplayAlerts = False

'
'Delete empty header rows that resulted from Get-NetStatTCP export
'
    Rows("1:1").Select
    Selection.Delete Shift:=xlUp
    Rows("2:2").Select
    Selection.Delete Shift:=xlUp

'
'Text-to-Columns, space-delimited
'
    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), _
    TrailingMinusNumbers:=True

'
'Join LocalAddress & LocalPort with a ":" then copy results and replace both columns w/a paste-value
'
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "SRC"
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]&"":""&RC[-1]"
    Range("C2").Select
    Selection.AutoFill Destination:=Range("C2:C77")
    Range("C2:C77").Select
    Selection.Copy
    Range("D2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Columns("A:C").Select
    Range("C1").Activate
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft

'
'Join RemoteAddress & RemotePort with a ":" then copy results and replace both columns w/a paste-value
'
    Columns("D:D").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "DST"
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]&"":""&RC[-1]"
    Range("D2").Select
    Selection.AutoFill Destination:=Range("D2:D77")
    Range("D2:D77").Select
    Selection.Copy
    Range("E2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Columns("B:D").Select
    Range("D1").Activate
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft

'
'Auto-fit columns
'
    Columns("A:D").Select
    Columns("A:D").EntireColumn.AutoFit

'
'Auto-filter loopback address/port combinations and delete resulting rows
'
    Columns("A:A").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$A$79").AutoFilter Field:=1, Criteria1:="=127.0.0.1*", Operator:=xlFilterValues
    ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
    ActiveSheet.ShowAllData

'
'Remove duplicate values in DST column
'
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$C$67").RemoveDuplicates Columns:=2, Header:=xlYes
    ActiveWindow.SmallScroll Down:=-9

'
'Allow Alerts
'
    Application.DisplayAlerts = True


End Sub

答案1

您使用 复制列中的公式Range.AutoFill()。因此,如果宏在错误的单元格中创建公式,则意味着您提供的范围是错误的。例如,您正在使用Range("C2:C77"),而真正的范围可能是。Range("C2:C76")

您可以使用以下命令找到某个范围内最后使用的行(此处为 C 列):

LastRow = Range("C" & Rows.Count).End(xlUp).Row

此外,当您删除空标题时,您将:

(a)删除第 1 行

(b)删除执行(a)之前的第 2 行,即第 3 行。

这是故意的吗?如果你想删除第一行和第二行,你可以这样做:

Rows("1:2").EntireRow.Delete

编辑:

因为您确实想删除第 1 行和第 3 行:

Rows(1).EntireRow.Delete
Rows(2).EntireRow.Delete

相关内容