标题: 请教,拷贝行代码出错 [打印本页] 作者: qiaonation 时间: 2008-12-12 05:38 标题: 请教,拷贝行代码出错 原始数据在一个sheet("all")里,按年份(列K)排序。现在要:
1.新建sheet,以年份命名;
2.将all里的各行,按照年份,拷贝到相应的新建sheet中去,
写了一段代码,运行时总是出现错误,还请高手指教。万分感谢!
Sub Macro1()
Dim i As Long, iBegin As Long, iEnd As Long
i = 2
iBegin = 2
While Cells(K, i) <> ""
If (Cells(K, i) <> Cells(K, i + 1)) Then
iEnd = i
Sheets.Add(after:=Sheets(Sheets.Count)).Name = Cells(K, i) ‘新建sheet并命名
Sheets("all").Rows(1).Copy Sheets("Cells(K, i)").Rows(1) ’拷贝标题行
Sheets("all").Rows("iBegin:iEnd").Copy Sheets("Cells(K, i)").Rows(2) ‘拷贝年份相同的行
iBegin = i + 1
End If
i = i + 1
Wend
End Sub作者: pureshadow 时间: 2008-12-12 23:29
Sheets("Cells(K, i)")这里不需要引号
另外还要看CELLS(K,I)里的值和工作表名是否一致