将内容从一张纸复制到另一张时出错

时间:2014-07-03 14:21:12

标签: excel vba excel-vba

我正在尝试将工作表CurrentDistribution与工作表distribution1连接起来。为此,我查看distribution1的最后一行,并在最后一行之后复制CurrentDistribution的内容。

LastCellCurrentDistribution = FindLastCell(CurrentDistribution)
LastCellDistribution1 = FindLastCell("distribution1")
LastRowInDistribution1 = Right(LastCellDistribution1, Len(LastCellDistribution1) - 1)
Sheets(CurrentDistribution).Range("A4:" + LastCellCurrentDistribution).Copy Sheets("distribution1").Range("A" + CStr((CInt(LastRowInDistribution1) + 1))) 'i only wany the row (number), not the column (letter)
Sheets(CurrentDistribution).Delete

现在问题是我尝试这样做时出现此错误:'Run-time error '6': Overflow'。我使用调试器,发现它的第4行复制了抛出此错误的东西。任何想法是什么问题以及如何解决它?

2 个答案:

答案 0 :(得分:1)

我想你需要声明变量LastCellCurrentDistribution LastCellDistribution1 LastRowInDistribution1,因为整数不能处理大于32,000的值。

尝试声明如下:

Dim LastCellCurrentDistribution As Long
Dim LastCellDistribution1 As Long
Dim LastRowInDistribution1 As Long

答案 1 :(得分:0)

看起来我的工作表太大了,INT类型无法处理我的最后一个单元格号。所以替换:

Sheets(CurrentDistribution).Range("A4:" + LastCellCurrentDistribution).Copy Sheets("distribution1").Range("A" + CStr((CInt(LastRowInDistribution1) + 1))) 'i only wany the row (number), not the column (letter)

使用

Sheets(CurrentDistribution).Range("A4:" + LastCellCurrentDistribution).Copy Sheets("distribution1").Range("A" + CStr(LastRowInDistribution1 + 1)) 'i only wany the row (number), not the column (letter)