+ Reply to Thread
Results 1 to 11 of 11

vba excel - creating objects within an array

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    vba excel - creating objects within an array

    I have a dynamic range containing string variables containing numeric values that I would like to remove all non-numeric characters. I've run a loop using a regexp function which takes a few minutes to execute, so I resorted to using an array. My problem is it takes about 30-40 seconds to create the array which is probably the consequence of the regexp function is my guess. My question is, is there a faster way to do this? Currently it takes about 30 seconds to process 50k lines. It's still faster than looping, so I'll take it if this is the fastest way to process a range. I was curious if any of the masters here had any better way to handle this. My code is below.
    Please Login or Register  to view this content.

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: vba excel - creating objects within an array

    Try changing the code like below where you use "Early Binding" for the RegEx where you have to enable it from the Visual Basic Editor via Tools --> References --> Microsoft VBScript Regular Expression 5.5
    Please Login or Register  to view this content.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: vba excel - creating objects within an array

    It may not make much speed difference but I would use a simpler pattern:

    [^0-9]
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: vba excel - creating objects within an array

    Quote Originally Posted by terriertrip View Post
    ... to process 50k lines ...
    Arrgghhh .... You create and destroy a very same object 50.000 times

    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: vba excel - creating objects within an array

    Quote Originally Posted by nankw83 View Post
    Try changing the code like below where you use "Early Binding" for the RegEx
    The big benefit here is not early binding but rather that you are getting instantiation of regEx out of the loop.

  6. #6
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: vba excel - creating objects within an array

    Quote Originally Posted by 6StringJazzer View Post
    The big benefit here is not early binding but rather that you are getting instantiation of regEx out of the loop.
    Totally agree as illustrated by karedog

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: vba excel - creating objects within an array

    This is one of the times I find Static declarations to be a neat solution:
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel - creating objects within an array

    Thank you all for your help and input. Been sick with the flu for several days and finally feeling good enough to stare at a screen again.

    Wow. Thank you karedog for showing me the error in my ways! After retooling the routine, it pretty much processes it from 23 sec to less than 1 sec now. smh. Not sure if the 'early binding' contributes to the increase in speed beyond keeping it to one object, but I'll take it.

    Thanks Jeff for pointing out the pattern. I had no idea you could use the caret like that. Very cool.

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: vba excel - creating objects within an array

    You are welcome, thanks for marking the thread as solved and rep.points given.

    Regards

  10. #10
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: vba excel - creating objects within an array

    How would I deal with negative numbers?

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: vba excel - creating objects within an array

    Pattern of the regex need to be changed, it depends on your actual data, need to see some sample of them.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Creating Objects that know about each other...
    By RyderS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2021, 01:47 PM
  2. MS Excel Objects Lock Row based on Value working with Other Objects
    By Neuner in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2019, 02:26 PM
  3. Replies: 4
    Last Post: 12-23-2015, 04:26 PM
  4. Creating Objects
    By blane245 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2010, 06:49 PM
  5. Creating and Destroying Excel Objects from VB
    By RiosPapa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2005, 01:05 PM
  6. Creating Objects Along a Path
    By JanetFan95758 in forum Excel General
    Replies: 0
    Last Post: 05-17-2005, 08:00 PM
  7. Creating Objects Along a Path
    By JanetFan95758 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-17-2005, 03:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1