我有一个表(称为PortDistances
),显示端口对之间的距离。我已将其设置为不显示总计行,因为它与其中的数据无关。
我还有一个UserForm,可以使用以下代码将一行数据添加到表的末尾:
Private Sub AddOne_OK_Click()
Dim lrow As Long
lrow = Worksheets("Port distances").Cells(Rows.Count, 1).End(xlUp).Row
' Add new port pair distance
Worksheets("Port distances").Cells(lrow + 1, 1).Formula = "=" & Worksheets("Port distances").Cells(lrow + 1, 2).Address(False, False) & "&" & Worksheets("Port distances").Cells(lrow + 1, 3).Address(False, False) ' Extend formulas
Worksheets("Port distances").Cells(lrow + 1, 6).Formula = "=" & Worksheets("Port distances").Cells(lrow + 1, 1).Address(False, False) & "&" & Worksheets("Port distances").Cells(lrow + 1, 4).Address(False, False) ' Extend formulas
Worksheets("Port distances").Cells(lrow + 1, 2).Value = strFrom ' POL
Worksheets("Port distances").Cells(lrow + 1, 3).Value = strTo ' POD
Worksheets("Port distances").Cells(lrow + 1, 4).Value = Val(Box_DistNew.Value) ' Distance
Worksheets("Port distances").Cells(lrow + 1, 5).Value = "Schedule App" ' Source
Unload Me
End Sub
令人讨厌的是,每次运行它时,添加的行都会变成总行,通常会问我是否要覆盖现有的总行公式(请记住, 没有总计从一行开始。)
我尝试添加Worksheets("Port distances").ListObjects("PortDistances").ShowTotals = False
将其重新设置为法线,但这只是擦除了那一行。另一方面,手动清除“Totals row”复选框只会将其变为法线。
为什么将它作为总计行,我该如何阻止它?
答案 0 :(得分:2)
您可以使用Worksheets("Port distances").ListObjects("PortDistances").Listrows.add
添加新行,然后填充它。例如:
Dim lo As ListObject
Dim lr As ListRow
Dim n As Long
Set lo = Worksheets("Port distances").ListObjects("PortDistances")
Set lr = lo.ListRows.Add
For n = 1 To lo.ListColumns.Count
lr.Range(1, n).Value = "item " & n
Next n