[英]runtime error 1004 method range of object global failed
Hello I need help I am getting an error in my code in this line of the code 您好,我需要帮助,我在此行代码中遇到错误
Range(copyRngStart & copyRngEnd).Copy Destination:=Worksheets("Display").Range("A" & lr2 + 1)
runtime error 1004 method range of object global failed 运行时错误1004对象全局的方法范围失败
could someone please take a look to fix it, thanks. 有人可以看一下修复它,谢谢。
Sub Distinct()
Dim lr2 As Long
Dim searchRng As Range, copyRngStart As Range, copyRngEnd As Range
Set searchRng = Worksheets("Information").Range("A1")
lr2 = Sheets("Display").Cells(Rows.Count, "A").End(xlUp).Row
' Enter/continue loop while A-column is non-empty
Do While searchRng.Value <> ""
' When we encounter the string TRNS in column A and Triumph Foods LLC in column E
If searchRng.Value = "TRNS" And searchRng.Offset(0, 4).Value = "Triumph Foods LLC" Then
' Set the start of the copy area
Set copyRngStart = searchRng
End If
' When we encounter the string ENDTRNS
If searchRng.Value = "ENDTRNS" Then
' .. set the end of the copy area
Set copyRngEnd = searchRng.Offset(-1, 5)
' Copy and paste
Range(copyRngStart & copyRngEnd).Copy Destination:=Worksheets("Display").Range("A" & lr2 + 1)
lr2 = Sheets("Display").Cells(Rows.Count, "A").End(xlUp).Row
End If
' Increment search loop
Set searchRng = searchRng.Offset(1, 0)
Loop
End Sub
Sub Distinct()
Const TRNS_START As String = "TRNS"
Const TRNS_END As String = "ENDTRNS"
Const COMPANY As String = "Triumph Foods LLC"
Dim searchRng As Range, copyRngStart As Range, copyRngEnd As Range
Set searchRng = Worksheets("Information").Range("A1")
' Enter/continue loop while A-column is non-empty
Do While searchRng.Value <> ""
' When we encounter the string TRNS in column A and Triumph Foods LLC in column E
If searchRng.Value = TRNS_START And _
searchRng.Offset(0, 4).Value = COMPANY Then
Set copyRngStart = searchRng ' Set the start of the copy area
End If
' When we encounter the string ENDTRNS
' (*and had a start cell already*)
If searchRng.Value = TRNS_END And Not copyRngStart Is Nothing Then
Set copyRngEnd = searchRng.Offset(-1, 5)
copyRngEnd.Worksheet.Range(copyRngStart, copyRngEnd).Copy _
Destination:=Sheets("Display").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
Set copyRngStart = Nothing 'clear the "start" range
End If
Set searchRng = searchRng.Offset(1, 0)
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.