strpath = Application.GetOpenFilename("ACCESS文件(*.mdb), *.mdb")
Application.Workbooks.Open (strpath)
如上语句,条开*.mdb文件后,会打开另外一个Excel文件,
求:怎样更改上边语句,使它不打开另外一个Excel文件,而从当前Excel表的Sheet3表单中打开*.mdb文件内容?
在已打开的Excel中,在Sheet3中打开指定文件(用VBA)
答案:2 悬赏:60 手机版
解决时间 2021-03-09 03:35
- 提问者网友:嗝是迷路的屁
- 2021-03-08 08:03
最佳答案
- 五星知识达人网友:往事埋风中
- 2021-03-08 08:54
Application.ScreenUpdating = False
Dim sh As Workbook
Filename = Application.GetOpenFilename(FileFilter:="Access 数据库文件 (*.mdb),*.mdb", Title:="请选择文件")
If Filename <> False Then
If Dir(Filename) <> ThisWorkbook.Name Then
Set sh = Workbooks.Open(Filename)
'取生成新表的表单名,start
t = Filename
a = Len(t)
t1 = Left(t, a - 4)
a1 = a - 4
For m = 1 To a - 4
b = Mid(t1, a1, 1)
If b = "\\" Then
Exit For
End If
a1 = a - 4 - m
Next
C = Right(t1, m - 1)
'取生成新表的表单名,end
sh.Sheets(C).Cells.Copy ThisWorkbook.Sheets("sheet3").Range("a1")
sh.Close SAVECHANGES:=False
End If
End If
Application.ScreenUpdating = True
以上语句全部复制,执行
Dim sh As Workbook
Filename = Application.GetOpenFilename(FileFilter:="Access 数据库文件 (*.mdb),*.mdb", Title:="请选择文件")
If Filename <> False Then
If Dir(Filename) <> ThisWorkbook.Name Then
Set sh = Workbooks.Open(Filename)
'取生成新表的表单名,start
t = Filename
a = Len(t)
t1 = Left(t, a - 4)
a1 = a - 4
For m = 1 To a - 4
b = Mid(t1, a1, 1)
If b = "\\" Then
Exit For
End If
a1 = a - 4 - m
Next
C = Right(t1, m - 1)
'取生成新表的表单名,end
sh.Sheets(C).Cells.Copy ThisWorkbook.Sheets("sheet3").Range("a1")
sh.Close SAVECHANGES:=False
End If
End If
Application.ScreenUpdating = True
以上语句全部复制,执行
全部回答
- 1楼网友:蕴藏春秋
- 2021-03-08 09:10
两种方法:
1.你确定读取数据库表名后,将sheet3激活,然后从a1开始黏贴
这个用到ado连接数据库.
2.你新开了workbook,把这张新开的activesheet用move移动到,旧的workbook,改名sheet3也可以.
两种你参考着用吧.
如果你是写vba来开启.那么我建议你用第一种:
写法如下:
dim cnn as new adodb.connection
dim rs as new adodb.recordset
dim cnnstr as string
dim sql as string
dim icols, i as integer
dim datestart, dateend as date
cnnstr = "dbq=" & strpath & ";defaultdir=" & 这里写mdb文件的路径 & ";driver={microsoft access driver (*.mdb, *.accdb)};driverid=25;fil=ms access;maxbuffersize=2048;maxscanrows=8;pagetimeout=5;safetransactions=0;threads=3;usercommitsync=yes;"
cnn.open cnnstr
sheets("sheet3").activate
'这句要在你的表有"sheet3"才能使用成功
sqlstr = "selet * from 表名"
cnn.commandtimeout = 0
rs.open sqlstr, cnn, 1, 3, adcmdtext
for icols = 0 to rs.fields.count - 1
cells(1, icols + 1) = rs.fields(icols).name
next
'这个循环读取列
[a2].copyfromrecordset rs
'放到a2单元格开始插入
rs.close
cnn.close
set rs = nothing
set cnn = nothing
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