运行时错误'1004'object'_Global'的'Range'失败

在这里输入图像说明

我执行我的命令时遇到此错误。

我试过解决,但仍然有同样的错误。 任何想法我怎么能解决这个问题。

一段代码

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"范围内的引用错误(检查您的名称pipe理器)。 因此,请务必在将值分配给imincol之前将活动工作表设置为"type"所在的值,或者使用imin = Sheets("RelevantSheetName").Range("type").Row + 1