(转)如何导入 Excel 数据多于 256 个字段或列

如何导入 Excel 数据多于 256 个字段或列
概要
MicrosoftExcel 表格有 256 列 (列IV)。 因此, Excel 无法接受数据与每个工作表上记录超过 256 个字段。 其他字段当您尝试从数据库或文本文件含有每个记录, 多于 256 字段手动导入数据被截断从记录。
可以使用 Visual Basic for Applications 宏可占用了多张导入数据 本文提供示例宏, 至多 51

1. 启动或切换到 Excel。
2. 工具 菜单, 指向 , 然后单击 VisualBasic 编辑器
3. 插入 菜单上, 单击 模块
4. 模块, 中键入如下代码:

Sub LargeDatabaseImport()

'In the event of an error, make sure the application is reset to
'normal.
On Error GoTo ErrorCheck

'Dimension Variables
Dim ResultStr As String
Dim FileName As String
Dim FileNum As Integer
Dim Counter As Double
Dim CommaCount As Integer
Dim WorkResult As String

'Ask for the name of the file.
FileName = InputBox("Please type the name of your text file, for example, test.txt")

'Turn off ScreenUpdating and Events so that users can't see what is
'happening and can't affect the code while it is running.
Application.ScreenUpdating = False
Application.EnableEvents = False

'Check for no entry.
If FileName = "" Then End

'Get next available file handle number.
FileNum = FreeFile()

'Open text file for input.
Open FileName For Input As #FileNum

'Turn ScreenUpdating off.
Application.ScreenUpdating = False

'Set the counter to 1.
Counter = 1

'Place the data in the first row of the column.
Range("A1").Activate

'Loop until the end of file is reached.
Do While Seek(FileNum) <= LOF(FileNum)

'Show row number being imported on status bar.
Application.StatusBar = "Importing Row " & _
Counter & " of text file " & FileName

'Store one line of text from file to variable.
Line Input #FileNum, ResultStr

'Initialize the CommaCount variable to zero.
CommaCount = 0

'Store the entire string into a second, temporary string.
WorkResult = ResultStr

'Parse through the first line of data and separate out records
'257 to 510.
While CommaCount < 255

WorkResult = Right(WorkResult, Len(WorkResult) - InStr(1, WorkResult, ","))
CommaCount = CommaCount + 1

Wend

'Parse out any leading spaces.
If Left(WorkResult, 1) = " " Then WorkResult = Right(WorkResult, Len(WorkResult) - 1)

'Ensure that any records that contain an "=" sign are
'brought in as text, and set the value of the current
'cell to the first 256 records.
If Left(WorkResult, 1) = "=" Then
ActiveCell.Value = "'" & Left(ResultStr, Len(ResultStr) - Len(WorkResult))
Else
ActiveCell.Value = Left(ResultStr, Len(ResultStr) - Len(WorkResult))
End If

'Ensure that any records that contain an "=" sign are
'brought in as text,and set the value of the next cell
'to the last 256 records.
If Left(WorkResult, 1) = "=" Then
ActiveCell.Offset(0, 1).Value = "'" & WorkResult
Else
ActiveCell.Offset(0, 1).Value = WorkResult
End If

'Move down one cell.
ActiveCell.Offset(1, 0).Activate

'Increment the Counter by 1.
Counter = Counter + 1

'Start again at top of 'Do While' statement.
Loop

'Close the open text file.
Close

'Take records 257-510 and move them to sheet two.
Columns("B:B").Select
Selection.Cut
Sheets("Sheet2").Select
Columns("A:A").Select
ActiveSheet.Paste

'Run the text-to-columns wizard on both sheets.
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1))
Sheets("Sheet1").Select
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1))

'Reset the application to its normal operating environment.
Application.StatusBar = False
Application.EnableEvents = True
Application.ScreenUpdating = True

Exit Sub

ErrorCheck:

'Reset the application to its normal operating environment.
Application.StatusBar = False
Application.EnableEvents = True
Application.ScreenUpdating = True
MsgBox "An error occured in the code."

End Sub

5. 文件 菜单上, 单击 关闭并返回到 MicrosoftExcel
6. 工具 菜单, 指向 , 然后单击
7. 在宏, 的列表选择 LargeDatabaseImport 宏。 单击 运行
8. 对话框中出现, 键入您要导入的 CSV 文件路径和文件名。
来源:http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B272729

Random Posts

Leave a Reply