简体   繁体   English

运行时错误1004对象全局的方法范围失败

[英]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.

相关问题 “对象_global的运行时错误1004方法范围失败” VBA错误 - “runtime error 1004 method range of object _global failed” VBA error vba excel:对象_global的运行时错误1004方法范围失败 - vba excel : runtime error 1004 method range of object _global failed 运行时错误&#39;1004&#39;:对象&#39;_global&#39;的方法&#39;range&#39;失败 - runtime error '1004': method 'range' of object '_global' failed 单元格的引用范围生成对象“ _global”的运行时错误1004方法“ range” - Referencing range of cells generates Runtime error 1004 method 'range' of object '_global' failed 如何纠正此“对象_global失败的运行时错误1004方法范围”错误? - How can I correct this “runtime error 1004 method range of object _global failed” error? VBA运行时错误1004的object_global范围失败 - VBA Runtime error 1004 range of object_global failed VBA运行时错误1004:对象&#39;_global&#39;的方法&#39;Range&#39; - VBA runtime error 1004: Method 'Range' of object'_global' 宏运行时错误 1004:object '_global' 的方法 'Cells' 失败 - Macros Runtime Error 1004: Method 'Cells' of object '_global' failed 运行时错误&#39;1004&#39;:,对象&#39;_Global&#39;的方法&#39;Intersect&#39;失败 - Runtime Error '1004':, Method 'Intersect' of object '_Global' failed 对象 _Global 的运行时错误 1004 方法相交失败 - Runtime Error 1004 Method Intersect' of object _Global failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM