请有人告诉我下面的代码在哪里出错了?
Dim lastcol as Long, endrow as Long
lastcol = Cells(3, Column.Count).End(xlToLeft).Column
endrw = Cells(Rows.Count, “B”).End(xlUp).Row
Range(“D4:” & Cells(endrow, lastcol)).Select
这是导致`错误的最后一行。 排行找到第44行 lastcol查找列N
答案 0 :(得分:1)
注意;您将End Row变量声明为endrw
,然后将其用作endrow
,并且还写了Column
而不是Columns
。
在最后一行中,您尝试从预定义的单元格(D4
)创建范围,并将其扩展到新标识的单元格。目前,您正在尝试将字符串"D4"
与单元格的Range对象相连。试一试;
Range("D4:" & Cells(endrow, lastcol).Address(RowAbsolute:=False, ColumnAbsolute:=False)).Select
甚至更好;
Range(Cells(4,4), Cells(endrow, lastcol)).Select
第一个将新单元格转换为字符串地址,第二个仅使用D4
的Range对象(单元格)代替字符串地址。
总而言之,它应该看起来像这样;
Sub Test()
Dim lastcol As Long, endrow As Long
lastcol = Cells(3, Columns.Count).End(xlToLeft).Column
endrow = Cells(Rows.Count, 2).End(xlUp).Row
Range(Cells(4, 4), Cells(endrow, lastcol)).Select
End Sub
答案 1 :(得分:0)
此行:Range(“D4:” & Cells(endrow, lastcol)).Select
,更确切地说,该语句“D4:” & Cells(endrow, lastcol)
将D4:
与值为Cells(endrow, lastcol)
的串联在一起,因此可能是任何东西(为此感到很幸运)形成有效范围:))。
可以使用单元格来指定范围,也可以通过传递定义范围的字符串来指定范围,但不得将两者混合使用(只要该单元格不包含其他单元格地址即可)。
所以您应该写:Range(Cells(4, 4), Cells(endrow, lastcol)).Select
答案 2 :(得分:0)
您的代码中有几个错误:
尝试使用此变体:
Dim lastcol As Long, endrow As Long
lastcol = Cells(3, Columns.Count).End(xlToLeft).Column
endrw = Cells(Rows.Count, "B").End(xlUp).Row
Range(Cells(4, 4), Cells(endrw, lastcol)).Select