REMOVE item from array

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

REMOVE item from array

Post by sal21 »

i fill an array with:

Code: Select all


...
ReDim Preserve STRLETTERS(RC)
            STRLETTERS(RC) = RIGA & "-" & COLONNA & "-" & Me.CGIORNO.Text
            RC = RC + 1
            ....

my test code

Code: Select all


...
INDICE=RIGA & "-" & COLONNA & "-" & Me.CGIORNO.Text
Call REMOVE_ITEM(INDICE)...

Private Sub REMOVE_ITEM(INDICE)
Dim K As Integer
    For K = LBound(STRLETTERS) To UBound(STRLETTERS)
        If STRLETTERS(K) = INDICE Then
            '[b]HERE CODE TO REMOVE ITEM INDICE FROM ARRAY[/b]
        End If
    Next K
End Sub
other way are welcomed...

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: REMOVE item from array

Post by SpeakEasy »

Have you considered using a Collection instead? VBA arrays are not really designed for adding or removing items cleanly, except at the end of the array.

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REMOVE item from array

Post by HansV »

It might be better to use a Collection or Dictionary object. Both have a remove method.
Anyway, for the array:

Code: Select all

Private Sub REMOVE_ITEM(INDICE)
    Dim K As Long
    Dim L As Long
    Dim F As Boolean
    For K = LBound(STRLETTERS) To UBound(STRLETTERS)
        If STRLETTERS(K) = INDICE Then
            L = K
            F = True
            Exit For
        End If
    Next K
    If F Then
        For K = L To UBound(STRLETTERS) - 1
            STRLETTERS(K) = STRLETTERS(K + 1)
        Next K
        ReDim Preserve STRLETTERS(LBound(STRLETTERS) To UBound(STRLETTERS) - 1)
    End If
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: REMOVE item from array

Post by sal21 »

HansV wrote:
02 Sep 2021, 10:39
It might be better to use a Collection or Dictionary object. Both have a remove method.
Anyway, for the array:

Code: Select all

Private Sub REMOVE_ITEM(INDICE)
    Dim K As Long
    Dim L As Long
    Dim F As Boolean
    For K = LBound(STRLETTERS) To UBound(STRLETTERS)
        If STRLETTERS(K) = INDICE Then
            L = K
            F = True
            Exit For
        End If
    Next K
    If F Then
        For K = L To UBound(STRLETTERS) - 1
            STRLETTERS(K) = STRLETTERS(K + 1)
        Next K
        ReDim Preserve STRLETTERS(LBound(STRLETTERS) To UBound(STRLETTERS) - 1)
    End If
End Sub
tks bro, work peerfect!

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: REMOVE item from array

Post by sal21 »

SpeakEasy wrote:
02 Sep 2021, 10:39
Have you considered using a Collection instead? VBA arrays are not really designed for adding or removing items cleanly, except at the end of the array.
Hi bro, can you modify this code to a collection...

...
ReDim Preserve STRLETTERS(RC)
STRLETTERS(RC) = RIGA & "-" & COLONNA & "-" & Me.CGIORNO.Text
RC = RC + 1
...

is in a for next loop.

and example to remove INDICE in a collection?

INDICE = RIGA & "-" & COLONNA & "-" & Me.CGIORNO.Text

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REMOVE item from array

Post by HansV »

You could use code like this to populate the collection:

Code: Select all

    Dim colLetters As Collection
    Dim INDICE As String
    Set colLetters = New Collection
    ...
    For RIGA = ... To ...
        For COLONNA = ... To ...
            INDICE = RIGA & "-" & COLONNA & "-" & Me.CGIORNO.Text
            colLetters.Add Key:=INDICE, Item:=INDICE
        Next COLONNA
    Next RIGA
If you want to remove an item later:

Code: Select all

    INDICE = ...
    colLetters.Remove INDICE
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: REMOVE item from array

Post by sal21 »

HansV wrote:
28 Sep 2021, 08:32
You could use code like this to populate the collection:

