本文介绍了我看到的是运行时错误消息"1004",的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在代码中的lastrow = Sheet1.Cells(Rows.Count, 1).End(x1Up).Row
处看到运行时错误1004":
I am seeing "Run-time Error 1004" at lastrow = Sheet1.Cells(Rows.Count, 1).End(x1Up).Row
in my code:
Sub copycolumns()
Dim lastrow As Long, erow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(x1Up).Row
For i = 2 To lastrow
If Sheet1.Cells(i, 6) = "Algiers" Then
Sheet1.Cells(i, 1).Copy
erow = Sheet2.Cells(Rows.Count, 1).End(x1Up).Offset(1, 0).Row
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 1)
Sheet1.Cells(i, 3).Copy
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 2)
Sheet1.Cells(i, 6).Copy
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 3)
End If
Next i
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
Range("A1").Select
End Sub
你能帮忙吗?
推荐答案
您需要一个工作表对象,并且xlUp
不等于x1Up
(您的代码有错字,即数字1).
You need a worksheet object, and xlUp
is not equal to x1Up
(your code has a typo i.e. digit 1).
在行下使用
lastrow = ThisWorkbook.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
代替
lastrow = Sheet1.Cells(Rows.Count, 1).End(x1Up).Row
完整代码
Sub RoundedRectangle2_Click()
Dim lastrow, erow As Long
lastrow = ThisWorkbook.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
If Sheet1.Cells(i, 6) = "Algiers" Then
Sheet1.Cells(i, 1).Copy
erow = ThisWorkbook.Worksheets("sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 1)
Sheet1.Cells(i, 3).Copy
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 2)
Sheet1.Cells(i, 6).Copy
Sheet1.Paste Destination:=Worksheets("Sheet2").Cells(erow, 3)
End If
Next i
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
Range("A1").Select
End Sub
或者,您可以使用下面的行查找行数
Alternatively, you can use below line to find the rows count
rows_count = WorksheetFunction.CountA(ThisWorkbook.Worksheets("Sheet1").Range("A:A"))
这篇关于我看到的是运行时错误消息"1004",的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!