VBA and array structure

Please post any questions regarding the program here.

Moderator: 2020vision

VBA and array structure

Postby Captain Sensible » Fri Sep 09, 2011 4:09 pm

Trying to learn a bit of VBA by fiddling with some of the examples on the forum but my only coding knowledge is with php where I can easily print the array contents to screen to check my multidimensional arrays are formatted as I want them

Is there any way I can dump the vba array structures to the screen to see how they're formatted
User avatar
Captain Sensible
 
Posts: 2923
Joined: Sat Nov 19, 2005 2:29 pm

Postby dgs2001 » Fri Sep 09, 2011 7:18 pm

Captain it would probably be easier to dump your array to a spare blank sheet.

Don't do this when you go live as writing to sheets is a relatively slow process in terms of excel.

If you prefer to see it on the screen then the immediate window in the vba editor is where you need to be .

Duncan
User avatar
dgs2001
 
Posts: 334
Joined: Thu Apr 05, 2007 4:53 pm
Location: The Home Of National Hunt

Postby osknows » Fri Sep 09, 2011 8:11 pm

Hello,

If data is meant to be written back to Excel it's generally better to set up the array as 2 dimensional with base 1, eg dim ArrayX(1 to row, 1 to column). It makes it much easier to manipulation.

Having said that most 1 dimensional arrays can be written to a range either directly or using Application.Transpose(ArrayX) first.

A last resort is using a FOR loop. This link is quite useful http://www.dailydoseofexcel.com/archive ... using-vba/
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby negapo » Sat Sep 10, 2011 12:46 am

You can do a debug.print Array(i,n) and it will show the value on the imediate window. You can also drag the array (while running the code) to the watch windows and then check all the values inside the array.
negapo
 
Posts: 179
Joined: Thu Mar 19, 2009 1:17 pm
Location: Porto, Portugal

Postby Captain Sensible » Sat Sep 10, 2011 1:04 am

Thanks for the pointers, I'm so used to my arrays being in formats like

Code: Select all
Array
(
    [0] =stdClass Object
        (
            [asianLineId] =0
            [betCategoryType] =E
            [betPersistenceType] None
            [betType] =B
            [bSPLiability] =
            [marketId] =100379764
            [price] =50
            [selectionId] =5760451
            [size] =4.76
        )

    [1] =stdClass Object
        (
            [asianLineId] =0
            [betCategoryType] =E
            [betPersistenceType] None
            [betType] =B
            [bSPLiability] =
            [marketId] =100379764
            [price] =60
            [selectionId] =5228518
            [size] =4.53
        )



)


Etc

I just haven't used VBA arrays so no idea how they're formatted or even stored I just need to see how they're stored so I can get a better idea of how to access them or relevant parts later. Dumping to a text file always makes those things seem much more readable rather than having some assumption of how the dat's stored to me
User avatar
Captain Sensible
 
Posts: 2923
Joined: Sat Nov 19, 2005 2:29 pm

Postby Captain Sensible » Sat Sep 10, 2011 1:18 am

My only knowledge of programming is using php but for me as soon as I'd learnt how arrays were structured to either add to or access the info the rest was pretty much a pice of cake to do what i needed. But seeing them echoed to screen makes life alot easier to see you're on the right track when adding or pulling out the info you need , the maths etc is never the hardest part.
User avatar
Captain Sensible
 
Posts: 2923
Joined: Sat Nov 19, 2005 2:29 pm

Postby osknows » Sat Sep 10, 2011 1:48 am

The example you've given 'datawise' could be constructed many ways using VBA (and other languages)

eg

i) Variant Array
ii) Array of Arrays
iii) Range
iv) Collection / Dictionary / List

Often it depends what you intend to do with the data in the array that determines the best method to use. Eg do you need to search the array, process the whole array, slice the array etc
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby dgs2001 » Sat Sep 10, 2011 8:37 am

Speaking personally I find Variant Arrays the most usefull as a range can be written to a variant array in one read of the page and also written back to a page in one write.

Duncan
User avatar
dgs2001
 
Posts: 334
Joined: Thu Apr 05, 2007 4:53 pm
Location: The Home Of National Hunt

Postby dgs2001 » Sat Sep 10, 2011 8:52 am

Code: Select all
 Sub From_sheet_make_array()
      Dim myarray As Variant
      myarray = Range("a5:p30").Value

      'Looping structure to look at array.
      For i = LBound(myarray) To UBound(myarray)
         MsgBox myarray(i, 1)
      Next
   End Sub


if you have gruss linked to cell A1 of a sheet this will show you the names and prices etc from myarray

Duncan
User avatar
dgs2001
 
Posts: 334
Joined: Thu Apr 05, 2007 4:53 pm
Location: The Home Of National Hunt

Postby Captain Sensible » Sat Sep 10, 2011 1:55 pm

Thanks Duncan. I'll look at them later today after racing's finished
User avatar
Captain Sensible
 
Posts: 2923
Joined: Sat Nov 19, 2005 2:29 pm


Return to Help

Who is online

Users browsing this forum: Bing [Bot] and 32 guests

Sports betting software from Gruss Software


The strength of Gruss Software is that it’s been designed by one of you, a frustrated sports punter, and then developed by listening to dozens of like-minded enthusiasts.

Gruss is owned and run by brothers Gary and Mark Russell. Gary discovered Betfair in 2004 and soon realised that using bespoke software to place bets was much more efficient than merely placing them through the website.

Gary built his own software and then enhanced its features after trialling it through other Betfair users and reacting to their improvement ideas, something that still happens today.

He started making a small monthly charge so he could work on it full-time and then recruited Mark to help develop the products and Gruss Software was born.

We think it’s the best of its kind and so do a lot of our customers. But you can never stand still in this game and we’ll continue to improve the software if any more great ideas emerge.