错误1004“工作表”的默认“范围”失败

大家好,我似乎无法解决这个错误。

错误在线: lastrmale = malesheet.Range("A" & malesheet.Range("A1").SpecialCells(xlLastCell).Row + 1).End(xlUp).Row

上面的代码在列A,K和M上search匹配的“男性”和“女性”。如果所有3列匹配,则来自两张纸的行被复制并粘贴在“混合”纸上。

有谁知道这个解决scheme? 任何帮助表示赞赏!

 Dim lastr As Long Dim lastrmale As Long Dim lastrfemale As Long Dim lastrmix As Long Dim malesheet As Worksheet Dim Femalesheet As Worksheet Dim mixsheet As Worksheet Dim i As Long Set malesheet = Worksheets("Male") Set Femalesheet = Worksheets("Female") Set mixsheet = Worksheets("mix") lastrmale = malesheet.Range("A" & malesheet.Range("A1").SpecialCells(xlLastCell).Row + 1).End(xlUp).Row lastrfemale = Femalesheet.Range("A" & Femalesheet.Range("A1").SpecialCells(xlLastCell).Row + 1).End(xlUp).Row lastr = WorksheetFunction.Min(lastrmale, lastrfemale) lastrmix = 2 For i = 2 To lastr If (malesheet.Range("A" & i).value = Femalesheet.Range("A" & i).value) And (malesheet.Range("K" & i).value = Femalesheet.Range("K" & i).value) And (malesheet.Range("M" & i).value = Femalesheet.Range("M" & i).value) Then malesheet.Rows(i & ":" & i).Copy mixsheet.Range("A" & lastrmix).PasteSpecial xlPasteAll lastrmix = lastrmix + 1 Femalesheet.Rows(i & ":" & i).Copy mixsheet.Range("A" & lastrmix).PasteSpecial xlPasteAll lastrmix = lastrmix + 1 End If Next 

爆炸的线是一个任务。 该赋值的右边是一个值expression式 ; 值expression式可以很长,只要你能做到,VBA就不会感觉到任何事情。 但时间越长,他们越难以阅读,维护和修复,

 lastrmale = malesheet.Range("A" & malesheet.Range("A1").SpecialCells(xlLastCell).Row + 1).End(xlUp).Row 

在这个expression中,你有不less于6个成员的电话:

  • malesheet.Range("A" & ...)
  • malesheet.Range("A1")
  • [Range].SpecialCells(xlLastCell)
  • [Range].Row
  • [Range].End(xlUp)
  • [Range].Row

使自己成为一个接受RangeWorksheet的函数,并返回一个代表最后一行的Long

现在你有:

 lastrmale = malesheet.Range("A" & FindLastRow(malesheet.Range("A1")) + 1).Row 

这也可能是:

 Dim rename_me As Long rename_me = FindLastRow(malesheet.Range("A1") lastrmale = malesheet.Range("A" & rename_me + 1).Row 

这看起来很傻,因为你之后的行号只是rename_me + 1

如错误状态,您的范围定义是错误的。

看看这一行是一个正确的例子:

 If (malesheet.Range("A" & i).value = ... 

注意范围将parsing为A1,A2,…等

对于抛出错误的行,该范围指的是什么单元格?

 malesheet.Range("A" & malesheet.Range("A1").SpecialCells(xlLastCell).Row + 1).End(xlUp).Row 

尝试分解该操作的部分,以确认值是如预期的那样。 设置断点或将值写入其他单元进行debugging:例如

 Dim temp = xlLastCell '(does this value exist) Dim temp2 = xlUp '(does this value exist) Dim temp3 = malesheet.Range("A1").SpecialCells(xlLastCell).Row '(does this return an int) Dim temp4 = malesheet.Range("A1").End(xlUp).Row '(does this work) Dim temp5 = malesheet.Range("A" & 1 + 1) '(does this work) 

一旦确认了基础值,您可以对值进行硬编码以进行范围testing。

我相信下面的代码,你试图找出最后一行。 但我认为这是行不通的

 lastrmale = malesheet.Range("A" & malesheet.Range("A1").SpecialCells(xlLastCell).Row + 1).End(xlUp).Row 

它是假设的

 lastrmale = malesheet.cells.SpecialCells(xlLastCell).Row + 1 

顺便说一句,单元格或范围(“A1”)都是相同的

 malesheet.cells.SpecialCells(xlLastCell).Row + 1 = malesheet.Range("A1").SpecialCells(xlLastCell).Row + 1 

或者我最爱的方法

 lastrmale = malesheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row