VBA代码-Excel 选中单元格区域并复制

时间:2026-02-13 03:46:31

1、说明:

1、将从数据库中取到的数据放到AG4单元格;

2、选中AG4单元格的区域,并复制;

3、将复制的单元格区域转置并放到H2单元格;

4、清除AG4单元格区域;

2、代码:

    Cells(4, "AG").CopyFromRecordset cnn.Execute(sq1)


    Range("AG4").CurrentRegion.Select
    Selection.Copy
    Cells(2, "H").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
    Range("AG4").CurrentRegion.ClearContents

3、解释:

1、sq1是SQL语句;

     Cells(4, "AG").CopyFromRecordset cnn.Execute(sq1) 'cnn.execute(sq1)是执行数据库命令。整个语句是将执行数据库命令得到的结果放到AG4单元格

© 2026 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com