0
我有一個工作子例程,它將值插入到單元格中,作爲表的新行的一部分。我引用單元格將數據插入列號。不過,我希望能夠使用表格中的列/標題名稱,而不是數字,以便可以移動表格列而不會擾亂宏中的單元格引用。有誰知道如何做到這一點?如何通過列名引用表中的新單元格
這是我的示例代碼。
Sub InsertValue(ByRef iTargetCell As Integer, ByRef CellData As String, ByRef strType As String)
Select Case strType
' Define the data as Text
Case "T"
' Specify the number format to be used for a particular column in the new row
NewRow.Range.Cells(1, iTargetCell).NumberFormat = "@"
' Insert the data for a particular column into the new row
NewRow.Range.Cells(1, iTargetCell).Value = CellData
' Define the data as a number
Case "N"
NewRow.Range.Cells(1, iTargetCell).NumberFormat = "0"
NewRow.Range.Cells(1, iTargetCell).Value = CellData
End Select
End Sub
變量NewRow已經公開定義。
Public NewRow As ListRow
它被設置在另一個子程序中。
Set NewRow = Range(strTableName).ListObject.ListRows.Add(AlwaysInsert:=True)
三江源返回與標題 「COLNAME」 的列的位置。在前面的子程序中,我已經設置了一個變量(Tbl)來保存表ListObject,所以我可以修改我的子程序來創建一個新的變量(iColNum)來保存列號,然後使用'iColNum = Tbl。 ListColumns(strTargetCell).Index' – Dominic