Excel 2010 VBA Range 没有 select 更正命名 table 中的数据
Excel 2010 VBA Range doesn't select correct data out of a named table
我尝试从命名的 运行ge 中 select 多列。我用一些变量构建了应该 selected 的字符串:
Dim onecolumn as Integer, lastcolumn as Integer
Dim tblTable as ListObject
Dim strforrange as String
Set tblTable = Sheet1.ListObjects("Table1")
onecolumn = 3
lastcolumn = tblTable.Range.Columns.Count
strforrange = tblTable & "[[#All],[Column" & onecolumn & "]:[Column" & lastcolumn - 1 & "]]"
Range(strforrange).Select
我运行 代码一步一步检查 "strforrange" 变量。即使它是正确的,selected 列也与字符串中所述的不同。
是否有任何其他方法来获取命名 table 的 运行ge,或者有人知道解决方案吗?
以下内容应该有所帮助;我已经包含了一个有用的函数的引用,该函数将列号转换为相应的列字母(来源:Function to convert column number to letter? @brettdj 的回答)
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
使用此函数,您只需将 DataBodyRange
函数与 Columns
一起使用即可 select 所需范围:
Sub SelectSpecificDataRange
Dim startColumn as Long, lastColumn as Long
Dim tblTable as ListObject
Dim startColumnLetter, lastColumnLetter as String
Set tblTable = Sheet1.ListObjects("Table1")
startColumn = 3
startColumnLetter = Col_Letter(startColumn)
lastColumn = tblTable.Range.Columns.Count
lastColumnLetter = Col_Letter(lastColumn)
tblTable.DataBodyRange.Columns(startColumnLetter & ":" & lastColumnLetter).Select
End Sub
我尝试从命名的 运行ge 中 select 多列。我用一些变量构建了应该 selected 的字符串:
Dim onecolumn as Integer, lastcolumn as Integer
Dim tblTable as ListObject
Dim strforrange as String
Set tblTable = Sheet1.ListObjects("Table1")
onecolumn = 3
lastcolumn = tblTable.Range.Columns.Count
strforrange = tblTable & "[[#All],[Column" & onecolumn & "]:[Column" & lastcolumn - 1 & "]]"
Range(strforrange).Select
我运行 代码一步一步检查 "strforrange" 变量。即使它是正确的,selected 列也与字符串中所述的不同。
是否有任何其他方法来获取命名 table 的 运行ge,或者有人知道解决方案吗?
以下内容应该有所帮助;我已经包含了一个有用的函数的引用,该函数将列号转换为相应的列字母(来源:Function to convert column number to letter? @brettdj 的回答)
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
使用此函数,您只需将 DataBodyRange
函数与 Columns
一起使用即可 select 所需范围:
Sub SelectSpecificDataRange
Dim startColumn as Long, lastColumn as Long
Dim tblTable as ListObject
Dim startColumnLetter, lastColumnLetter as String
Set tblTable = Sheet1.ListObjects("Table1")
startColumn = 3
startColumnLetter = Col_Letter(startColumn)
lastColumn = tblTable.Range.Columns.Count
lastColumnLetter = Col_Letter(lastColumn)
tblTable.DataBodyRange.Columns(startColumnLetter & ":" & lastColumnLetter).Select
End Sub