有没有办法将所有值从一个数组转移到另一个数组,然后擦除原始数组?
Is there a way to transfer all values from one array to another, then erase the original array?
我 运行 遇到了我在工作中尝试开发的代码块的问题。本质上,我在 excel 中创建了一个用户表单,人们将在其中输入轨道车的数据,因为它们在特定位置装载(我们将这些称为“点 1、点 2、点 3 等”)。
有时他们不得不将那辆车移到另一个地方,在这种情况下,我希望他们能够保留 first/original 条目中有关轨道车的所有信息,然后删除数据完成后从原来的位置开始。
为了以更精简的方式完成此操作,我为 5 个点中的每一个都建立了数组,这些点引用了他们在用户表单中输入数据的所有单元格:
Dim spot1information(14)
spot1information(0) = UserForm.ProductType1.Value
spot1information(1) = UserForm.ProductID1.Value
spot1information(2) = UserForm.BatchID1.Value
etc....
Dim spot2information(14)
spot2information(0) = UserForm.ProductType2.Value
spot2information(1) = UserForm.ProductID2.Value
spot2information(2) = UserForm.BatchID2.Value
etc....
所有五个位置依此类推。我不知道这是否会使事情变得更困难,但请注意,这些数组值并非都是同一类型。例如,索引 (0) 将是一个字符串,但索引 (10) 是 DATETIME,索引 (12) 被定义为 Long。
假设他们正在将汽车从地点 1 移到地点 2。简而言之,我希望代码执行以下操作:
- 将 spot2information 中索引 0 - 6 的值(当前为空)替换为 spot1information 中索引 0 - 6 的值(用户已在用户表单中填写)。
- 我只对继承索引 0-6 感兴趣,因为它们包含相关的轨道车信息
- 将 spot1information 的每个值清空为“”
为此,我尝试了以下代码及其一些变体:
If OriginalSpot.Value = 1 Then
If DestinationSpot.Value = 2 Then
For i = 0 to 6
spot2information(i) = spot1information(i)
Next
For Each i in spot1information
spot1information(i) = ""
Next
End If
End If
但是,这会不断出现类型不匹配的问题。我想是因为 spot2information 数组中的数据是空的,而 spot1information 数组中的数据不是,但我不完全确定解决这个问题的方法。
更新:我按照下面的建议进行了操作,并将:spot1information(i) = ""
替换为 Erase spot1information
代码现在基本上可以工作了!数组“spot2information”的值现在是“spot1information”以前的值,“spot1information”现在为空。
下面建议的二维数组也很有用。我一直面临的新问题是数组值正在更新,但用户窗体没有。 (注意:以后我会把这类事情作为一个单独的问题发布,我很抱歉!)
将其作为二维数组更容易管理:
Sub Tester()
Dim spots(1 To 5, 0 To 14), n As Long, i As Long
'fill your spot arrays from the form....
For n = 1 To 5
spots(n, 0) = UserForm.Controls("ProductType" & n).Value
spots(n, 1) = UserForm.Controls("ProductID" & n).Value
spots(n, 2) = UserForm.Controls("BatchID" & n).Value
'etc etc
Next n
'swap a spot with another
Debug.Print spots(2, 1), spots(3, 1)
SwapSpots spots:=spots, fromSpot:=2, toSpot:=3
Debug.Print spots(2, 1), spots(3, 1)
End Sub
Sub SwapSpots(spots, fromSpot As Long, toSpot As Long)
Dim n As Long
For n = 0 To 6
spots(toSpot, n) = spots(fromSpot, n)
spots(fromSpot, n) = Empty 'empty the source slot value
Next n
End Sub
假设数组的 DataType
与 Index
相同,即 index(0)
对于所有点都是 string
,Index(2)
是 long
用于所有景点,依此类推。
如果是这样,那么这部分应该不会产生任何错误:
For i = 0 to 6
spot2information(i) = spot1information(i)
Next
错误应该发生在这部分,更准确地说是在标有#
的行中
For Each i in spot1information
spot1information(i) = "" '#
Next
错误的原因似乎是尝试将字符串值 ""
分配给数字类型,给定 “不匹配” 错误。
Using For Each i in spot1information
表示你想 "Initiate" or Erase 整个数组,因此我建议使用这一行而不是 For…Next
方法。
Erase spot1information
对此:
But I've now run into a new problem, where the values on the userform haven't updated to reflect the new values stored in the array. Do I need to somehow "refresh" the userform?
您刚刚更新了数组,那么您需要运行用于更新UserForm
.
中两个数组影响的对象值的程序
我 运行 遇到了我在工作中尝试开发的代码块的问题。本质上,我在 excel 中创建了一个用户表单,人们将在其中输入轨道车的数据,因为它们在特定位置装载(我们将这些称为“点 1、点 2、点 3 等”)。
有时他们不得不将那辆车移到另一个地方,在这种情况下,我希望他们能够保留 first/original 条目中有关轨道车的所有信息,然后删除数据完成后从原来的位置开始。
为了以更精简的方式完成此操作,我为 5 个点中的每一个都建立了数组,这些点引用了他们在用户表单中输入数据的所有单元格:
Dim spot1information(14)
spot1information(0) = UserForm.ProductType1.Value
spot1information(1) = UserForm.ProductID1.Value
spot1information(2) = UserForm.BatchID1.Value
etc....
Dim spot2information(14)
spot2information(0) = UserForm.ProductType2.Value
spot2information(1) = UserForm.ProductID2.Value
spot2information(2) = UserForm.BatchID2.Value
etc....
所有五个位置依此类推。我不知道这是否会使事情变得更困难,但请注意,这些数组值并非都是同一类型。例如,索引 (0) 将是一个字符串,但索引 (10) 是 DATETIME,索引 (12) 被定义为 Long。
假设他们正在将汽车从地点 1 移到地点 2。简而言之,我希望代码执行以下操作:
- 将 spot2information 中索引 0 - 6 的值(当前为空)替换为 spot1information 中索引 0 - 6 的值(用户已在用户表单中填写)。
- 我只对继承索引 0-6 感兴趣,因为它们包含相关的轨道车信息
- 将 spot1information 的每个值清空为“”
为此,我尝试了以下代码及其一些变体:
If OriginalSpot.Value = 1 Then
If DestinationSpot.Value = 2 Then
For i = 0 to 6
spot2information(i) = spot1information(i)
Next
For Each i in spot1information
spot1information(i) = ""
Next
End If
End If
但是,这会不断出现类型不匹配的问题。我想是因为 spot2information 数组中的数据是空的,而 spot1information 数组中的数据不是,但我不完全确定解决这个问题的方法。
更新:我按照下面的建议进行了操作,并将:spot1information(i) = ""
替换为 Erase spot1information
代码现在基本上可以工作了!数组“spot2information”的值现在是“spot1information”以前的值,“spot1information”现在为空。
下面建议的二维数组也很有用。我一直面临的新问题是数组值正在更新,但用户窗体没有。 (注意:以后我会把这类事情作为一个单独的问题发布,我很抱歉!)
将其作为二维数组更容易管理:
Sub Tester()
Dim spots(1 To 5, 0 To 14), n As Long, i As Long
'fill your spot arrays from the form....
For n = 1 To 5
spots(n, 0) = UserForm.Controls("ProductType" & n).Value
spots(n, 1) = UserForm.Controls("ProductID" & n).Value
spots(n, 2) = UserForm.Controls("BatchID" & n).Value
'etc etc
Next n
'swap a spot with another
Debug.Print spots(2, 1), spots(3, 1)
SwapSpots spots:=spots, fromSpot:=2, toSpot:=3
Debug.Print spots(2, 1), spots(3, 1)
End Sub
Sub SwapSpots(spots, fromSpot As Long, toSpot As Long)
Dim n As Long
For n = 0 To 6
spots(toSpot, n) = spots(fromSpot, n)
spots(fromSpot, n) = Empty 'empty the source slot value
Next n
End Sub
假设数组的 DataType
与 Index
相同,即 index(0)
对于所有点都是 string
,Index(2)
是 long
用于所有景点,依此类推。
如果是这样,那么这部分应该不会产生任何错误:
For i = 0 to 6
spot2information(i) = spot1information(i)
Next
错误应该发生在这部分,更准确地说是在标有#
的行中 For Each i in spot1information
spot1information(i) = "" '#
Next
错误的原因似乎是尝试将字符串值 ""
分配给数字类型,给定 “不匹配” 错误。
Using For Each i in spot1information
表示你想 "Initiate" or Erase 整个数组,因此我建议使用这一行而不是 For…Next
方法。
Erase spot1information
对此:
But I've now run into a new problem, where the values on the userform haven't updated to reflect the new values stored in the array. Do I need to somehow "refresh" the userform?
您刚刚更新了数组,那么您需要运行用于更新UserForm
.