ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

TextToColumns如何分离成数组。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-3-22 07:46 | 显示全部楼层 |阅读模式
image.png


  1. Sub ll()
  2.    Dim Rng As Range
  3.        Set Rng = Selection
  4.        Rng.TextToColumns , xlFixedWidth, _
  5.        FieldInfo:=Array(Array(0, 1), Array(4, 1), Array(8, 1), Array(10, 1), Array(12, 1), _
  6.         Array(13, 1), Array(15, 1), Array(17, 1), Array(19, 1)), TrailingMinusNumbers:=True


  7.       
  8. End Sub
复制代码


程序运行后,结果在多个单元列中,不是目标需求。


image.png




名称必选/可选数据类型说明
Destination可选Variant一个 Range 对象,指定 Microsoft Excel 放置结果的位置。如果该区域大于一个单元格,则使用左上角的单元格。
DataType可选XlTextParsingType将被拆分到多列中的文本的格式。
TextQualifier可选XlTextQualifier指定是将单引号、双引号用作文本分隔符还是不使用引号。
ConsecutiveDelimiter可选Variant如果为 True,则 Microsoft Excel 将连续分隔符视为一个分隔符。默认值为 False。
Tab可选Variant如果为 True,则 DataType 为 xlDelimited 并将制表符作为分隔符。默认值为 False。
Semicolon可选Variant如果为 True,则 DataType 为 xlDelimited 并将分号作为分隔符。默认值为 False。
Comma可选Variant如果为 True,则 DataType 为 xlDelimited 并将逗号作为分隔符。默认值为 False。
Space可选Variant如果为 True,则 DataType 为 xlDelimited 并将空格字符作为分隔符。默认值为 False。
Other可选Variant如果为 True,则 DataType 为 xlDelimited 并将 OtherChar 参数指定的字符作为分隔符。默认值为 False。
OtherChar可选Variant(如果 Other 为 True,则为必选项)。当 Other 为 True 时的分隔符。如果指定了多个字符,则仅使用字符串中的第一个字符而忽略剩余字符。
FieldInfo可选Variant包含单列数据相关分列信息的数组。对该参数的解释取决于 DataType 的值。如果此数据由分隔符分隔,则该参数为由两元素数组组成的数组,其中每个两元素数组指定一个特定列的转换选项。第一个元素为列标(从 1 开始),第二个元素是 xlColumnDataType 的常量之一,用于指定分列方式。
DecimalSeparator可选Variant识别数字时,Microsoft Excel 使用的小数分隔符。默认设置为系统设置。
ThousandsSeparator可选Variant识别数字时,Excel 使用的千位分隔符。默认设置为系统设置。
TrailingMinusNumbers可选Variant以减号字符开始的数字。





TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-22 10:18 | 显示全部楼层
简单的事,搞复杂了。
用 Mide简单方便直接。
  1.     Dim Str, DateStr
  2.     Dim oDate As Date
  3.         DateStr = Selection
  4.         Str = Mid(DateStr, 5, 4) & " / " & Mid(DateStr, 9, 2) & " / " & Mid(DateStr, 11, 2)
  5.         Str = Str & " " & Mid(DateStr, 14, 2) & ":" & Mid(DateStr, 16, 2) & ":" & Mid(DateStr, 18, 2)
  6.         Debug.Print Str
  7.         Stop
  8.         oDate = Str
  9.         Debug.Print oDate
  10.         Stop
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-3-22 20:38 | 显示全部楼层
ning84 发表于 2024-3-22 10:18
简单的事,搞复杂了。
用 Mide简单方便直接。

  1. Function TraverseFile(oFolder As Folder)
  2.      Dim oFile As File
  3.      Dim DateStr, Str
  4.      Dim oDate As Date
  5.          For Each oFile In oFolder.Files
  6.              If InStr(oFile.Name, "IMG") > 0 Then
  7.                   DateStr = oFile.Name
  8.                   Str = Mid(DateStr, 5, 4) & " / " & Mid(DateStr, 9, 2) & " / " & Mid(DateStr, 11, 2)
  9.                   Str = Str & " " & Mid(DateStr, 14, 2) & ":" & Mid(DateStr, 16, 2) & ":" & Mid(DateStr, 18, 2)
  10.                   oDate = Str
  11.                   'Debug.Print oDate,
  12.                   'Debug.Print apiSetFileTime(oFile.Path, oDate, oDate, oDate)
  13.                   Debug.Print apiSetFileTime(oFile.Path, Str, Str, Str)
  14.              ElseIf InStr(UCase(oFile.Name), "SCR") > 0 Then
  15.                   DateStr = oFile.Name
  16.                   Str = Mid(DateStr, 5, 4) & " / " & Mid(DateStr, 9, 2) & " / " & Mid(DateStr, 11, 2)
  17.                   Str = Str & " " & Mid(DateStr, 14, 2) & ":" & Mid(DateStr, 16, 2) & ":" & Mid(DateStr, 18, 2)
  18.                   oDate = Str
  19.                   'Debug.Print oDate,
  20.                   Debug.Print apiSetFileTime(oFile.Path, Str, Str, Str)
  21.                   Stop
  22.                   Debug.Print apiSetFileTime(oFile.Path, oDate, oDate, oDate)
  23.              End If
  24.              Debug.Print oFile.Name, oFile.DateLastModified
  25.             
  26.          Next oFile
  27. End Function

  28. Function TraverseFolder(oFolders As Folders)
  29.      Dim oFolder As Folder
  30.      Dim ii
  31.          'For ii = 1 To oFolders.Cot
  32.          For Each oFolder In oFolders
  33.               'Set oFolder = oFolders.Item(ii)
  34.               'Debug.Print oFolder.Path ', oFolder.SubFolders.Count
  35.               If InStr(oFolder.Path, "System Volume Information") <= 0 Then
  36.                  'Debug.Print oFolder.Path, oFolder.SubFolders.Count
  37.                  TraverseFile oFolder
  38.               End If
  39.          Next oFolder
  40.          'Next ii
  41. End Function


  42. Sub TraverseFolderFile()
  43.     Dim Fso As FileSystemObject
  44.         Set Fso = New FileSystemObject
  45.     Dim oFolder As Folder, oFile As File
  46.     Dim oFolders As Folders
  47.         Set oFolder = Fso.GetFolder(ThisWorkbook.Path & "")
  48.         
  49.         Set oFolders = oFolder.SubFolders
  50.         
  51.         Debug.Print oFolder.Name
  52.         TraverseFolder oFolders
  53.         
  54. End Sub
复制代码


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-9-29 12:32 , Processed in 0.039285 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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