将一个阵列复制到另一个阵列

时间:2014-08-20 12:43:17

标签: excel vba excel-vba

我的Sub继续在线上以“< --- HERE”结尾的错误。

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

如果我将数组保持为动态(如下面的示例),我会得到Run-Time error '13': Type mismatch

我不明白为什么两者都不起作用?

也作为输入Entered_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

5 个答案:

答案 0 :(得分:5)

您可能不需要所有这些变量。

查找Split()

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

显然假设您的路径始终采用以下格式DRIVE:\FOLDER\SUB

答案 1 :(得分:4)

我认为错误是由于数组声明引起的

您的选项很少

  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

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

    e.g。 Full_Path(0) = Split(Entered_Path, ":", , vbTextCompare)(0)

答案 2 :(得分:4)

Split返回一个String数组,以便将返回的数组分配给动态数组,它们必须是相同的类型:

Dim Full_Path() As String

答案 3 :(得分:3)

根据微软Split function 的说法,您可以使用比较方法IF IFQUIRED。在这种情况下,基本拆分可能就足够了。

Optional ByVal Compare As CompareMethod = CompareMethod.Binary 

用于二进制比较,或

Optional ByVal Compare As CompareMethod = CompareMethod.Textual 

for textual然后使用split:

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

您将拥有一个{Lastname,Firstname}数组,因此您可以namesArray(0) & " " & namesArray(1)为您提供姓氏名字

以基本形式分割功能:

Split(Entered_Path, ":")

There is a lot of good reading here with further detail

答案 4 :(得分:0)

将vbTextcompare更改为CompareMethod.Text和Full_Path为字符串数组

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