对象'_Global'的运行时错误'1004''范围'失败

时间:2017-01-19 08:40:12

标签: excel vba excel-vba

enter image description here

执行命令时出现此错误。

我尝试过解决但仍然有同样的错误。我知道如何解决这个问题。

一段代码

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

1 个答案:

答案 0 :(得分:1)

运行此修改后的代码后:

return result.reduce(function(val)) {

我发现在活动工作表与范围 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""type"之前,请确保将有效工作表设置为imin所在的工作表,或者使用col等。