簡體   English   中英

運行時錯誤 '1004' 'Range' of object'_Global' 失敗

[英]Run-time error '1004' 'Range' of object'_Global' failed

在此處輸入圖片說明

執行命令時出現此錯誤。

我試過解決,但仍然有同樣的錯誤。 任何想法我該如何解決這個問題。

一段代碼

Dim col As Long

col = Range("type").Column 'this line produces an error

這是完整的代碼:

Sub refresh()

Dim imin As Long, col As Long, imax As Long, column As Long, numRow As Long
Dim myLine As Integer

myLine = 2
numRow = Sheets("merge").Range("A" & Rows.Count).End(xlUp).Row

If numRow > myLine Then

    imin = Range("type").Row + 1
    col = Range("type").Column

    With Sheets("temp")
        .Cells.Delete
        If Sheets("merge").Range("A" & Rows.Count).End(xlUp).Row >= myLine Then
            Sheets("merge").Range("C" & myLine & ":E" & Sheets("merge").Range("E" & Rows.Count).End(xlUp).Row).Copy .Range("A1")
        End If

        Application.CutCopyMode = False

        For column = 1 To 3
            If .Range(Fct_LetCol(column) & Rows.Count).End(xlUp).Row > 1 Then
                .Range(.Cells(1, column), .Cells(.Range(Fct_LetCol(column) & Rows.Count).End(xlUp).Row, col)).RemoveDuplicates Columns:=1, header:=xlNo
                imax = .Range(Fct_LetCol(column) & Rows.Count).End(xlUp).Row
                tridata Sheets("temp"), Range(Fct_LetCol(column) & "1:" & Fct_LetCol(column) & imax), Range(Fct_LetCol(column) & "1:" & Fct_LetCol(column) & imax), xlAscending, xlSortNormal, xlGuess
            End If
        Next column
    End With

    With Sheets("selection")
        imax = .UsedRange.Rows.Count + .UsedRange.Row - 1
        If imax >= imin Then .Rows(imin & ":" & imax).Delete
        Sheets("temp").Range("A1:A" & Sheets("temp").Range("A" & Rows.Count).End(xlUp).Row).Copy .Cells(imin, col)
        Sheets("temp").Range("B1:B" & Sheets("temp").Range("B" & Rows.Count).End(xlUp).Row).Copy .Cells(imin, col + 2)
        Sheets("temp").Range("C1:C" & Sheets("temp").Range("C" & Rows.Count).End(xlUp).Row).Copy .Cells(imin, col + 4)
        Application.CutCopyMode = False
        imax = .UsedRange.Rows.Count + .UsedRange.Row - 1
    End With

    Sheets("temp").Cells.Delete
    Sheets("selection").Activate
    Application.Calculation = xlAutomatic

Else
    msg = MsgBox("Error", 64, "Error")
End If   

End Sub

運行此修改后的代碼后:

 Sub refresh()

Dim imin As Long, col As Long, imax As Long, numRow As Long
Dim myLine As Integer

myLine = 2
numRow = Sheets("merge").range("A" & Rows.Count).End(xlUp).Row

If numRow > myLine Then

imin = range("type").Row + 1
col = range("type").Column

With Sheets("selection")
    imax = .UsedRange.Rows.Count + .UsedRange.Row - 1
    If imax >= imin Then .Rows(imin & ":" & imax).Delete
    Sheets("temp").range("A1:A" & Sheets("temp").range("A" & Rows.Count).End(xlUp).Row).Copy .Cells(imin, col)
    Sheets("temp").range("B1:B" & Sheets("temp").range("B" & Rows.Count).End(xlUp).Row).Copy .Cells(imin, col + 2)
    Sheets("temp").range("C1:C" & Sheets("temp").range("C" & Rows.Count).End(xlUp).Row).Copy .Cells(imin, col + 4)
    Application.CutCopyMode = False
    imax = .UsedRange.Rows.Count + .UsedRange.Row - 1
End With

Sheets("temp").Cells.Delete
Sheets("selection").Activate
Application.Calculation = xlAutomatic

Else
  msg = MsgBox("Error", 64, "Error")
End If

End Sub

我發現在活動工作表與"type"范圍所在的工作表不同的情況下運行該過程會導致"type"范圍內的引用錯誤(檢查您的名稱管理器)。 因此,請確保在為imincol分配值之前將活動工作表設置為"type"所在的工作表,或者使用imin = Sheets("RelevantSheetName").Range("type").Row + 1等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM