Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 09-03-2023, 10:35 PM
Guessed's Avatar
Guessed Guessed is offline Any way to test if item in an array is in an ArrayList? Windows 10 Any way to test if item in an array is in an ArrayList? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,185
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

My solution is based off a post I once saw on another forum (probably Woody's or Eileen's lounge) although I didn't have the good manners or time to go and find it to give kudos to whoever it was that first thought to join an array.

Making functions like this gets me all pedantic on a never ending loop of 'what ifs'. I agree it would work as a function but I would add a couple of alterations to make it slightly more useful in edge situations.

I think the separator would need to be an optional component (in case '|' is used in the dictionary values). Also, I think you need the varString (and InStr find) to start and finish with the separator so you don't get false hits eg "Blue Berries|Fig|" would say that "Berries|" is a hit although I would consider it a miss.

This includes those modifications to Greg's function to deal with those minor issues
Code:
Function fcnDicValueExists(oDicPassed, varValue, Optional sSep As String = "|") As Boolean
'Function by Greg Maxey
'Created from suggestions offered by Andrew Lockton in the Word VBA Forum.
Dim oArrList As Object, varMembers, varString
  'Convert dictionary list members to an ArrayList
  Set oArrList = CreateObject("System.Collections.Arraylist")
  oArrList.Add oDicPassed.Items
  varMembers = oArrList.ToArray  'Convert ArrayList to array
  varString = sSep & Join(varMembers(0), sSep) & sSep  'Join array element members to a delimited string
  fcnDicValueExists = InStr(varString, sSep & varValue & sSep) > 0  'Test if value found in string
lbl_Exit:
  Exit Function
End Function
FWIW, you could go to town and add another option for case sensitivity.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
 

Tags
arraylist contains, dictionary, indexof

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test, just a test Thomasordef Office 0 07-25-2019 10:43 AM
Student's t-test (T.TEST): how to get p value? seanspotatobusiness Excel 1 07-20-2018 05:46 AM
Any way to test if item in an array is in an ArrayList? How do I make a checkbox or dropdown item reference data in that specific section of the data array? dhare Excel Programming 2 02-24-2016 12:36 PM
Any way to test if item in an array is in an ArrayList? Copying data related to one item to worksheet with many instances of the same item nmp13 Excel 3 02-06-2016 02:13 AM
Any way to test if item in an array is in an ArrayList? Convert String Array to Integer Array from a User Input? tinfanide Excel Programming 4 12-26-2012 08:56 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:33 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft