![]() |
Testing a string array for any values
Hi,
I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you |
Testing a string array for any values
Based on the limited description of the situation I believe you would have
to loop through each element and compare it to a null string ("") Since you have redim'd the array and haven't erased it, I don't think you can check for anything against the array variable itself If you have erased it that would be a different situation. -- Regards, Tom Ogilvy "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you |
Testing a string array for any values
Chris,
You could try If Application.CountA(ary) 0 Then 'process it End If it will fail if there are strings or numbers. -- HTH RP (remove nothere from the email address if mailing direct) "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you |
Testing a string array for any values
I don't think this will work with string arrays:
Dim myArray() As String ReDim myArray(1 To 5) MsgBox Application.CountA(myArray) I got 5 back in the msgbox. Bob Phillips wrote: Chris, You could try If Application.CountA(ary) 0 Then 'process it End If it will fail if there are strings or numbers. -- HTH RP (remove nothere from the email address if mailing direct) "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you -- Dave Peterson |
Testing a string array for any values
Good catch. It would work if the array were variant, but a string array in
initialized to "" for each element I believe. -- Regards, Tom Ogilvy "Dave Peterson" wrote in message ... I don't think this will work with string arrays: Dim myArray() As String ReDim myArray(1 To 5) MsgBox Application.CountA(myArray) I got 5 back in the msgbox. Bob Phillips wrote: Chris, You could try If Application.CountA(ary) 0 Then 'process it End If it will fail if there are strings or numbers. -- HTH RP (remove nothere from the email address if mailing direct) "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you -- Dave Peterson |
Testing a string array for any values
That's what I saw when I had a watch on that variable.
And I think it depends on what you call elements... Option Explicit Sub testme() Dim myArray() As Variant Dim iCtr As Long ReDim myArray(1 To 5) MsgBox Application.CountA(myArray) For iCtr = LBound(myArray) To UBound(myArray) myArray(iCtr) = Empty Next iCtr MsgBox Application.CountA(myArray) End Sub If I stick Empty in the array's elements, is it being used? If no, then I like Bob's response. If yes, then I don't like Bob's response. Tom Ogilvy wrote: Good catch. It would work if the array were variant, but a string array in initialized to "" for each element I believe. -- Regards, Tom Ogilvy "Dave Peterson" wrote in message ... I don't think this will work with string arrays: Dim myArray() As String ReDim myArray(1 To 5) MsgBox Application.CountA(myArray) I got 5 back in the msgbox. Bob Phillips wrote: Chris, You could try If Application.CountA(ary) 0 Then 'process it End If it will fail if there are strings or numbers. -- HTH RP (remove nothere from the email address if mailing direct) "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you -- Dave Peterson -- Dave Peterson |
Testing a string array for any values
if join(arrayName,"")="" then
'do stuff end if Tim "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you |
Testing a string array for any values
Spot on Dave. It assumes variant (there was an implicit giveaway in my
comment that it would fail if there are strings or numbers :-)). Bob -- HTH RP (remove nothere from the email address if mailing direct) "Dave Peterson" wrote in message ... I don't think this will work with string arrays: Dim myArray() As String ReDim myArray(1 To 5) MsgBox Application.CountA(myArray) I got 5 back in the msgbox. Bob Phillips wrote: Chris, You could try If Application.CountA(ary) 0 Then 'process it End If it will fail if there are strings or numbers. -- HTH RP (remove nothere from the email address if mailing direct) "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you -- Dave Peterson |
Testing a string array for any values
since he said:
I have a dynamic, zero-based, string array guess you should have stated it more directly. -- Regards, Tom Ogilvy "Bob Phillips" wrote in message ... Spot on Dave. It assumes variant (there was an implicit giveaway in my comment that it would fail if there are strings or numbers :-)). Bob -- HTH RP (remove nothere from the email address if mailing direct) "Dave Peterson" wrote in message ... I don't think this will work with string arrays: Dim myArray() As String ReDim myArray(1 To 5) MsgBox Application.CountA(myArray) I got 5 back in the msgbox. Bob Phillips wrote: Chris, You could try If Application.CountA(ary) 0 Then 'process it End If it will fail if there are strings or numbers. -- HTH RP (remove nothere from the email address if mailing direct) "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you -- Dave Peterson |
Testing a string array for any values
Guess I should.
"Tom Ogilvy" wrote in message ... since he said: I have a dynamic, zero-based, string array guess you should have stated it more directly. -- Regards, Tom Ogilvy "Bob Phillips" wrote in message ... Spot on Dave. It assumes variant (there was an implicit giveaway in my comment that it would fail if there are strings or numbers :-)). Bob -- HTH RP (remove nothere from the email address if mailing direct) "Dave Peterson" wrote in message ... I don't think this will work with string arrays: Dim myArray() As String ReDim myArray(1 To 5) MsgBox Application.CountA(myArray) I got 5 back in the msgbox. Bob Phillips wrote: Chris, You could try If Application.CountA(ary) 0 Then 'process it End If it will fail if there are strings or numbers. -- HTH RP (remove nothere from the email address if mailing direct) "Chris W." wrote in message ... Hi, I have a dynamic, zero-based, string array that I Redim often, and i want to know how do i test it to see if it actually has any elements in it before I put it thru the loop to extract the data. If I have no elements I want to exit before looping. Can I use IsEmpty to test a string Array or is it a "" value by default. I forget, and documentation is sparse on details. thank you -- Dave Peterson |
All times are GMT +1. The time now is 09:40 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com