cha*_*zjo 3 vba named-ranges microsoft-excel-2010
在 Excel 2010 中,我需要逐行浏览数据表并在 VBA 中操作数据。
我原来的函数是这样的:
Private Function GetValue(ByVal myRange As Excel.Range, ByVal strCol As String) As String
Dim myCell As Object
Set myCell = myRange.Cells(, strCol)
GetValue = myCell.Value
End Function
Run Code Online (Sandbox Code Playgroud)
我这样称呼它:
GetValue(myRow, "AE")
Run Code Online (Sandbox Code Playgroud)
myRow是表示一行的 Excel.Range。
"AE"是列索引。
我想转换为使用列名而不是列索引,因为用户将来可能会选择添加或删除列。我将一系列单元格标识为 Excel 中的表格,命名该表格,并选择唯一的列名。
这意味着我现在将使用以下方法调用该函数:
GetValue(myRow, "column_name")
Run Code Online (Sandbox Code Playgroud)
但我找不到可以仅指定列名而不指定行的示例。
这甚至可行吗?
其他答案中的代码没有为我编译,但它允许我做一些进一步的研究。
灵感来自如何使用他的姓名和列引用循环 excel 2010 表? 以及如何遍历表并按列标题访问行项目?,我最终使用了:
Private Function GetValue(ByVal myRange As Excel.Range, ByVal strCol As String) As String
GetValue = myRange.Columns(myRange.ListObject.ListColumns(strCol).Range.Column).Value
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29843 次 |
| 最近记录: |