Is there a way to transfer all values from one array to another, then erase the original array?

↘锁芯ラ 提交于 2021-01-07 06:37:18

问题


I'm running into a problem with a block of code I'm trying to develop at my job. Essentially, I'm creating a userform in excel where folks will enter data for railcars as they get loaded at a certain location (we'll call these "spot 1, spot 2, spot 3, etc.").

Sometimes they'll have to move that car to a different spot, in which case I want them to be able to keep all the information on the railcar from the first/original entry, and then erase the data from the original spot once that's done.

To accomplish this in a more streamlined fashion, I've established arrays for each of the 5 spots that reference all the cells they're entering data into on the userform:

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....

And so forth for all five spots. I don't know if this makes things more difficult, but note that these array values aren't all of the same type. For instance, index (0) will be a string, but index (10) is a DATETIME and index (12) is defined as Long.

So say that they are moving a car from spot 1 to spot 2. In short, I want the code to do the following:

  • Replace the values of indices 0 - 6 in spot2information (which is currently empty) with the values of indices 0 - 6 in spot1information (which the user has filled on the userform).
  • I'm only interested in carrying over indices 0-6 because they contain the pertinent railcar information
  • Empty every value of spot1information to ""

To accomplish this, I tried the following code and a few variations thereof:

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

However, this keeps coming up with a type mismatch. I figure because the data in the spot2information array is empty, and the data in the spot1information array is not, but I'm not entirely sure of a way around this.


Update: I did what was suggested below and replaced: spot1information(i) = "" with Erase spot1information

The code now essentially works! The values of array "spot2information" are now the former values of "spot1information", with "spot1information" now empty.

The 2D array suggested below also works like a charm. New problem I've been facing is that array values are updating, but the userform isn't. (note: in the future I'll be posting this type of thing as a separate question, my apologies!)


回答1:


Assuming the DataType of the arrays is the same by Index i.e. index(0) is string for all spots, Index(2) is long for all spots, and so on.

If that is the case then this part should not produce any error:

    For i = 0 to 6
       spot2information(i) = spot1information(i)
    Next

The error should be happening in this part more precisely in the line marked with #

    For Each i in spot1information
       spot1information(i) = ""   '#
    Next

and the reason for the error it seems to be that trying to assign a string value "" to a numeric type, given the "mismatch" error.

Using For Each i in spot1information indicates that you want to "Initiate" or Erase the entire array, therefore I suggest to use this line instead of the For…Next method.

Erase spot1information

In regards this:

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?

You just updated the arrays, then you need to run the procedures used to update the values of the objects affected by both arrays in the UserForm.




回答2:


Easier to manage this as a 2D array:

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


来源:https://stackoverflow.com/questions/64866771/is-there-a-way-to-transfer-all-values-from-one-array-to-another-then-erase-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!