我有一个代码,我可以使用一些帮助。我写的这个脚本是为了将第一行到最后一行的范围复制到另一个工作表。如果有超过2行包含数据,它可以正常工作,但如果只有1行(第一行),它会忽略该数据,不复制它,并输出错误。
有什么可以识别可能修复此代码的内容吗?我无休止地搜索没有结果。谢谢!
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Set ws1 = Worksheets("HCA")
Set ws2 = Worksheets("FormA")
Set ws3 = Worksheets("NamedRange")
ws3.Range("T1:U1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("FormA").Select
ws2.Range("AQ7").PasteSpecial
答案 0 :(得分:4)
End(xlDown)
会产生不良影响。使用工作表底部的xlUp
代替
Sub test()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Set ws1 = Worksheets("HCA")
Set ws2 = Worksheets("FormA")
Set ws3 = Worksheets("NamedRange")
With ws3
.Range(.Range("T1"), .Cells(.Rows.Count, "U").End(xlUp)).Copy
End With
ws2.Range("AQ7").PasteSpecial
End Sub
答案 1 :(得分:2)
替换,
ws3.Range("T1:U1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
...用,
with ws3
.Range(.cells(1, "T"), .cells(.rows.count, "U").end(xlup)).copy
end with
在寻找最后一个非空白单元时,从下往上看;不是自上而下。