复制一个数组到另一个

我的小组不断地收到错误在行结束与“<— HERE”。

如果在Dim中指定该数组是Full_Path(2),则得到compile error Cannot be assigned to an array

如果我把数组作为dynamic的(就像在示例波纹pipe中),我得到Run-Time error '13': Type mismatch

我不明白为什么不能工作?

也作为inputEntered_Path = D:\Data\MBS

 Sub Set_Folder(Entered_Path As String) ' this function wil point to the folder that you want your data be saved into Dim Drive As String, Folder As String Dim Full_Path() 'Assign this to a Public Variable Path = Entered_Path Full_Path = Split(Entered_Path, ":", , vbTextCompare) <--- HERE Drive = Full_Path(0) Folder = Full_Path(1) ChDrive Drive ChDir Folder End Sub 

你可能不需要所有这些variables。

查看Split()

 Sub SetFolder(Entered_Path) ChDrive Split(Entered_Path, ":")(0) ChDir Split(Entered_Path, ":")(1) End Sub 

这显然假设你的path总是在以下格式DRIVE:\FOLDER\SUB

Split返回一个String数组,所以为了将返回的数组赋值给一个dynamic数组,它们必须是相同的types:

 Dim Full_Path() As String 

我相信这个错误是由于数组声明造成的

你有几个select

  1. Dim Full_Path()更改为Dim Full_PathDim Full_Path() as String (推荐)

  2. 全部删除Dim Full_Path()语句

  3. Dim Full_Path()声明为Dim Full_Path(2) as String

    在这种情况下,您必须循环访问数组或按索引抓取数组以放置内容

    例如Full_Path(0) = Split(Entered_Path, ":", , vbTextCompare)(0)

根据微软的分裂function,你可以使用比较方法如果需要。 在这种情况下,基本拆分可能就足够了。

 Optional ByVal Compare As CompareMethod = CompareMethod.Binary 

为二进制比较,或

 Optional ByVal Compare As CompareMethod = CompareMethod.Textual 

为文本,然后使用分割:

 Split(EnteredPaty, ":", ,CompareMethod.Text) 

你将有一个{姓氏,名字}的数组,所以你可以只有namesArray(0) & " " & namesArray(1)给你姓氏名

拆分function的基本forms:

 Split(Entered_Path, ":") 

在这里有很多很好的阅读和进一步的细节

将vbTextcompare更改为CompareMethod.Text和Full_Path作为string数组

 Sub Set_Folder(Entered_Path As String) 'this function wil point to the folder that you want your data be saved into Dim Drive As String, Folder As String 'Dim Full_Path() <--- Look HERE 'Assign this to a Public Variable Path = Entered_Path Dim Full_Path As String() = Split(Entered_Path, ":", , CompareMethod.Text) <--- Look ABOVE Drive = Full_Path(0) Folder = Full_Path(1) ChDrive Drive ChDir Folder End Sub