Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Old February 5th 21, 10:43 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Oct 2018
Posts: 25
Default Extract segments of an array

My array of strings is already sorted from A to Z:

mySort(1) = "apples"
mySort(2) = "apricot"
mySort(3) = "banana"
mySort(4) = "beans"
mySort(5) = "bread"
mySort(6) = "donuts"
'... etc, etc..
mySort(38) = "zuchini"

Is there an easy function that can copy any alphabetical segment of the array into a new array?

For example, if I give the function input "B", it should create this new array:

myarray = Array("banana", "beans", "bread")

Thanks!

  #2   Report Post  
Old February 6th 21, 01:37 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2019
Posts: 86
Default Extract segments of an array

"Tatsujin" wrote in message
My array of strings is already sorted from A to Z:

mySort(1) = "apples"
mySort(2) = "apricot"
mySort(3) = "banana"
mySort(4) = "beans"
mySort(5) = "bread"
mySort(6) = "donuts"
'... etc, etc..
mySort(38) = "zuchini"

Is there an easy function that can copy any alphabetical segment of the
array into a new array?

For example, if I give the function input "B", it should create this new
array:

myarray = Array("banana", "beans", "bread")


Dim i as Long, k as Long
Dim myarray () As String
' code
ReDim myarray (1 To 3) ' or 0 to 2
For i = 3 To 5
k = k + 1
myarray (k) = mySort(i)
Next

There are other efficient ways to copy entire or part arrays but complicated
with string arays. The worksheet Index function would do it but not
efficient and much faster to loop as above.

Peter T


  #3   Report Post  
Old February 6th 21, 03:16 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2011
Posts: 3,866
Default Extract segments of an array

Hi,

Am Fri, 5 Feb 2021 14:43:22 -0800 (PST) schrieb Tatsujin:

My array of strings is already sorted from A to Z:

mySort(1) = "apples"
mySort(2) = "apricot"
mySort(3) = "banana"
mySort(4) = "beans"
mySort(5) = "bread"
mySort(6) = "donuts"
'... etc, etc..
mySort(38) = "zuchini"

Is there an easy function that can copy any alphabetical segment of the array into a new array?


if you don't know where your searched character starts in the array you
can search the words beginnig with your expected character with Reg.Exp:

Set re = CreateObject("vbscript.regexp")
myString = Join(mySort, ", ")
'modify the first character as expected
ptrn = "b.[a-z]{1,}"

re.Pattern = ptrn
re.Global = True
re.ignoreCase = True
Set matches = re.Execute(myString)
For Each match In matches
ReDim Preserve myArray(n)
myArray(n) = match
n = n + 1
Next


Regards
Claus B.
--
Windows10
Microsoft 365 for business
  #4   Report Post  
Old February 6th 21, 03:48 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Jul 2019
Posts: 86
Default Extract segments of an array

"Peter T" wrote in message

Seeing Claus' reply I realise I misread your question.

Peter T


  #5   Report Post  
Old February 7th 21, 01:30 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2011
Posts: 3,866
Default Extract segments of an array

Hi,

Am Sat, 6 Feb 2021 16:42:22 -0800 (PST) schrieb Tatsujin:

I changed the mysort() array to this:

mysort(1) = "apple"
mysort(2) = "apricot"
mysort(3) = "baby"
mysort(4) = "banana"
mysort(5) = "bar"
mysort(6) = "fit"

When I use the search pattern of "a", it sets myarray to this:
myarray = ["apple", "apricot", "aby", "anana"].


sorry, my bad.

to find the "a" at the beginning of the word you have to insert \bin
front of the pattern:
ptrn = "\ba[a-z]{1,}"


Regards
Claus B.
--
Windows10
Microsoft 365 for business


  #6   Report Post  
Old February 7th 21, 01:41 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Oct 2018
Posts: 25
Default Extract segments of an array

Cool! That pattern works!

But how do you test when no matches are found? I think it's returning the last element of the array when I search for a letter not in the array, such as "x".
  #7   Report Post  
Old February 7th 21, 02:18 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Oct 2018
Posts: 25
Default Extract segments of an array

I mean, there's an error message if I search for a missing letter in the array.

How do you test for an empty array? I thought "IsEmpty()" would work?
  #8   Report Post  
Old February 7th 21, 03:33 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Oct 2018
Posts: 25
Default Extract segments of an array

On Saturday, February 6, 2021 at 6:30:12 PM UTC-7, Claus Busch wrote:
Hi,
Am Sat, 6 Feb 2021 16:42:22 -0800 (PST) schrieb Tatsujin:


The code works great! Ignore my previous post about testing for an empty array. I merely need to check the count of the variable n to determine the array size. Thanks Claus!
  #9   Report Post  
Old February 7th 21, 07:06 AM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Oct 2018
Posts: 25
Default Extract segments of an array

On Saturday, February 6, 2021 at 6:30:12 PM UTC-7, Claus Busch wrote:
Hi,
Am Sat, 6 Feb 2021 16:42:22 -0800 (PST) schrieb Tatsujin:

I changed the mysort() array to this:

mysort(1) = "apple"
mysort(2) = "apricot"
mysort(3) = "baby"
mysort(4) = "banana"
mysort(5) = "bar"
mysort(6) = "fit"

When I use the search pattern of "a", it sets myarray to this:
myarray = ["apple", "apricot", "aby", "anana"].

sorry, my bad.

to find the "a" at the beginning of the word you have to insert \bin
front of the pattern:
ptrn = "\ba[a-z]{1,}"


Hi Claus. Do you know how to modify that regex pattern so that it grabs the entire string after the first matched letter?

So, if mysort(1) = "Andy is in Room #29.", then myarray(0) should have "Andy is in Room #29."


  #10   Report Post  
Old February 8th 21, 04:46 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Apr 2011
Posts: 3,866
Default Extract segments of an array

Hi,

Am Sat, 6 Feb 2021 23:06:11 -0800 (PST) schrieb Tatsujin:

Hi Claus. Do you know how to modify that regex pattern so that it grabs the entire string after the first matched letter?

So, if mysort(1) = "Andy is in Room #29.", then myarray(0) should have "Andy is in Room #29."


RegExp is for searching words or substrings in a text but not for
searching sentences.


Regards
Claus B.
--
Windows10
Microsoft 365 for business


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to extract data from an array jmccaski Excel Programming 8 February 1st 11 11:58 AM
Extract multiple values from array vpinnell Excel Discussion (Misc queries) 2 October 28th 09 02:39 AM
Extract a column/row from a 2-D array Fred Chow Excel Programming 1 October 13th 07 06:46 PM
How do I extract a single value from the linest array result? hil Excel Worksheet Functions 3 July 12th 06 12:37 PM
How do I extract hyperlink as text from an array of hyperlinks? Hoya Excel Worksheet Functions 8 December 29th 05 05:16 PM


All times are GMT +1. The time now is 09:48 PM.

Powered by vBulletin® Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Copyright 2004-2021 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"

 

Copyright © 2017