p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Excel VBA (http://p2p.wrox.com/forumdisplay.php?f=79)
-   -   arrays (http://p2p.wrox.com/showthread.php?t=34815)

ozPATT November 2nd, 2005 11:50 AM

Hi again,

could someone help me out with an array in vba?

I have about 15 words that I want to search for in an excel spreadsheet.

for each word in the array, i want to search the sheet for it, and if it finds it, add 1 to a counter. when the sheet has searched for that word, i want to assign the number to a variable, reset the number to 0, and start counting the next word.

arrays baffle me at the best of times, and as i still don't know too much about vba arrays, if someone could help me tht would be great. just about to google it, but would still appreciate any help that is on offer.



Visit my site: http://www.drybonesuk.com

acdsky November 3rd, 2005 02:11 AM


Arrays are pretty straight forward. You could do something like this:

Dim MyArr as Variant
'if you want to load 2 values into the array e.g "A" and "B"
ReDim MyArr(0,1) 'Here you tell the array that you will have one object/row of data (The 0) which will contain 2 values


To Set the data in the array:

MyArr(0,0) = "A"
MyArr(0,1) = "B"

To Retrieve the values:

ValueA = MyArr(0,0)
ValueB = MyArr(0,1)

If you do multiples rows of data declare the Array as:

Redim MyArr(1,1)

Now you can load (0,0) and (0,1) and also a second row/set of data into (1,0) and (1,1) the rest is similar.

So in your case your code will look something like this:

Dim MyArr as Variant
ReDim MyArr(0,14)

MyArr(0,0) = "Word1"
MyArr(0,1) = "Word2"

etc etc...
'Now loop through the array, get the word, do the search and increment your counter.
n = 0 'n is the counter
For i = 0 To 14
 MySearchText = MyArr(0,i)
    '***Do excel Search logic. Assuming you loop through all the cells
        ' ""If MySearchText in Found then""
           n = n + 1
         '"""End the if""
    n = 0

Hope this helps...lemme know if you need more info.


ozPATT November 4th, 2005 06:11 AM

thats fine thanks. used that as a base and played around a bit and have it working fine now. my code is not very dynamic, but hey, at least it does the job for now. :o)


Visit my site: Dry Bones Internet Solutions

All times are GMT -4. The time now is 04:41 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.