Remove Empty array items in Excel VBA

maniknath

New Member
Joined
Nov 4, 2009
Messages
11
Hi All,

I'm stuck with an array problem in excel. I have an array MyArr() in excel.

Its length is from 1 to i where i takes dynamic value from varibable.

Now the problem is MyArr(1 to i) has some empty values.

Like say: if i = 5 then
MyArr(1) = "a"
MyArr(2) = ""
MyArr(3) = "b"
MyArr(4) = ""
MyArr(5) = "c"

How can I get rid of those empty elements so that MyArr() becomes only three elements long and then display them in a range of three cells.

Thanks in advance for any help.

Manik
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How did MyArr get populated in the first place?
 
Upvote 0
Code:
MyArr(1) = "a"
MyArr(2) = ""
MyArr(3) = "b"
MyArr(4) = ""
MyArr(5) = "c"

ReDim NewArr(LBound(MyArr) To UBound(MyArr))
For i = LBound(MyArr) To UBound(MyArr)
    If MyArr(i) <> "" Then
        j = j + 1
        NewArr(j) = MyArr(i)
    End If
Next i
ReDim Preserve NewArr(LBound(MyArr) To j)
Range("A1").Resize(j - LBound(NewArr) + 1) = Application.Transpose(NewArr)
 
Upvote 0
Hi xld,

thanks for the reply. but what is the value of 'j'? coz' when i run this code it return an error of "subscript out of range".

Hi Peter,

MyArr() got populated from a string and then I performed an operation which compared this array with another array and delete out the common characters. So I was left with this array which has some elements populated and some elements empty. So need to get rid of the empty elements and have an array that has only the filled elements.

Thanks for your help.

Manik
 
Upvote 0
Hi xld,

thanks for the reply. but what is the value of 'j'? coz' when i run this code it return an error of "subscript out of range".

j is a counter for the newly created array, telling it the index of the next item.
 
Upvote 0
Does your array need to be in a particular order? Sorting it would bubble all the blanks up to the top where you could bulk remove them.
 
Upvote 0
Actually, if the order is irrelevant, why not just drop the whole array on a range as is, and then sort it. The blanks then effectively disappear.
 
Upvote 0

Forum statistics

Threads
1,221,864
Messages
6,162,497
Members
451,770
Latest member
tsalaki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top