您可以仅使用Excel将第二张工作表从Excel导入到Access中吗? [英] Can you import only the 2nd Sheet from Excel to Access using VBA

查看:147
本文介绍了您可以仅使用Excel将第二张工作表从Excel导入到Access中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您只能使用VBA从Excel导入第二张表到Access吗?我对此进行了一些谷歌搜索,但是找不到确切的答案.

Can you import only the 2nd Sheet from Excel to Access using VBA?! I did some Googling on this and couldn't find a definitive answer.

这是我要开始工作的代码.

Here is the code that I'm trying to get working.

strPathFile = Fil

objXL.Visible = True
Set wkb = objXL.Workbooks.Open(strPathFile)
For Each wks In wkb.Worksheets
    If wkb.wks = wkb.Sheet(2) Then
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames
    End If
Next

我正试图从一堆Excel文件(总是从文件的第二张表)中导入数据,但是每张表都有不同的名称.

I'm trying to import data from a bunch of Excel files, always from the second sheet in the file, but each sheet has a different name.

推荐答案

事实证明,我错过了两件事.

As it turns out, I was missing 2 things.

1)  wks.Index = 2

2)  NeedThisSheet = wks.Name & "!"

这是代码段.

strPathFile = Fil

objXL.Visible = True
Set wkb = objXL.Workbooks.Open(strPathFile)
For Each wks In wkb.Worksheets
    If wks.Index = 2 Then
        NeedThisSheet = wks.Name & "!"
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames, NeedThisSheet
    End If
Next
wkb.Close

谢谢大家!

这篇关于您可以仅使用Excel将第二张工作表从Excel导入到Access中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