Hello
I have a single dimensional array in which I want to change lower bound, keeping the upper bound as same.
One way is to loop through the array and copy the desired values to a new variable. But, is there any shortcut or direct function ?
Hello
I have a single dimensional array in which I want to change lower bound, keeping the upper bound as same.
One way is to loop through the array and copy the desired values to a new variable. But, is there any shortcut or direct function ?
assuming existing array holds (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) and you want (6, 7, 8, 9, 10) in resultant array, try:
yeah, loopSub vbax_55984_change_arrLB() Dim arrA Dim i As Long, NewLB As Long arrA = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) NewLB = 5 For i = NewLB To UBound(arrA) arrA(i - NewLB) = arrA(i) Next i ReDim Preserve arrA(UBound(arrA) - NewLB) 'test For i = LBound(arrA) To UBound(arrA) MsgBox "index is " & i & " - Element is " & arrA(i) Next End Sub
PLS DO NOT PM; OPEN A THREAD INSTEAD!!!
1) Posting Code
[CODE]PasteYourCodeHere[/CODE]
(or paste your code, select it, click # button)
2) Uploading File(s)
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.
3) Testing the Codes
always back up your files before testing the codes.
4) Marking the Thread as Solved
from Thread Tools (on the top right corner, above the first message)
Why would you want this ?
Lbound is 0 or 1.
but if you want (1, 2, 3, 4, 5) in the final array, below procedure will do it:
Sub vbax_55984_change_arrLB() Dim arrA Dim i As Long, NewLB As Long arrA = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) NewLB = 5 ReDim Preserve arrA(NewLB To UBound(arrA)) End Sub
PLS DO NOT PM; OPEN A THREAD INSTEAD!!!
1) Posting Code
[CODE]PasteYourCodeHere[/CODE]
(or paste your code, select it, click # button)
2) Uploading File(s)
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.
3) Testing the Codes
always back up your files before testing the codes.
4) Marking the Thread as Solved
from Thread Tools (on the top right corner, above the first message)
---------------------------------------------------------------------------------------------------------------------
Paul
Remember: Tell us WHAT you want to do, not HOW you think you want to do it
1. Use [CODE] ....[/CODE ] Tags for readability
[CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
2. Upload an example
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
3. Mark the thread as [Solved] when you have an answer
Thread Tools (on the top right corner, above the first message)
4. Read the Forum FAQ, especially the part about cross-posting in other forums
http://www.vbaexpress.com/forum/faq...._new_faq_item3
I don't see any advantage.
Be as you wish to seem
---------------------------------------------------------------------------------------------------------------------
Paul
Remember: Tell us WHAT you want to do, not HOW you think you want to do it
1. Use [CODE] ....[/CODE ] Tags for readability
[CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
2. Upload an example
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
3. Mark the thread as [Solved] when you have an answer
Thread Tools (on the top right corner, above the first message)
4. Read the Forum FAQ, especially the part about cross-posting in other forums
http://www.vbaexpress.com/forum/faq...._new_faq_item3
Try using row.
Sub vbax_55984_change_arrLB_2() Dim arrA, i As Long, NewLB As Long arrA = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) MsgBox Join(arrA, vbLf) NewLB = 5 For i = NewLB To UBound(arrA) arrA(i - NewLB) = arrA(i) Next i ReDim Preserve arrA(UBound(arrA) - NewLB) MsgBox Join(arrA, vbLf) End Sub 'snb arrays, http://www.snb-vba.eu/VBA_Arrays_en.html 'http://www.snb-vba.eu/VBA_Arrays_en.html#L_6.1 Sub vbax_55984_change_arrLB_3() Dim arrA, i As Long, NewLB As Long, arrB() As Variant arrA = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) MsgBox Join(arrA, vbLf) NewLB = 5 'arrB() = Application.Index(arrA, [row(5:10)]) arrB() = Application.Index(arrA, Evaluate("=row(" & NewLB & ":10)")) Show2DArray arrB() MsgBox Join(Application.Transpose(arrB), vbLf) End Sub 'similar to, http://www.cpearson.com/excel/vbaarrays.htmhttp://www.cpearson.com/excel/vbaarrays.htm Public Sub Show2DArray(ByRef myArry() As Variant) '<-- Note ByRef modifier. Dim x As Long Dim y As Long Dim s As String s = "" For y = LBound(myArry, 2) To UBound(myArry, 2) For x = LBound(myArry, 1) To UBound(myArry, 1) s = s & myArry(x, y) & ", " Next x If Mid(s, Len(s) - 1, 1) = "," Then s = Left(s, Len(s) - 2) s = s & vbNewLine Next y MsgBox s End Sub
HI All Thanks for your answers,
The reason for me to change the LBOUND of a single dimensional array is that I want to delete first few entries from the array. Is there any other solution to do this ?
Why did they get there in the first place ?
You can ignore any value in any array without modifying the array.
You can 'remove/retain' any value in any 1-dimensional array using 'filter'.
1. Are the elements fixed length, intrinsic variables (Long, Double, etc.) not String?The reason for me to change the LBOUND of a single dimensional array is that I want to delete first few entries from the array. Is there any other solution to do this ?
2. Do you start with ary1(1 to 100) [= 100 elements]and then you only want ary2 (51 to 100) [= 50 elements] such that ary2(51) = ary1(51) ?
However, it's really not necessary to delete elements like that
---------------------------------------------------------------------------------------------------------------------
Paul
Remember: Tell us WHAT you want to do, not HOW you think you want to do it
1. Use [CODE] ....[/CODE ] Tags for readability
[CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
2. Upload an example
Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
3. Mark the thread as [Solved] when you have an answer
Thread Tools (on the top right corner, above the first message)
4. Read the Forum FAQ, especially the part about cross-posting in other forums
http://www.vbaexpress.com/forum/faq...._new_faq_item3
Sub test() Dim c As New Collection 'add 20 items For i = 1 To 20 c.Add i Next 'remove first 10 For i = 1 To 10 c.Remove 1 Next 'print remaining values For Each itm In c Debug.Print itm Next End Sub
A collection <> an Array
so?
There are 3 viable solutions and with a bit more work, another using jonh's collection method. I guess we could go at this for a long time to find many solutions. The row and filter methods are usually sufficient for my needs. snb's web site is best if you want to learn more about vba arrays.
This method is similar to rewriting the array method and the collection method. The main purpose is to show you the power that arraylist can provide.
'https://msdn.microsoft.com/en-us/library/system.array_methods(v=vs.110).aspx 'Array Methods Sub Test_aRedim() Dim a() As Variant, i As Integer a() = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j") MsgBox Join(a, vbLf) 'a() = aRedim(a(), 4, 3) 'e,f,g a() = aRedim(a(), 4) 'e,f,g,h,i,j MsgBox Join(a(), vbLf) End Sub Function aRedim(a() As Variant, iFrom As Long, Optional iTo As Long = 0) Dim o As Object, i As Long If iTo = 0 Then iTo = UBound(a) - iFrom End If If LBound(a) = 0 Then iTo = iTo + 1 Set o = CreateObject("System.Collections.ArrayList") With o For i = LBound(a) To UBound(a) .Add a(i) Next i aRedim = o.getrange(iFrom, iTo).Toarray() End With End Function
To 'remove' the first 5 items in this array:
Sub M_snb() sn = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j") sn = Split(Trim(Split(Join(sn), sn(4))(1))) End Sub