Excel宏文本到列

时间:2014-01-27 10:28:27

标签: excel vba excel-vba

我想为我的.csv文件写“宏”。我需要先对数据“text do columns”执行,但是当我尝试记录宏时,我收到一个错误:

“线路延续太多”。

我选择了collumn A,然后将“text”按到“Columns”。

宏观代码:

    Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+e
'
    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),Array(3,1),Array(4,1),Array(5,1),Array(6,1), _
        Array(7,1),Array(8,1),Array(9,1),Array(10,1),Array(11,1),Array(12,1),Array(13,1 _
        ),Array(14,1),Array(15,1),Array(16,1),Array(17,1),Array(18,1),Array(19,1),Array _
        (20,1),Array(21,1),Array(22,1),Array(23,1),Array(24,1),Array(25,1),Array(26,1), _
        Array(27,1),Array(28,1),Array(29,1),Array(30,1),Array(31,1),Array(32,1),Array( _
        33,1),Array(34,1),Array(35,1),Array(36,1),Array(37,1),Array(38,1),Array(39,1), _
        Array(40,1),Array(41,1),Array(42,1),Array(43,1),Array(44,1),Array(45,1),Array( _
        46,1),Array(47,1),Array(48,1),Array(49,1),Array(50,1),Array(51,1),Array(52,1), _
        Array(53,1),Array(54,1),Array(55,1),Array(56,1),Array(57,1),Array(58,1),Array( _
        59,1),Array(60,1),Array(61,1),Array(62,1),Array(63,1),Array(64,1),Array(65,1), _
        Array(66,1),Array(67,1),Array(68,1),Array(69,1),Array(70,1),Array(71,1),Array( _
        72,1),Array(73,1),Array(74,1),Array(75,1),Array(76,1),Array(77,1),Array(78,1), _
        Array(79,1),Array(80,1),Array(81,1),Array(82,1),Array(83,1),Array(84,1),Array( _
        85,1),Array(86,1),Array(87,1),Array(88,1),Array(89,1),Array(90,1),Array(91,1), _
        Array(92,1),Array(93,1),Array(94,1),Array(95,1),Array(96,1),Array(97,1),Array( _
        98,1),Array(99,1),Array(100,1),Array(101,1),Array(102,1),Array(103,1),Array(104 _
        ,1),Array(105,1),Array(106,1),Array(107,1),Array(108,1),Array(109,1),Array(110, _
        1),Array(111,1),Array(112,1),Array(113,1),Array(114,1),Array(115,1),Array(116,1 _
        ),Array(117,1),Array(118,1),Array(119,1),Array(120,1),Array(121,1),Array(122,1) _
        ,Array(123,1),Array(124,1),Array(125,1),Array(126,1),Array(127,1),Array(128,1), _
        Array(129,1),Array(130,1),Array(131,1),Array(132,1),Array(133,1),Array(134,1)
End Sub

我该如何解决这个问题?


-----------------------编辑----------------------- -----

我尝试按照您的建议更改代码:

Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+e
'
    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),Array(3,1),Array(4,1),Array(5,1),Array(6,1), Array(7,1),Array(8,1),Array(9,1),Array(10,1),Array(11,1),Array(12,1),Array(13,1),Array(14,1),Array(15,1),Array(16,1),Array(17,1),Array(18,1),Array(19,1) _
End Sub

BUt尝试保存之后,我收到错误“编译错误:数组的最后一个lin的无效字符(19,1)_”

我试图删除_但是同样的错误。

3 个答案:

答案 0 :(得分:4)

Too many lines continuation指的是您在所呈现的代码中的大多数行中找到的字符:_(下划线)。您需要做的是通过将一些行添加到另一行来压缩代码。

换句话说,您需要减少代码行数,但需要更长时间。

示例:

'instead of this part of your code:
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),Array(3,1),Array(4,1),Array(5,1),Array(6,1), _
    Array(7,1),Array(8,1),Array(9,1),Array(10,1),Array(11,1),Array(12,1),Array(13,1 _
    ),Array(14,1),Array(15,1),Array(16,1),Array(17,1),Array(18,1),Array(19,1),Array _
    '...etc

'you should try to compact it in this way:
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),Array(3,1),Array(4,1),Array(5,1),Array(6,1), Array(7,1),Array(8,1),Array(9,1),Array(10,1),Array(11,1),Array(12,1),Array(13,1),Array(14,1),Array(15,1),Array(16,1),Array(17,1),Array(18,1),Array(19,1),Array _
'...etc

重要 执行此操作时,请确保在每行末尾的下划线前留空(空)空格。你需要有类似的东西:

...) _
..., _
....), _

答案 1 :(得分:3)

如果每个字段的数据类型相同,则可以使用:

Selection.TextToColumns Destination:= Range("A1"), DataType:= xlDelimited, Comma:=True

我认为应该这样做? 希望这会有所帮助。

答案 2 :(得分:0)

请尝试使用此代码,提示用户选择范围,然后将文本应用于列功能。注意:参数值可能会根据您使用的数据集而改变。 - > Source Link

Sub TextToColumnsRangeSelection()
    On Error Resume Next
    Dim MySelection As Range

    Application.DisplayAlerts = False
    Set MySelection = Application.InputBox(Prompt:="Please select your range", Title:="Select Range", Type:=8)

    MySelection.TextToColumns _
    Destination:=MySelection, _
    DataType:=xlDelimited, _
    TextQualifier:=xlTextQualifierDoubleQuote, _
    ConsecutiveDelimiter:=False, _
    Tab:=False, _
    Semicolon:=False, _
    Comma:=True, _
    Space:=False, _
    Other:=False
End Sub