我正在尝试使用第1行中已使用列中的所有单元格值创建一个变量。
Dim vaData As Variant
With wsSheet
vaData = .Range("B1:" & .Range(.Columns.Count & "1").End(xlRight).Column).Value
End With
我收到范围失败错误。
我已成功完成此操作以获取列BK中的值。
vaData = .Range(.Range("BK2"), .Range("BK100").End(xlUp)).Value
答案 0 :(得分:1)
试试这个:
Dim vaData As Variant
With wsSheet
vaData = .Range("B1", .Cells(1, .Columns.Count).End(xlToLeft))
End With
问题是你可能在范围对象和单元格属性之间感到困惑。
请参阅下面的范围对象语法我经常使用:
Range("A1", "A10")
Range("A1:A10")
Range(Cells(1, 1), Cells(10, 1))
首先评估范围A1:A10 。现在如何让它变得动态?
<强>示例:强>
动态上一行
With Sheet1
.Range("A1", .Range("A" & .Rows.Count).End(xlUp)) '~~> dynamic last row
.Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row) '~~> same as above
.Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) '~~> same as above
End With
动态上一栏
With Sheet1
.Range("A1", .Cells(1, .Columns.Count).End(xlToLeft)) '~~> same as my answer
.Range(.Cells(1, 1), .Cells(1, .Columns.Count).End(xlToLeft)) '~~> same as above
End With
顺便说一下,another more complex approach is found here using Find Method.
同样的方法was used here to find the last column
您也可以查看different ways on using Cells Property.
答案 1 :(得分:0)
我的第一个猜测是,在第一个示例中,您缺少列字母,即公式将评估为
vaData =.Range("B1:10").Value 'Error!
而不是
vaData =.Range("B1:B10").Value 'Ok!
希望这会有所帮助......