乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > ACCESS中用VBA如何导入当前目录内的指定<em>EXCEL</em>-excel vba导

ACCESS中用VBA如何导入当前目录内的指定<em>EXCEL</em>-excel vba导

作者:乔山办公网日期:

返回目录:excel表格制作


sub CopyContentsTo()
dim sPath$,sFilename$,sSheetname$
dim sTemp$,i%
sPath="H:\HRleader\DOCMENT\shift handover report\LINE\L18-DG DN" '可以自己修改
sFilename="L18-DN.xls" ‘同上
sSheetname="October" ’同上

sTemp="'"& sPath & "\[" & sFilename & "]" & sSheetname & "'!"
With Thisworkbook.Worksheets("Sheet1")
For i=5 to 80
With .Range("A"& i - 3 )
.FormulaR1C1="=" & Temp & "$C$" & i
.value=.value
End With
Next i
End With
End Sub

P.S. 如果要循环调用不同路径的e68a84e8a2ade799bee5baa6e79fa5e98193338不同文件的不同Sheet,
可以给sPath,sFilename,sSheetname赋数组,

dim i%,Sheetname(1 to 12)
For i = 1 to 12
Sheetname(i)=Array ("January","February","March","April","May","June", _
"July","August","September","October","November","December")(i)
Next i
则可以在后续的程序中调用Sheetname(1)到Sheetname(12)

.....
For k = 1 to 12
sTemp="'"& sPath(i) & "\[" & sFilename(j) & "]" & sSheetname(k) & "'!"
.......sTemp......
Next k
......

部分中文详文:

在导入数据的时候,有的时候,Access会把一些数据作为数值型的数据,实际上,我们需要作为字符型的数据,那么如何在导入的时候,指定导入的各种规格和细节呢?

答案是利用Schema.ini即可(导入/导出规格文件)。

有几种方式创建Schema,第一种是利用Access自身的文本数据导入导出向导:

在这个对话框中,点击[高级...]按钮,弹出对话框:

然后只要设定好各种格式,然后点击[另存为]保存即可。这种方式创建的Schema,只能集成在数据库中,无法灵活单独使用,因此我们需要用到另外一种方式:INI文件,推荐使用INI文件这种方式。

Schema.ini格式如下(参考:MSDN主题 Schema.ini File):

Schema.ini用于提供文本数据中的记录规格信息。每个Schema.ini的条目用于指明表的5个特征之一:

文本文件名
文件名有方括号括起来,例如如果要对Sample.txt使用Schema,那么它的对应的Schema条目应该是

[Sample.txt]

文件格式
指令如下:

Format=Value

Value可以取下面的值之一:

TabDelimited 用Tab分隔

CSVDelimited 用逗号分隔

FixedLength 固定长度

