Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 2 3
- 4 5 6
- 7 8 9
- MyArray(2,1 : 2,3) = 13
- Range("A1:C3") = MyArray: Range("A1,B2,C1,B3") = 11: MyArray = Range("A1:C3")
- Range("A1:C3") = MyArray: Range("A2:C2") = 13: MyArray = Range("A1:C3")
- Dim MyArray() As Variant
- Public Function MyArrayOut() As Variant
- MyArrayOut = MyArray
- End Function
- Sub testing()
- 'we are using a 5x4 (4 columns / 5 rows) array
- ReDim MyArray(0 To 4, 0 To 3) As Variant 'also 1 to 5 and 1 to 4 are possible.... doesn't matter
- Dim i As Long, j As Long 'input some numbers
- For i = 0 To 4
- For j = 0 To 3
- MyArray(i, j) = i + 5 * j + 1
- Next
- Next
- [A1:D5].Value2 = MyArray
- Stop ' check the sheet -> should be 1 to 20
- 'lets add 11 to the second row
- MyArray = Evaluate("=IF({0;1;0;0;0},MyArrayOut()+11,MyArrayOut())")
- [A1:D5].Value2 = MyArray
- Stop ' check again. row 2 should be 13, 18, 23, 28
- 'set only 1,1 ; 2,2 ; 4,2 and 5,4 to 0. just keep in mind that using math will fail if ANY value is not numeric (or empty)
- MyArray = Evaluate("=MyArrayOut()*{0,1,1,1;1,0,1,1;1,1,1,1;1,0,1,1;1,1,1,0}")
- [A1:D5].Value2 = MyArray
- Stop 'A1, B2, B4, D5 all should be 0
- 'set column 3 to "abc"
- MyArray = Evaluate("=IF({0,0,1,0},""abc"",MyArrayOut())")
- [A1:D5].Value2 = MyArray
- Stop ' check again. column C should be just abc
- End Sub
- Public Sub multiSetSame(ByRef arr As Variant, ByVal val As Variant, ParamArray str() As Variant)
- Dim runner As Variant
- For Each runner In str
- arr(Split(runner, ",")(0), Split(runner, ",")(1)) = val
- Next
- End Sub
- Sub test()
- Dim MyArray(1 To 3, 1 To 4) As Long
- multiSetSame MyArray, 11, "1,1", "2,2", "1,3", "3,2"
- [A1:D3].Value2 = MyArray
- End Sub
- MyArray = array(_
- array(1, 2, 3),_
- array(4, 5, 6),_
- array(7, 8, 9)_
- )
- Wscript.echo MyArray(0)(0) '1'
- Wscript.echo MyArray(2)(2) '9'
- SetArray(MyArray, patternStr, value)
- SetArray(MyArray, "1,1 2,2 1,3 3,2", 11)
- Set aw = New MyArrayWrapper
- MyArrayWrapper.array = MyArray
- aw.SetArray("1,1 2,2 1,3 3,2", 11)
- aw.SetArray("r1 c2", 12)
- MyArray = Array( [{1, 2, 3}], [{4, 5, 6}], [{7, 8, 9}] )
- MyArray(0)(1) = 11
- MyArray(1) = [Column(A1:A3)*0+13] ' changes a whole "row" to 13
- MyArray(2) = MyArray(1) ' copies one row to another
- Dim MyArray(2,2)
- MyArray(0,0) = 1
- MyArray(0,1) = 2
- MyArray(0,2) = 3
- MyArray(1,0) = 4
- MyArray(1,1) = 5
- MyArray(1,2) = 6
- MyArray(2,0) = 7
- MyArray(2,1) = 8
- MyArray(2,2) = 9
- Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
- lpvDest As Any, _
- lpvSource As Any, _
- ByVal cbCopy As Long)
- Sub test()
- 'assign a 2D array using the [] shorthand syntax for Eval
- myArray = [{1,2,3;4,5,6;7,8,9}]
- 'Change an individual value
- myArray(1, 2) = 11
- 'Change the entire first row (this ONLY works for the first row)
- 'Using a 1-based array assigned with []/Eval
- colarray = [{14,15,16}]
- CopyMemory myArray(1, 1), colarray(1), (UBound(colarray, 1) - LBound(colarray, 1) + 1) * 16 ' changes a whole "column"
- 'Change the entire first row (this ONLY works for the first row)
- 'Using a 0-based array assigned with Array()
- colarray = Array(17, 18, 19)
- CopyMemory myArray(1, 1), colarray(LBound(colarray, 1)), (UBound(colarray, 1) - LBound(colarray, 1) + 1) * 16 ' changes a whole "column"
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement