1:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
必须注意
(1)要求目标表Table2必须存在,并且字段field,field2...也必须存在
(2)注意Table2的主键约束,如果Table2有主键而且不为空,则 field1, field2...中必须包括主键
(3)注意语法,不要加values,和插入一条数据的sql混了,不要写成:
Insert into Table2(field1,field2,...) values (select value1,value2,... from Table1)
(4)由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。
2:语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中 。
看不出难度呀,单元格是一对一的复制,表1的B-F列右移一下到表2的C-G列,8节课在2-3、4-5之间添加一个空行,要求就是这样吗,解决此问题用公式最好,如果作为学VBA的例子,主要是练习数组使用、FOR循环使用。代码可以这样:dim arr1,arr2,i,j,t
arr1=sheets("表1").range("a1:f10")
arr2=sheets("表2").range("c5:g14") '需要填写数据的区域
t=Array(0,3,4,0,5,6,0,7,8,9,10) '行对应关系,t(x)值表示arr2的x行数据在arr1中的行号
for i=lbound(arr2) to ubound(arr2)
if t(i)<>0 then
for j=lbound(arr2,2) to ubound(arr2,2)
arr2(i,j)=arr1(t(i), j+1)
next j
end if
next i
sheets("表2").range("c5:g14")=arr2 '写入数据
首先假设:你的原始数据所在工作表表名叫:成绩,数据存放在A:F列,A1:F1是数据标题。
Sub 生成数据()Dim newbook As Workbook
Dim newsheet As Worksheet
Dim wb As Workbook
arr = Sheets("成绩").Range("A1:F1")
Set wb = Application.Workbooks.Add
With wb.Sheets(1)
.Name = "专业成绩为零"
.Range("a1:f1") = arr
.Columns("a:f").AutoFit '根据内容调整列宽
End With
wb.SaveAs ThisWorkbook.Path & "\专业成绩为零.xlsx"
wb.Close False
Set wb = Application.Workbooks.Add
With wb.Sheets(1)
.Name = "行测成绩为零"
.Range("a1:f1") = arr
.Columns("a:f").AutoFit '根据内容调整列宽
End With
wb.SaveAs ThisWorkbook.Path & "\行测成绩为零.xlsx"
wb.Close False
Set wb = Nothing
Set Cnn1 = CreateObject("ADODB.connection")
Cnn1.Open "Provider=Microsoft.ACE.OLEDB.12.0Extended Properties='Excel 12.0'Data Source=" & ThisWorkbook.Path & "\专业成绩为零.xlsx"
Sql = "insert into [专业成绩为零$] select * from [Excel 12.0Database=" & ThisWorkbook.FullName & "].[成绩$a:f] WHERE (专业成绩=0) "
Cnn1.Execute Sql
Cnn1.Close
Cnn1.Open "Provider=Microsoft.ACE.OLEDB.12.0Extended Properties='Excel 12.0'Data Source=" & ThisWorkbook.Path & "\行测成绩为零.xlsx"
Sql = "insert into [行测成绩为零$] select * from [Excel 12.0Database=" & ThisWorkbook.FullName & "].[成绩$a:f] WHERE (行测成绩=0) "
Cnn1.Execute Sql
Cnn1.Close
Set Cnn1 = Nothing
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)