Delimited(C) 指定字符,其中C可以为除了双引号(")外的任何字符,也可以为空

字段名、字段宽度和类型
格式为:Coln=字段名 数据类型 [width 宽度]

字段名可以是任意字符,如果字段名包含空格,请使用双引号括起来。

数据类型可以为:

Bit

Byte

Short(Integer)

Long

Currency

Single

Double(Float)

DateTime(Date DateFormat)

Text(Char)

Memo(LongChar)

其中DateFormat是日期的格式字符串例如:Date YYYY-MM-DD

字符集
格式:CharacterSet=ANSI | OEM

格式只有两种:ANSI和OEM

特殊数e799bee5baa6e79fa5e98193e78988e69d83333据类型转换
特殊数据类型转换一般使用的比较少,主要是自定义日期、货币等等的数据格式,一般不用理会。在此也不作详细叙述。请自己查看MSDN帮助:Schema.ini File

 

 

下面给出一个简单的例子,假设有一个表Contacts.txt类似下面:

First NameLast NameHireDate

Nancy Davolio 10-22-91

Robert King 10-23-91

那么Schema.ini个是类似下面的INI文件(我加了注释):

[Contacts.txt] ///需要导入的文本文件名

ColNameHeader=True ///是否有数据头

Format=FixedLength ///字段固定长度

MaxScanRows=0 ///最多导入行

CharacterSet=OEM ///字符集

Col1="First Name" Char Width 10 ///第一列格式

Col2="Last Name" Char Width 9 ///第二列格式

Col3="HireDate" Date Width 8 ///第三列格式

////依此类推

我们可以根据数据自动创建这个Schema.ini文件!

注意,Schema.ini必须和需要导入的文本文件在同一目录!!!如果不在同一个目录,必须指定Schema.ini的全路径!

此后,我们就可以利用下面的语句来导入数据了:

DoCmd.TransferText acImportFixed, , "Contacts", "C:Documents.txt"

或者

DoCmd.TransferText acImportFixed, "C:Documents.ini", "Contacts", "C:Documents.txt"

下面给出TransferText的语法(摘自Access帮助):

DoCmd.TransferText [TransferType][, SpecificationName], TableName, FileName[, HasFieldNames][, HtmlTableName][, CodePage]

TransferType 可选 AcTextTransferType。

AcTextTransferType 可以是下列 AcTextTransferType 常量之一:

acExportDelim

acExportFixed

acExportHTML

acExportMerge

acImportDelim 默认

acImportFixed

acImportHTML

acLinkDelim

acLinkFixed

acLinkHTML

如果将该参数留空,则采用默认常量 (acImportDelim)。

SpecificationName 可选 Variant 型。字符串表达式,表示在当前数据库中创建并保存的导入或导出规格的名称。对于固定长度的文本文件, 必须指定参数或使用 schema.ini 文件,该文件还必须保存在导入、链接或导出的文本文件的同一个文件夹中。若要创建一个方案文件, 可使用文本导入/导出向导创建此文件。对于分隔的文本文件和 Microsoft Word 邮件合并数据文件,可以将该参数留空,以便选择默认的导入/导出规格。

TableName 可选 Variant 型。字符串表达式,表示要向其导入文本数据、从中导出文本数据或链接文本数据的 Microsoft Access 表的名称,或者要将其结果导出到文本文件的 Microsoft Access 查询的名称。

FileName 可选 Variant 型。字符串表达式,表示要从中导入、导出到或链接到的文本文件的完整名称(包括路径)。

HasFieldNames 可选 Variant 型。使用 True (-1) 可以在导入、导出或链接时,使用文本文件中的第一行作为字段名。使用 False (0) 可以将文本文件中的第一行看成普通数据。如果将该参数留空,则采用默认值 (False)。该参数将被 Microsoft Word 邮件合并数据文件忽略,这些文件的第一行中必须包含字段名。

HTMLTableNam 可选 Variant 型。字符串表达式,表示要导入或链接的 HTML 文件中的表或列表的名称。除非 transfertype 参数设为 acImportHTML 或 acLinkHTML,否则该参数将被忽略。如果将该参数留空,则导入或链接 HTML 文件中的第一个表或列表。如果 HTML 文件中存在 <CAPTION> 标记,则 HTML 文件的表或列表名称取决于该标记指定的文本。如果没有 <CAPTION> 标记,则名称由 <TITLE> 标记指定的文本决定。如果有多个表或列表具有相同的名称,则 Microsoft Access 将通过给每个表或列表名称结尾添加一个数字,如“雇员1”和“雇员2”来区分它们。

CodePage 可选 Variant 型。Long 型值,用于标识代码页的字符集。

更详细的英文参考:
Schema.ini File (Text File Driver)
When the Text driver is used, the format of the text file is determined by using a schema information file. The schema information file, which is always named Schema.ini and always kept in the same directory as the text data source, provides the IISAM with information about the general format of the file, the column name and data type information, and a number of other data characteristics. A Schema.ini file is always required for accessing fixed-length data; you should use a Schema.ini file when your text table contains DateTime, Currency, or Decimal data or any time you want more control over the handling of the data in the table.

Note The Text ISAM will obtain initial values from the registry, not from Schema.ini. The same default file format applies to all new text data tables. All files created by the CREATE TABLE statement inherit those same default format values, which are set by selecting file format values in the Define Text Format dialog box with <default> chosen in the Tables list. If the values in the registry are different from the values in Schema.ini, the values in the registry will be overwritten by the values from Schema.ini.
Understanding Schema.ini Files
Schema.ini files provide schema information about the records in a text file. Each Schema.ini entry specifies one of five characteristics of the table:

The text file name
The file format
The field names, widths, and types
The character set
Special data type conversions
The following sections discuss these characteristics.

Specifying the File Name
The first entry in Schema.ini is always the name of the text source file enclosed in square brackets. The following example illustrates the entry for the file Sample.txt:

[Sample.txt]
Specifying the File Format
The Format option in Schema.ini specifies the format of the text file. The Text IISAM can read the format automatically from most character-delimited files. You can use any single character as a delimiter in the file except the double quotation mark ("). The Format setting in Schema.ini less than 1 and greater than –1 should contain leading zeros; this value can either be False (no leading zeros) or True.
CurrencySymbol Indicates the currency symbol to be used for currency values in the text file. Examples include the dollar sign ($) and Dm.
CurrencyPosFormat Can be set to any of the following values:
Currency symbol prefix with no separation ($1)
Currency symbol suffix with no separation (1$)
Currency symbol prefix with one character separation ($ 1)
Currency symbol suffix with one character separation (1 $)

CurrencyDigits Specifies the

CurrencyThousandSymbol
[ 本贴由 大熊 于 2003-4-1 16:33 最后编辑 ]收藏 分享
Sub Cad_radExcel()
Dim xlApp As Excel.Application
Dim xlSheet As Object
Dim xlWorkbook As Object

Set xlApp = CreateObject("excel.application")
Set xlWorkbook = xlApp.Workbooks.Open("C:\你的.xls")
xlApp.Visible = True
End Sub

方法/步骤

首先,Excel已有自定义函数(这里就不多说了,如果不了解自定义函数的,可以在我关于自定义函数的经验中找找看)。

点击Excel左上角的”Office按钮“,选择”另存为“,”其他格式“。

在弹出的新对话框中,选择”Excel 97-2003加载宏(*.xla),然后选择保存的路径和保存的文件名,点击“保存”键即可。
到这一步,实际上就完成了制作自定义加载项的工作,接下来,就是添加自定义加载项到Excel中了。

随便打开一个新的Excel文件,点击左上角的“Office按钮”,选择下面的“Excel选项”,继续选择“加载项”。

然后点击下方的“转到”按钮。

这时候又弹出一个新的对话框出来,选择其中的“浏览”按钮。

此时会出现一个“打开”对话框,找到在步骤3中储存的xla格式的文件,并点击“确定”按钮。

8
这时候,就可以看到左侧多了一个文件名,这说明按照正确的路径找到了这个文件,点击“确定”按钮,返回Excel。

9
这时候,就可以在Excel中,像自带的函数一样,随意使用模块中的两个函数了。而且任意时候,打开Excel,这个函数都是可以使用的。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网