VBA_String无法添加到数组中(错误:预期数组)

时间:2016-05-31 12:05:15

标签: arrays excel-vba vba excel

下午好,

我想添加一个长字符串,例如" OA_OB_OC_OD_n1"成阵列。然后使用Split功能将其分成几个部分并将它们放入我的工作表中,如: OA | OB | OC | OD | N1 |

这是我的代码(所有Dim都已完成):

Public Sub Boutton_Importer_Click()

list_de_controle = "TEXT;" & listPath
Open listPath For Input As #1  
'to read a list which contains the name of files I want to open

Do While Not EOF(1)
    Line Input #1, nom_de_Fich  'read the name of file
    WrdArray() = Split(nom_de_Fich, "_")  'here is the problem

    For Each wrd In WrdArray()
        ActiveCell.Value = wrd
        ActiveCell.Offset(0, 1).Select
    Next wrd

    Open nom_de_Fich For Input As #2  'open the file
    Insérer_contenu  'I have a function here to read the contents
    Close #2

    ActiveCell.Offset(1, 0).Select
    ActiveCell.End(xlToLeft).Select
Loop
Close #1

End Sub

如果我更改此部分:

Line Input #1, nom_de_Fich  'read the name of file
WrdArray() = Split(nom_de_Fich, "_")  'here is the problem

For Each wrd In WrdArray()
    ActiveCell.Value = wrd
    ActiveCell.Offset(0, 1).Select
Next wrd

成:

Line Input #1, nom_de_Fich  'read the name of file
ActiveCell.Value = nom_de_Fich
ActiveCell.Offset(0, 1).Select

效果很好。所以我不知道如何解决这个问题。 提前谢谢!

2 个答案:

答案 0 :(得分:0)

我写了一个较小的一个,阅读起来更简单:

Sub try()

    Dim longstr As String
    Dim myarray() As String
    Dim wrd As Variant

    longstr = "OA_OB_OC_OD_OE_OF_OG"


    myarray() = Split(longstr, "_")
    For Each wrd In myarray
        ActiveCell.Value = wrd
        ActiveCell.Offset(0, 1).Select
    Next
End Sub

再次感谢。

答案 1 :(得分:0)

嗯,我相信我什么也没做,只是删除所有内容并再次写下所有内容并且有效。 毕竟谢谢你!祝你有个美好的一天!