TA的每日心情 | 开心 2012-3-19 10:42 |
---|
签到天数: 43 天 [LV.5]常住居民I
|
马上注册,下载丰富资料,享用更多功能,让你轻松玩转阳光石油论坛。
您需要 登录 才可以下载或查看,没有账号?欢迎注册
x
本帖最后由 twtjerdf 于 2011-12-29 12:58 编辑
Private Sub 导入文本数据_Click()
'If Me.TextBox3 = "" Then MsgBox "请选择保存路径!", 64, "提示": Exit Sub '没有存放路径则提示且退出程序
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim MyRng As Range
Dim i As Integer
Dim j As Integer
Set aimbook = Workbooks.Add(xlWorksheet)
Dim fd As FileDialog, Item As Integer
'如果选择了文件
If fd.Show = -1 Then
'遍历所有文件
For Item = 1 To fd.SelectedItems.Count
'逐个打开文件
Workbooks.Open (fd.SelectedItems(Item))
ActiveWorkbook.Sheets.Select
Columns("A:A").Select
Application.CutCopyMode = False
If (CheckBox6.Value) Then '忽略连续分隔
If (CheckBox2.Value) Then '空格分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
If (CheckBox1.Value) Then 'TAB 键分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
If (CheckBox3.Value) Then '逗号分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
If (CheckBox4.Value) Then '分号分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
If (CheckBox5.Value) Then '自定义分隔
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False '消除所有空格
Selection.Replace What:=Me.TextBox1.Value, Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False ' 自定义分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
Else '不忽略连续分隔
If (CheckBox2.Value) Then '空格分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
If (CheckBox1.Value) Then 'TAB 键分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
If (CheckBox3.Value) Then '逗号分隔
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)), TrailingMinusNumbers:=True
End If
If (CheckBox4.Value) Then '分号分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
If (CheckBox5.Value) Then '自定义分隔
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False '消除所有空格
Selection.Replace What:=Me.TextBox1.Value, Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False ' 自定义分隔
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
End If
End If
Sheets.Move Before:=aimbook.Sheets(1)
Next
Else
MsgBox "未发现文件"
End If
aimbook.SaveAs "导入数据"
'Shell "EXPLORER.EXE " & Me.TextBox3, vbNormalFocus '打开存放新工作簿的文件夹
End Sub
|
-
VBA界面设计
评分
-
查看全部评分
|