Change Base 1 Array to Base 0 Array
Asked Answered
U

1

9

I'm retrieving data from Excel and would like to keep my arrays 0 based but Excel returns 1 base. Is there a fairly simple way to return change the array from 1 to 0 base? Or do I just need to create a loop?

Here's an example code right here:

dim oData(,) as object
dim rng as range
dim wks as worksheet = xlApp.Activeworkbook.sheets(Sheet1)

rng=wks.Range("A1:B2")

oData=rng.Value2
Unbuckle answered 31/1, 2012 at 20:12 Comment(4)
Yes - loop. I'm not sure it's a good practice though - anyone familiar with how VBA works will know that the variant array from Range().Value is 1-based: if you change it to 0-based it may cause more problems than it solves, particularly if soemone else ends up maintaining your code.Rycca
Yes, I wasn't sure how it was done in VB.NET, I'm using a library that I made from Excel 2003 execution file and thought it might be different because of that. I know Excel-DNA returns a zero based array, so I guess that is why I assumed it was standard to be 0-based in .NET. I guess I'll stick with 1-based array. If you put your comment as an answer I'll mark it as correct, unless someone else comes by with a better answer between now and then.Unbuckle
@TimWilliams, I just noticed you were talking about VBA. So I guess you didn't answer my question for .NET. Well, maybe someone else will give it a shot!Unbuckle
I was mostly talking about changing the "native" behavior of Excel-derived objects. However, there are some suggestions in a previously-asked version of your question here: #1498266Rycca
M
8

A loop is the simplest option.

Dim target as string(0 to oData.Length - 1)

For index = 1 to oData.Length 
   target(index - 1) = oData(index)
Next

Thats from memory and not tested but it's obvious enough.

Minnesinger answered 23/5, 2012 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.