2

我正在尝试(对于我的学习和功能宏一样多)将我录制的宏转换为以下函数。

我收到错误“未选择要解析的数据”

我认为我的问题Selection.TextToColumns Destination:=Cells(1, (cNum + 1)).Select出在第二个子上。我不知道"iDel"我写它的方式是否有问题,因为我没有过去如何改变Destination:=Range("I1")

在哪里:

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

任何见解都是有帮助的

这是固定版本2:( 这是最后一个版本(我不知道我可以将 Array 放在“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
4

1 回答 1

2

OP 正确识别了两个问题:

i) Selection.TextToColumns Destination:=Cells(1, (cNum + 1)).Select 和
ii) iDel

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

正如现在在 OP 中所反映的那样,修复是 delete.SelectiDel.

于 2013-09-25T00:20:42.557 回答