Consulting

Results 1 to 17 of 17

Thread: Array manipulation

  1. #1

    Array manipulation

    I'm not gonna really take the time to really learn vba seeing as how I don't have much need for it in the real world, and I really hate this language (in general I don't really like HLL's, and really prefer MLL's and the C syntax). So all I have is a working knowledge of this thing.

    Anyway, I'm trying to do some general array manipulations, so as the parameter I specify that it accepts an array of type variant, such as the following:
    Sub MyArrayFunction(SomeParameter() As Variant)
    The problem is when I do something like:
    Dim MyArray As String
     
    MyArrayFunction MyArray
    During the "compiling" process I encounter an error due to a type mis-match. It says it's looking for an array or user-defined type. So I was wondering, how can I create array manipulating procedures regardless of the array data type.

    Also, I've tried using Object instead of Variant, and it produced the same results.

  2. #2
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Hi Berticus & Welcome to VBAExpress!

    The following works for me:

    [vba]Sub Testit()
    Dim myArray as string
    MyArrayFunction MyArray
    Debug.Print UBound(MyArray)
    End Sub

    Sub MyArrayFunction(SomeParameter As Variant)
    ReDim SomeParameter(1 to 10)
    End Sub[/vba]

    Best regards

    Richard

  3. #3
    Hey, thanks for the quick reply. I didn't do the second code block correctly. MyArray is suppose to start out as an array of type String. So more appropriately:
    Dim MyArray() As String
     
    MyArrayFunction MyArray
    Basically the array goes in, and it gets manipulated regardless of type.

  4. #4
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Sorry, I posted that incorrectly - you just need to leave off the parentheses from the variant argument to the sub (and have myarray declared as a dynamic array which you have done in your second code)

    [vba]Sub Testit()
    Dim myArray() As String
    MyArrayFunction MyArray
    Debug.Print UBound(MyArray) 'demonstrate it has worked
    End Sub

    Sub MyArrayFunction(SomeParameter As Variant)
    ReDim SomeParameter(1 To 10) 'do something with passed in reference
    End Sub[/vba]

    Make sense?

    Richard

  5. #5
    Ah, I see. Thanks

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Berticus
    I'm not gonna really take the time to really learn vba seeing as how I don't have much need for it in the real world, and I really hate this language (in general I don't really like HLL's, and really prefer MLL's and the C syntax). So all I have is a working knowledge of this thing.
    I don't know, is it me, or is that the sort of opening sentence that makes me thing, ooh, I must rush and help this guy as he has such a good attitude.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Hehe... it's not just you. I was thinking the same thing, but perhaps he had a little too much sun yesterday during the Holiday celebrations and was experiencing "fried brain" syndrome or something.

    Everyone has bad days... hopefully that's all it was.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  8. #8
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Quote Originally Posted by xld
    I don't know, is it me, or is that the sort of opening sentence that makes me thing, ooh, I must rush and help this guy as he has such a good attitude.
    Yep it's you: you're just a grumpy old bugger, Bob!

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I know, but I know a good gin when I see one (and I certainly haven't seen one from you yet).
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by xld
    I don't know, is it me, or is that the sort of opening sentence that makes me thing, ooh, I must rush and help this guy as he has such a good attitude.
    LOL! The only reason I scrolled down past that line is because I knew that there were already replies. I was curious who actually bit on it, as I don't think I would have bothered.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  11. #11
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    I'm munching on a great Chinese Take-Away and pouring some Saffron Gin over ice and a slice... I've had enough tonight so the rest of the bottle
    goes out to you guys for all your help and patience in the past .

    ENJOY

    BTW... What 'Real World' are you in Bert? Are we just a figmant of your dreams?
    Semper in excretia sumus; solum profundum variat.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Thankyou paulked ... to you.

    Another Welsh colleague, and a gin drinker to boot.

    From a gin drinking (and a few other things as well), Welsh descendant.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    The reason for that little introduction was, it was a simple little thing and just needed a quick answer. Typically those basics should have been covered before someone even starts coding. At least that's what happened when I learned data types in C, C++, and Java. But for those languages I took "formal" classes, I suppose you would say. With VB(A), I just have a working knowledge. I thought I would be introduced with criticism. At least I would've criticized a person who really wanted to learn a language about them rushing in to learn a language and start coding right away instead of sitting down to actually get to know the language. Didn't think it would be advanced with criticism.

    I don't plan to stick with VBA too much. Possibly just this summer for my job, but that's it (maybe next summer too). I'm teaching myself MatLab, and I prefer OOo2 over the Microsoft Office Suite at home/school (Regular Linux user). OOo2 has a different language than MS Office Suite for macro coding. I just learned about macros a few weeks ago, so I haven't taken a look into OOo2's choice of language. And when I meant "Real World," I just mean something that pertains to my field and that I'd actually use regularly. I'm just using VBA at a summer job.

  14. #14
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Well... we'll give you points for honesty. Generally, though, the more aggressive someone appears, the less likely that they'll get responses from us.

    We like to think that we're a pretty friendly group here generally, and we like to help people who need it. We're also not the type to blast someone because they're ignorant of VBA in any way. Many of us learned our craft by asking questions at this very site, and are painfully familiar with how hard it can be to get the compiler to do what you actually want.

    If you need more help, feel free to come back an visit us. I'd suggest maybe just letting people know that you could do this in other languages but struggle with the VBA syntax, rahter than tell us you don't care to learn.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I cannot just sit and take this.

    The guy has dissed Excel, he has dissed VBA, but has taken a job where he is expected to use them. But rather than being honest and either quitting an earning his living in his version of the 'real world', or knuckling down and working at it, he has just said that he can't be a**#d, and chucked it at us to earn his salary for him. Sorry, but no!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Bob, I can actually visualize the look on your face as you read that!

    You know... I've spent all day trying to be reasonable and understanding of fault. I was trying to put aside the dissing of the products that we love, but maybe I've gone a bit too far...

    For reference, I wasn't actually trying to give lessons on how to fool us into helping. Rather it was intended to be advice on how to actually get help in the first place. I can't govern intent and certainly do not support throwing jobs at volunteers rather than learning to do it yourself.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Ken,

    I don't mind people being critical of Excel, VBA, or MS, God knows you have been, I have been, and so we should, always endeavour to keep them on their toes.

    But I am not going to help anyone who basically says '... hey losers, sort out my problem, I can't be bothered with these pathetic tools'.

    I would rather spend 3 hours trying to understand and help someone who can't properly articulate a problem but really wants to master their brief. The first posting was bad, the justification just told me they don't give a !#*?, so I am not going to either. Call me perverse, but I am not going to assist this attitude.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •