展开记录的“Text To Column”宏

时间:2013-09-24 20:04:58

标签: excel excel-vba vba

我正在尝试(与我的功能宏一样的学习)将我录制的宏转换为以下功能。

我收到错误“没有选择数据来解析”

我认为我的问题是Selection.TextToColumns Destination:=Cells(1, (cNum + 1)).Select在第二个Sub中。我不知道"iDel"是否是我编写的问题,因为我还没有通过如何更改Destination:=Range("I1")

其中:

cNum是要解析的列
iCol是要插入的列数 iDel是解析分隔符
工作表编号中的iSn

任何见解都很有帮助

这是固定版本2: (这是最后一个版本(我不知道我可以把数组放在“fTexeToColumn”中试试看它有效)

Sub TexeToColumn()
'1st is the column to be parsed
'2nd is the number of columns to insert
'3rd is the parsing delimiter
'4th is the Sheet Number
'Array Set New Col Header Names, add as many name as 2nd parameter is equal to

fTexeToColumn "8", "3", "[", "2", Array("New Col Name1", "New Col Name2", "New Col Name3")

End Sub

Sub fTexeToColumn(cNum As Long, iCol As Long, iDel As String, iSn As Long, Headers As Variant)
'cNum is the column to be parsed
'iCol is the number of columns to insert
'iDel is the parsing delimiter
'iSn is the Sheet Number

 Dim i As Long
 Dim BaseWks As Worksheet

 '~~> Set your sheet here
 Sheets(iSn).Select

 '~~>Adding Columns
 For colx = 1 To iCol Step 1
 Columns(cNum + colx).Insert Shift:=xlToRight
 Next

 '~~>Column to be parsed
 Columns(cNum).Select

'~Set destination range here
Selection.TextToColumns Destination:=Cells(1, (cNum + 1)), DataType:=xlDelimited, _
  TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
  Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
  :=iDel, FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

'~~>Delete original column
Columns(cNum).Delete

'Set Header Names
 Set BaseWks = ThisWorkbook.Worksheets(iSn)

  For i = LBound(Headers) To UBound(Headers)
  BaseWks.Cells(1, i + cNum) = Headers(i)
  Next i

End Sub

1 个答案:

答案 0 :(得分:2)

OP正确地确定了两个问题:

i)Selection.TextToColumns目的地:=单元格(1,(cNum + 1))。选择和
ii)iDel

对于前一个.Select是语法错误(在“设置目标范围”时已经选择了列),而后者iDel被定义为[如果所需的分隔符是"iDel",则i的使用将起作用(因为单个字符是允许作为带有文本到列的分隔符的所有内容)。

现在反映在OP中的修正是删除.SelectiDel周围的引号。