7 个解决方案
#1
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Before:=Workbooks("Book2").Sheets(1)
Sheets("Sheet1 (2)").Select
#2
能否再详细一些吗?
#3
Workbooks("Book1").Sheets("Sheet1").Copy Before:=Workbooks("Book2").Sheets(1)
将BOOK2的SHEET1考到BOOK1的SHEET1前
将BOOK2的SHEET1考到BOOK1的SHEET1前
#4
Dim cnn As New ADODB.Connection
mypath = "C:\My Documents\"
distfile = Dir(mypath & "*.xls")
If distfile = "" Then Exit Sub
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & mypath & distfile & ";Extended Properties=Excel 8.0;"
srcefile = Dir
Do Until srcefile = ""
i = InStrRev(srcefile, ".") - 1
newtable = Left(srcefile, i)
cnn.execute "select * into " & newtable & " from [Sheet1$] in '" & mypath & srcefile & "' 'Excel 8.0;'"
srcefile = Dir
Loop
cnn.Close
mypath = "C:\My Documents\"
distfile = Dir(mypath & "*.xls")
If distfile = "" Then Exit Sub
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & mypath & distfile & ";Extended Properties=Excel 8.0;"
srcefile = Dir
Do Until srcefile = ""
i = InStrRev(srcefile, ".") - 1
newtable = Left(srcefile, i)
cnn.execute "select * into " & newtable & " from [Sheet1$] in '" & mypath & srcefile & "' 'Excel 8.0;'"
srcefile = Dir
Loop
cnn.Close
#5
of123,您的代码我基本看懂了是什么意思,感觉应该可以,但测试发现,有一个报错,说在cnn.execute "select * into " & newtable & " from [Sheet1$] in '" & mypath & srcefile & "' 'Excel 8.0;'"行有错
错误说明是:查询输入必须包含一个表或查询。
另外我不太理解在这一句的后面"' 'Excel 8.0;'"的格式是否对?
错误说明是:查询输入必须包含一个表或查询。
另外我不太理解在这一句的后面"' 'Excel 8.0;'"的格式是否对?
#6
是我自己弄错了,谢谢of123(90分)也谢谢henrryzhang(10分)
#7
代码我测试过,没有问题。报错的原因可能是变量的问题,例如 newtable 是否为空。
#1
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Before:=Workbooks("Book2").Sheets(1)
Sheets("Sheet1 (2)").Select
#2
能否再详细一些吗?
#3
Workbooks("Book1").Sheets("Sheet1").Copy Before:=Workbooks("Book2").Sheets(1)
将BOOK2的SHEET1考到BOOK1的SHEET1前
将BOOK2的SHEET1考到BOOK1的SHEET1前
#4
Dim cnn As New ADODB.Connection
mypath = "C:\My Documents\"
distfile = Dir(mypath & "*.xls")
If distfile = "" Then Exit Sub
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & mypath & distfile & ";Extended Properties=Excel 8.0;"
srcefile = Dir
Do Until srcefile = ""
i = InStrRev(srcefile, ".") - 1
newtable = Left(srcefile, i)
cnn.execute "select * into " & newtable & " from [Sheet1$] in '" & mypath & srcefile & "' 'Excel 8.0;'"
srcefile = Dir
Loop
cnn.Close
mypath = "C:\My Documents\"
distfile = Dir(mypath & "*.xls")
If distfile = "" Then Exit Sub
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & mypath & distfile & ";Extended Properties=Excel 8.0;"
srcefile = Dir
Do Until srcefile = ""
i = InStrRev(srcefile, ".") - 1
newtable = Left(srcefile, i)
cnn.execute "select * into " & newtable & " from [Sheet1$] in '" & mypath & srcefile & "' 'Excel 8.0;'"
srcefile = Dir
Loop
cnn.Close
#5
of123,您的代码我基本看懂了是什么意思,感觉应该可以,但测试发现,有一个报错,说在cnn.execute "select * into " & newtable & " from [Sheet1$] in '" & mypath & srcefile & "' 'Excel 8.0;'"行有错
错误说明是:查询输入必须包含一个表或查询。
另外我不太理解在这一句的后面"' 'Excel 8.0;'"的格式是否对?
错误说明是:查询输入必须包含一个表或查询。
另外我不太理解在这一句的后面"' 'Excel 8.0;'"的格式是否对?
#6
是我自己弄错了,谢谢of123(90分)也谢谢henrryzhang(10分)
#7
代码我测试过,没有问题。报错的原因可能是变量的问题,例如 newtable 是否为空。