Code: Select all

    Dim colLetters As Collection
    Dim INDICE As String
    Set colLetters = New Collection
    ...
    For RIGA = ... To ...
        For COLONNA = ... To ...
            INDICE = RIGA & "-" & COLONNA & "-" & Me.CGIORNO.Text
            colLetters.Add Key:=INDICE, Item:=INDICE
        Next COLONNA
    Next RIGA
If you want to remove an item later:

Code: Select all

    INDICE = ...
    colLetters.Remove INDICE
nice...
but i need to use as Public dim in other parft of project.
how to erase all in Collection for the next use?

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REMOVE item from array

Post by HansV »

Code: Select all

Set colLetters = Nothing
By the way, you can erase an array too, by using ReDim without the keyword Preserve.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: REMOVE item from array

Post by sal21 »

HansV wrote:
28 Sep 2021, 08:51

Code: Select all

Set colLetters = Nothing
By the way, you can erase an array too, by using ReDim without the keyword Preserve.
OK

but for looping the collection?

Sorry but my first experience with this. :thankyou:

User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REMOVE item from array

Post by HansV »

Sorry, I don't understand. What do you want to do?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: REMOVE item from array

Post by sal21 »

HansV wrote:
28 Sep 2021, 08:58
Sorry, I don't understand. What do you want to do?
looping/iterate items in collection

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: REMOVE item from array

Post by StuartR »

How about

Code: Select all

For Each INDICE in colLetters
...
Next
StuartR


User avatar
HansV
Administrator
Posts: 78232
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: REMOVE item from array

Post by HansV »

Thanks, Stuart!
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: REMOVE item from array

Post by SpeakEasy »

>how to erase all in Collection for the next use?

Just to complicate things, I know it has been suggested that you use

Code: Select all

Set colLetters = Nothing
but that does not set it up for reuse - you'd need to subsequently use

Code: Select all

Set colLetters = New Collection
but since the latter has the same effect (erasing the original collection), you might as well use it alone

In other words, instead of:

Code: Select all

Set colLetters = Nothing
Set colLetters = New Collection
just use

Code: Select all

Set colLetters = New Collection
Or (and here we get a little esoteric) make your public dim

Code: Select all

Dim colLetters As New Collection
rather than

Code: Select all

Dim colLetters As Collection
as this will make the object self-instantiate, so you never need to use

Code: Select all

Set colLetters = New Collection

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: REMOVE item from array

Post by rory »

I would suggest you generally should not use

Code: Select all

Dim ... As New ...
, since it has no real benefit and will prevent you from ever testing if your variable is Nothing, and can make debugging issues harder.
Regards,
Rory

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: REMOVE item from array

Post by Doc.AElstein »

I have also heard that this Auto instancing Early binding “Way Dim” ( using an auto instancing variable ) may make a code run a little slower, ( but I doubt these days noticeably )…
Last edited by Doc.AElstein on 28 Sep 2021, 12:15, edited 2 times in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: REMOVE item from array

Post by SpeakEasy »

>prevent you from ever testing if your variable is Nothing, and can make debugging issues harder.

Completely agree - autoinstantiation requires judicious use.. Meant to put that in as a caveat, and forgot.

That being said,, Microsoft provide a number of autoinstantiating classes though - e.g MSForms user forms
Last edited by SpeakEasy on 28 Sep 2021, 11:58, edited 1 time in total.

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: REMOVE item from array

Post by SpeakEasy »

>Auto instancing Early binding ... run a little slower

There is a very, very, very small performance hit (in simplistic terms, there's basically an extra check done to see whether the class has already been instantiated)

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: REMOVE item from array

Post by rory »

SpeakEasy wrote:
28 Sep 2021, 11:35
That being said,, Microsoft provide a number of autoinstantiating classes though - e.g MSForms user forms
That is true, but still doesn't make it a good idea. Well written code wouldn't use the automatic instance of a userform either. ;)
Regards,
Rory

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: REMOVE item from array

Post by Doc.AElstein »

Some of the bad practices possibly come about when its not clear , for example in the VB editor, whether something represented is a class or an instantiated object. Most people, even the experts often aren’t sure half the time, so that possibly means a lot of the Microsoft people themselves aren’t sure or forgot, or got it wrong.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also