Office中国论坛/Access中国论坛

标题: 新建sheet并拷贝,代码出错求救 [打印本页]

作者: qiaonation    时间: 2008-12-12 05:38
标题: 新建sheet并拷贝,代码出错求救
原始数据在一个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
作者: liwen    时间: 2008-12-12 15:53
去掉引用值前后的"号试试(未测试)
Sheets("all").Rows(iBegin & ":" & iEnd).Copy Sheets(Cells(K, i)).Rows(2)
作者: 方漠    时间: 2008-12-12 20:11
Try.......

Dim Shname as string
SHname=Cells(K, i).value   
Sheets("all").Rows(iBegin & ": " &iEnd).Copy Sheets(ShName).Rows(2)

[ 本帖最后由 方漠 于 2008-12-12 20:12 编辑 ]




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3