如何使用VBA更新Excel表,同时维护表的公式

我有一个电子表格,它定义了几个Excel表格,其中包括源数据和使用源数据的函数。 例如,名为MyTbl的表由三个“源列”和一个公式列组成

Part Price Qty Extended Price ------ ----- --- -------------- Shoe 5.50 10 =(MyTbl[[#ThisRow],[Price]])*(MyTbl[[#ThisRow],[Qty]]) ... 

定期地,来源列必须由CSV文件中的值replace,但公式列应保持不受影响。 该文件是这样的:

 Shoe,5.65,98 Coat,12.65,223 ... 

如何在维持Excel表格及其公式的存在的同时“刷新”源列? 我想在VBA中这样做,因为CSV文件没有一致的名称或path。

提前致谢。

感谢Doug Glancy的build议。

最初的问题是将.csv加载到listobject中。 我结束了从csv的数据加载到一个新的工作表listobject。 然后,我可以将源列表对象合并到目标列表对象中,同时使用以下过程维护目标所维护的额外列(和公式)。 此过程将清除不需要的数据,如果源包含的行比目标更less,则删除行,复制数据并调整listobject的大小。

这个解决scheme的假设
– 源列表对象包含less于目标的列数
– 这两个列表对象都包含一个标题行(我没有testing任何东西,只有一个标题行)

Private Sub CopyTableData(loSource As Excel.ListObject, loTarget As Excel.ListObject)
Dim lSourceRowCount As Long

 With loTarget If .DataBodyRange.Rows.Count <> loSource.DataBodyRange.Rows.Count Then ' clear of target area should clear target num rows and source num columns .Range(.Cells(1,1).Address, .Cells(.DataBodyRange.Rows.Count, lSource.RefersToRange.Columns.Count)).Clear ' clear rows if source has less than target If .DataBodyRange.Rows.Count > loSource.DataBodyRange.Rows.Count Then For i = .DataBodyRange.Rows.Count To loSource.DataBodyRange.Rows.Count + 1 Step -1 .DataBodyRange.Rows(i).Clear Next i End If ' resize list object lSourceRowCount = loSource.HeaderRowRange.Rows.Count + _ loSource.DataBodyRange.Rows.Count .Resize .Range.Cells(1).Resize(lSourceRowCount, .Range.Columns.Count) End If loSource.DataBodyRange.Copy .DataBodyRange.Cells(1) End With 

结束小组

再次感谢。