ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1918|回复: 1

[2007] excel VBA编程,想运行宏,出来浏览文件的对话框,选中文件导入,并去掉|分隔符

[复制链接]

TA的精华主题

TA的得分主题

发表于 2015-8-1 16:41 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
想通过VBA选择导入一个文件后,去掉|分隔符,以下是我写的代码,但只能运行指定运行一个指定的文件,但是想要的是一个可选择文件:
  1. <p>Sub 导入302()
  2. '
  3. ' 导入302 Macro
  4. '
  5. '关闭屏幕闪动</p><p>Application.ScreenUpdating = False</p><p>
  6.     Sheets("Sheet3").Select
  7.    
  8.     Cells.Select
  9.     Selection.ClearContents
  10.    
  11.     Range("A1").Select
  12.     With ActiveSheet.QueryTables.Add(Connection:= _
  13.         "TEXT;C:\Users\Administrator\Desktop\小谭\301,302\BEF302_19200300_01.20150708", _
  14.         Destination:=Range("$A$1"))
  15.         .Name = "BEF302_19200300"
  16.         .FieldNames = True
  17.         .RowNumbers = False
  18.         .FillAdjacentFormulas = False
  19.         .PreserveFormatting = True
  20.         .RefreshOnFileOpen = False
  21.         .RefreshStyle = xlInsertDeleteCells
  22.         .SavePassword = False
  23.         .SaveData = True
  24.         .AdjustColumnWidth = True
  25.         .RefreshPeriod = 0
  26.         .TextFilePromptOnRefresh = False
  27.         .TextFilePlatform = 936
  28.         .TextFileStartRow = 1
  29.         .TextFileParseType = xlDelimited
  30.         .TextFileTextQualifier = xlTextQualifierDoubleQuote
  31.         .TextFileConsecutiveDelimiter = False
  32.         .TextFileTabDelimiter = True
  33.         .TextFileSemicolonDelimiter = False
  34.         .TextFileCommaDelimiter = False
  35.         .TextFileSpaceDelimiter = False
  36.         .TextFileOtherDelimiter = "|"
  37.         .TextFileColumnDataTypes = Array(9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
  38.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
  39.         .TextFileTrailingMinusNumbers = True
  40.         .Refresh BackgroundQuery:=False
  41.     End With
  42.     Rows("1:1").Select
  43.     Selection.Delete Shift:=xlUp
  44.     Sheets("Sheet1").Select</p><p>End Sub</p><p>
  45. </p><p> </p>
复制代码

QQ截图20150801163158.png

数据.zip

5.12 KB, 下载次数: 17

TA的精华主题

TA的得分主题

发表于 2015-8-2 10:59 | 显示全部楼层
(1)將Sub 导入302()改成Sub 导入302(FileName As String)
(2)將
"TEXT;C:\Users\Administrator\Desktop\小谭\301,302\BEF302_19200300_01.20150708", _
改成
"TEXT;C:\Users\Administrator\Desktop\小谭\301,302\" & FileName, _
(3)寫一個程式
Sub Main()
    Dim strNowPath As String
    Dim strFileName As String
    Dim strFileExt As String
    Dim n As Integer
    Dim Arr() As String   
    strNowPath = "C:\Users\Administrator\Desktop\小谭\301,302"
    strFileExt = "*.*"
    n = 0
    If Right(strNowPath, 1) = "\" Then
        strFileName = Dir(strNowPath & strFileExt, vbDirectory)
    Else
        strFileName = Dir(strNowPath & "\" & strFileExt, vbDirectory)
    End If
    While strFileName <> ""
        If strFileName <> "." And strFileName <> ".." Then
            ReDim Preserve Arr(n)
            Arr(n) = strFileName
            Call 导入302(Arr(n))
            n = n + 1
        End If
        strFileName = Dir()
    Wend
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-27 01:53 , Processed in 0.027740 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表