Solved

Remove first column from a 2d array

Posted on 2014-12-12
4
128 Views
Last Modified: 2014-12-15
Is it possible using VBA only (no copying to excel etc.) to remove the first column in both the the first and second dimension?

Sample array
MyArr(1, 1) =  "Apple"
MyArr(1, 2) =  "Bannana"
MyArr(1, 3) =  "Pear"
MyArr(1, 4) =  "Orange"
MyArr(1, 5) =  "Cherry"

Desired result
MyArr(1, 1) =  "Bannana"
MyArr(1, 2) =  "Pear"
MyArr(1, 3) =  "Orange"
MyArr(1, 4) =  "Cherry"
0
Comment
Question by:MacroShadow
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40495999
HI,

pls try

Option Base 1
Sub Macro()

myArr(1, 1) = "Apple"
myArr(1, 2) = "Bannana"
myArr(1, 3) = "Pear"
myArr(1, 4) = "Orange"
myArr(1, 5) = "Cherry"
For i = 1 To UBound(myArr, 2) - 1
  myArr(1, i) = myArr(1, i + 1)
Next i
ReDim Preserve myArr(1, UBound(myArr, 2) - 1)

End Sub

Open in new window

Regards
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40496037
And why not simplify

myArr(1) = "Apple"
myArr(2) = "Bannana"
myArr(3) = "Pear"
myArr(4) = "Orange"
myArr(5) = "Cherry"
For i = 1 To UBound(myArr) - 1
  myArr(i) = myArr(i + 1)
Next i
ReDim Preserve myArr(UBound(myArr) - 1)

Open in new window

Regards
0
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 500 total points
ID: 40497806
    Dim x, y, i As Long, MyArr()
    
    ReDim MyArr(1 To 2, 1 To 5)
    
    MyArr(1, 1) = "Apple"
    MyArr(1, 2) = "Bannana"
    MyArr(1, 3) = "Pear"
    MyArr(1, 4) = "Orange"
    MyArr(1, 5) = "Cherry"
    
    MyArr(2, 1) = "Apple"
    MyArr(2, 2) = "Bannana"
    MyArr(2, 3) = "Pear"
    MyArr(2, 4) = "Kris"
    MyArr(2, 5) = "Sam"
    
    x = Evaluate("Row(1:" & UBound(MyArr, 1) & ")")
    y = Evaluate("transpose(Row(2:" & UBound(MyArr, 2) & "))")
    MyArr = Application.Index(MyArr, x, y)

Open in new window

0
 
LVL 27

Author Comment

by:MacroShadow
ID: 40498075
@Rgonzo1971
 It cannot be simplified because the array is populated from a range (MyArr = Sheets(1).Range("A1:G56").Value) which always creates a 2d array.
MyArray may contain several rows, your code won't work in such a case.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now