VBA methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone suggest a vba built in method that is similiar to
ArrayObject.Rows.Count and ArrayObject.Columns.Count that will count the rows
and columns of the area in the worksheet where the returned array will be
placed? I want to select the dimensions of the returned array to suit my
personal whim when I execute the VBA Function.
Les
 
Hi LesHurley,
Can anyone suggest a vba built in method that is similiar to
ArrayObject.Rows.Count and ArrayObject.Columns.Count that will count the rows
and columns of the area in the worksheet where the returned array will be
placed? I want to select the dimensions of the returned array to suit my
personal whim when I execute the VBA Function.

ubound(ArrayObject,1) and ubound(ArrayObject,2)?

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com
Member of:
Professional Office Developer Association
www.proofficedev.com
 
Jan; I dont know how to use that. I cant find "unbound" in VBE help.
Here is what I want to do:

Function Test(SomeArray)
Dim r As Integer, c As Integer
r=Test.Rows.Count 'Or some such function
c=Test.Columns.Count 'Ditto
'Some code to fill the Test Array
End Function
 
oops; I misspelled that. I did find Ubound and Lbound i help, but that
doesn't seem to apply. Can you give an example that will do what I suggest
in my last post?
 
Yeah, it applies.

r = UBound(SomeArray,1) - LBound(SomeArray,1) + 1
c = UBound(SomeArray,2) - LBound(SomeArray,2) + 1

Alan Beban
 
OK; here is what I wrote. the program compiles Ok but when run doesnt get as
far as the MsgBox before it crashes. What I did to execute it was to
highlight a blank area on the worksheet and write {=test()} and press
Ctrl+Shift+Enter as usual.

Function Test()
Dim A() As Integer
Dim i As Integer, j As Integer, r As Integer, c As Integer

r = UBound(Test, 1) - LBound(Test, 1) + 1
c = UBound(Test, 2) - LBound(Test, 2) + 1

MsgBox "Got 'em ."
ReDim A(r, c)
'For i = 1 To r
'For j = 1 To c
'A(i, j) = i + j - 1
'Next j
'Next i
Test = A



End Function
 
ps: I forgot to mention, I'm using Option Base 1
Thanks for your help
 
Well, you said you were using Function Test(SomeArray); now it turns out
you're not using some array at all, but simply selecting a range on the
worksheet. The following will work:

Function Test()
Dim A() As Integer
Dim i As Integer, j As Integer, r As Integer, c As Integer
r = Selection.Rows.Count
c = Selection.Columns.Count
'r = UBound(Test, 1) - LBound(Test, 1) + 1
'c = UBound(Test, 2) - LBound(Test, 2) + 1

MsgBox "Got 'em ."
ReDim A(r, c)
For i = 1 To r
For j = 1 To c
A(i, j) = i + j - 1
Next j
Next i
Test = A
End Function

Alan Beban
 
Hi Alan,
The following will work:

Function Test()
Dim A() As Integer
Dim i As Integer, j As Integer, r As Integer, c As Integer
r = Selection.Rows.Count
c = Selection.Columns.Count

I guess using "Selection" in a user defined function would give interesting
results <g>. Especially if I select something other than a range of cells and
Excel decides to recalc...

Regards,

Jan Karel Pieterse
Excel MVP
http://www.jkp-ads.com
Member of:
Professional Office Developer Association
www.proofficedev.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top