Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'm reading Excel files with a Visual Basic 6.0 program. In several cases,
I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? -- In theory, there is no difference between theory and practice; in practice, there is. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Consider Application.Counta(arrayName)
Alan Beban LabElf wrote: I'm reading Excel files with a Visual Basic 6.0 program. In several cases, I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
What about using error trapping?
On Error Resume Next X = ArrayName(0) If Err.Number < 0 Then 'it's not dim'd End If On Error Goto 0 PS: Nothing is used only with object variables, not "regular" variables and arrays. On Mon, 12 Sep 2005 14:05:03 -0700, LabElf wrote: I'm reading Excel files with a Visual Basic 6.0 program. In several cases, I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Alan -
Thank you for your help. Unfortunately, the CountA function gave me "Compile Error: Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions". This error probably occurs because my array contains elements of a type I defined; the method might work for my other arrays. Can someone point me toward more information on public object modules? I couldn't find much enlightenment in the online VB help. -- In theory, there is no difference between theory and practice; in practice, there is. "Alan Beban" wrote: Consider Application.Counta(arrayName) Alan Beban LabElf wrote: I'm reading Excel files with a Visual Basic 6.0 program. In several cases, I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Myrna -
Thank you, that worked for my situation. I'm only using the array once. I don't know if there's a way to completely clear the array so that it would be undefined again, if I wanted to use it over. -- In theory, there is no difference between theory and practice; in practice, there is. "Myrna Larson" wrote: What about using error trapping? On Error Resume Next X = ArrayName(0) If Err.Number < 0 Then 'it's not dim'd End If On Error Goto 0 PS: Nothing is used only with object variables, not "regular" variables and arrays. On Mon, 12 Sep 2005 14:05:03 -0700, LabElf wrote: I'm reading Excel files with a Visual Basic 6.0 program. In several cases, I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Take a look at Erase in VBA's help.
LabElf wrote: Myrna - Thank you, that worked for my situation. I'm only using the array once. I don't know if there's a way to completely clear the array so that it would be undefined again, if I wanted to use it over. -- In theory, there is no difference between theory and practice; in practice, there is. "Myrna Larson" wrote: What about using error trapping? On Error Resume Next X = ArrayName(0) If Err.Number < 0 Then 'it's not dim'd End If On Error Goto 0 PS: Nothing is used only with object variables, not "regular" variables and arrays. On Mon, 12 Sep 2005 14:05:03 -0700, LabElf wrote: I'm reading Excel files with a Visual Basic 6.0 program. In several cases, I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? -- Dave Peterson |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Ah, thank you. That fills in the blanks. (I *thought* I remembered seeing
something along those lines before...) -- In theory, there is no difference between theory and practice; in practice, there is. "Dave Peterson" wrote: Take a look at Erase in VBA's help. LabElf wrote: Myrna - Thank you, that worked for my situation. I'm only using the array once. I don't know if there's a way to completely clear the array so that it would be undefined again, if I wanted to use it over. -- In theory, there is no difference between theory and practice; in practice, there is. "Myrna Larson" wrote: What about using error trapping? On Error Resume Next X = ArrayName(0) If Err.Number < 0 Then 'it's not dim'd End If On Error Goto 0 PS: Nothing is used only with object variables, not "regular" variables and arrays. On Mon, 12 Sep 2005 14:05:03 -0700, LabElf wrote: I'm reading Excel files with a Visual Basic 6.0 program. In several cases, I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? -- Dave Peterson |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I hate to break something just to make it right. Using error trapping for
this purpose makes sense to the person who wrote the code but it is an inconvenience for anyone else reading your code. I propose that one uses a boolean flag (e.g., isArrayEmpty) to see if the array was set or if any items were inserted at the point where the array is created. Later you just test the flag. Ed "Myrna Larson" wrote: What about using error trapping? On Error Resume Next X = ArrayName(0) If Err.Number < 0 Then 'it's not dim'd End If On Error Goto 0 PS: Nothing is used only with object variables, not "regular" variables and arrays. On Mon, 12 Sep 2005 14:05:03 -0700, LabElf wrote: I'm reading Excel files with a Visual Basic 6.0 program. In several cases, I'm building dynamic arrays with the elements I find. I would like an elegant way to determing if anything has been put into my dynamic array yet. Before the array has been ReDim-ed the first type, attempts to use LBound or UBound give subscript out of range errors. The debugger shows me "arrayName = Nothing" before the array is ReDim-ed. Attempting to test arrayName = Nothing gives "Invalid Use of Object" error. Attempting to test arrayName Is Nothing gives "Type Mismatch" error. I've been working around this by Redimensioning the array as (0 To 0) until the first use, then Redimensioning as (1 to 1) for the first element, (1 to 2) next, etc., but this is clunky and using 1-based subscripts isn't portable to VB.Net, which I may do in the future. I don't know an easy way of determining if a (0 to 0) array is empty or contains one element, without using some dummy value. Does anyone know a better way? |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
How can I test if a cell is empty? | Excel Discussion (Misc queries) | |||
Test if the range is empty | Excel Discussion (Misc queries) | |||
Test for an empty selection | Excel Programming | |||
Can't test for Empty objects in an array | Excel Programming | |||
Better way to test for empty Recordset | Excel Programming |