Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Building/Creating an Array

I am trying/want to make code a procedure to search throught my worksheet
tabs in a workbook for the mrist 3 letters being MKT and if so i want them
to take that worksheets name and start to populate an array. Also i am
trying to find a way to count the number of values in an array and store
that number to a variable. If anyone know how to do this your help is
greatly appreciated. Thanks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Building/Creating an Array

This will get you started.

Code
-------------------
Sub test()

Dim w As Worksheet

For Each w In ActiveWorkbook.Worksheets
If Left(w.Name, 3) = "MKT" Then

'Do something to populate an array

End If
Next

End Su
-------------------

You will have to be more specific as to what you mean by populating a
array (what will it be used for, where will the data come from).

As to how to know how many items are in an array, that is difficult t
say. If you mean that you dimensioned an array like:

Dim strArray(100) as String

and want to get how many non-blanks are in it, you would just do this:

For i = 0 to 100
If strArray(i) = "" then Exit For
Next

iArrayLenght = i -1

Post back with more detail.



--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Building/Creating an Array

I have figured out what i need with the counting the values in an array. I
use the ubound function and the lbound function substract and add 1 becasue
of a default of 0 in the array. My question to be more specific it this. I
have a workbook with 5 worksheets: Mkt1, mkt2, mkt3, mkt4, and demog. I want
to have a procedure search the worksheet tabs/names and then create an array
with the worksheets names that begin with mkt. So in the end i will have an
array of (mkt1, mkt2...)

"kkknie " wrote in message
...
This will get you started.

Code:
--------------------
Sub test()

Dim w As Worksheet

For Each w In ActiveWorkbook.Worksheets
If Left(w.Name, 3) = "MKT" Then

'Do something to populate an array

End If
Next

End Sub
--------------------

You will have to be more specific as to what you mean by populating an
array (what will it be used for, where will the data come from).

As to how to know how many items are in an array, that is difficult to
say. If you mean that you dimensioned an array like:

Dim strArray(100) as String

and want to get how many non-blanks are in it, you would just do this:

For i = 0 to 100
If strArray(i) = "" then Exit For
Next

iArrayLenght = i -1

Post back with more detail.

K


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Building/Creating an Array

If the quantity of items is not known in advance there are 2 options:
create a dynamic array and resize it when you find something OR

if you just want to build a list of objects or names ,my preferred
method would be to use a collection object.


dim cFound as collection
dim ws as worksheet

'instantiate the new collection
set cFound = new collection
'loop thru the worksheets
For each ws in activeworkbook.worksheets
if lcase(ws.name) like "mkt*" then
cFound.add ws, ws.name
end if
Next

if cFound.count 1 then
Msgbox "I found " & cfound.count & "sheets"
end if

i suggest to read VBA help on collections.

have fun!


keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"Rich Cooper" wrote:

I am trying/want to make code a procedure to search throught my
worksheet tabs in a workbook for the mrist 3 letters being MKT and if
so i want them to take that worksheets name and start to populate an
array. Also i am trying to find a way to count the number of values
in an array and store that number to a variable. If anyone know how
to do this your help is greatly appreciated. Thanks




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Building/Creating an Array

The number of values in an array can be calculated with

myVar = UBound(aryName,1) - LBound(aryName,1) +1

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rich Cooper" wrote in message
...
I am trying/want to make code a procedure to search throught my worksheet
tabs in a workbook for the mrist 3 letters being MKT and if so i want them
to take that worksheets name and start to populate an array. Also i am
trying to find a way to count the number of values in an array and store
that number to a variable. If anyone know how to do this your help is
greatly appreciated. Thanks






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Building/Creating an Array

This should do it...

Code
-------------------
Dim strSheet() as String
Dim i As Long
Dim w AS Worksheet

Redim strSheet(1000)

i = 0
For Each w in Worksheets
If UCase(Left(w.Name,3)) = "MKT" Then
strSheet(i) = w.Name
i = i + 1
End If
Next

Redim Preserve strSheet(i-1
-------------------



--
Message posted from http://www.ExcelForum.com

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default Building/Creating an Array

Thanks for the help

One last question, i have done that now i want to insert a value at the very
end of the array. I want it to me a sheet call demog. So in the end my
array will be (mkt1, mkt2....demog) Can i use ubound to do that and if so
how? I greatly appreciate all the help.
"kkknie " wrote in message
...
This should do it...

Code:
--------------------
Dim strSheet() as String
Dim i As Long
Dim w AS Worksheet

Redim strSheet(1000)

i = 0
For Each w in Worksheets
If UCase(Left(w.Name,3)) = "MKT" Then
strSheet(i) = w.Name
i = i + 1
End If
Next

Redim Preserve strSheet(i-1)
--------------------

K


---
Message posted from http://www.ExcelForum.com/



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Building/Creating an Array

Change to read:

Code:
--------------------
Dim strSheet() as String
Dim i As Long
Dim w AS Worksheet

Redim strSheet(1000)

i = 0
For Each w in Worksheets
If UCase(Left(w.Name,3)) = "MKT" Then
strSheet(i) = w.Name
i = i + 1
End If
Next

strSheet(i) = "demog"

Redim Preserve strSheet(i)
--------------------

K


---
Message posted from http://www.ExcelForum.com/

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Building/Creating an Array

Rich Cooper wrote:

Thanks for the help

One last question, i have done that now i want to insert a value at the very
end of the array. I want it to me a sheet call demog. So in the end my
array will be (mkt1, mkt2....demog) Can i use ubound to do that and if so
how? I greatly appreciate all the help.

The number of elements in a one-dimensional array, myArray, is

UBound(myArray) - LBound(myArray) + 1

regardless of whether the lower bound is 0, 1 or something else.

To add another element you can use

Redim Preserve myArray(UBound(myArray) + 1)
myArray(UBound(myArray)) = "demog"

Alan Beban
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
Creating a list of value from an array Moanster Excel Worksheet Functions 2 June 20th 07 04:22 PM
creating an array Richard New Users to Excel 4 March 15th 07 01:06 PM
creating an array Richard Excel Worksheet Functions 4 March 15th 07 01:06 PM
Creating array formulaes in macro Raj Excel Discussion (Misc queries) 1 April 28th 05 09:20 AM
Creating an array Eric[_6_] Excel Programming 1 January 12th 04 08:25 PM


All times are GMT +1. The time now is 02:13 PM.

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

About Us

"It's about Microsoft Excel"