我收到这里提到的错误:
...从这段代码中...
...并且您将看到,lastrow和lastcol的单元格变量报告正常,那么为什么会出现此错误?
我也尝试过调整它,但是仍然是同样的问题。
基本上,我只是尝试使用由上面两个变量确定的单元格值将Rg设置为范围。使用Excel 2016 VBA。谢谢。明天再回来。
答案 0 :(得分:2)
范围是一个对象,因此您需要使用Set命令而不是仅用于标准变量(例如Sting,Integer等)的'='。因此在您的示例中:
Set Rg = Range(Cells(1, 1), Cells(lastRow, LastCol))
答案 1 :(得分:1)
请在下面找到如何设置范围:
Sub test()
Dim rng_Single As Range, rng_Union As Range
'Refer to the sheet you want to work with
With ThisWorkbook.Worksheets("Sheet1")
'Use the "." before range or cell in order to refer to the worksheet mention in the with statement
'Set the range.
Set rng_Single = .Range(.Cells(1, 1), .Cells(1, 5))
'Merge two ranges of the same worksheet
Set rng_Union = Union(.Range(.Cells(3, 1), .Cells(3, 5)), .Range(.Cells(4, 1), .Cells(4, 5)))
End With
End Sub