VBA:使用“单元格”选择范围

时间:2019-04-23 09:17:56

标签: excel vba

我试图在Excel中简化VBA项目的代码,并且在设置范围时遇到问题:

Sub Test()
Dim lRow As Long, lCol As Long
Dim TSheet As Variant, TSheets As Variant
Dim rng As Range

TSheets = Array("Sheet1", "Sheet2", "Sheet3")

For Each TSheet in TSheets
lRow = ThisWorkbook.Sheets(TSheet).Cells(Rows.Count, "B").End(xlUp).Row + 1
lCol = ThisWorkbook.Sheets(TSheet).Cells(lRow - 1, "B").End(xlToRight).Column
Set rng = ThisWorkbook.Sheets(TSheet).Range(Cells(2, 2), Cells(lRow, lCol))
Next TSheet

End Sub

我收到错误1004“应用程序定义的错误或对象定义的错误”。 Set rng = ThisWorkbook.Sheets(TSheet).Cells(lRow, lCol)有效,因此Range(Cells(),Cells())语法必须存在问题。你能帮我吗?

edit:感谢GSerg(非常感谢!),我能够正常工作,关键是我必须写Set rng = ThisWorkbook.Sheets(TSheet).Range(ThisWorkbook.Sheets(TSheet).Cells(2, 2), ThisWorkbook.Sheets(TSheet).Cells(lRow, lCol))。但是,我仍然不明白为什么我的原始代码收到错误消息。 cells()是否应该自动引用我正在处理的单张纸?

0 个答案:

没有答案