VBA:范围方法失败

时间:2016-07-11 11:04:27

标签: excel vba excel-vba

我正在编写一个代码来选择一个范围内我打开的工作簿的所有数据条目,但是编译器在最后一行给出错误(设置范围rng)

Dim wb As Workbook
Set wb = Workbooks.Open(Range("C2") & Range("C3"))
'here Range("C2") & Range("C3") contains the location of the file's path
Dim ws As Worksheet
Set ws = wb.ActiveSheet
Dim frow As Long
frow = ws.Range("A" & Rows.count).End(xlUp).Row
Dim rng As Range
Dim frow1 As Long
frow1 = ws.Cells(1, Columns.count).End(xlToLeft).Column
Set rng = wb.ActiveSheet.Range(Cells(1, 1), Cells(frow, frow1))

1 个答案:

答案 0 :(得分:2)

尝试:

Dim frow As Long
frow = ws.Range("A" & ws.Rows.count).End(xlUp).Row
Dim rng As Range
Dim fcol As Long
fcol = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(frow, fcol))

请记住,如果您使用的是集合工作表,则必须在所有范围对象中引用它