Array questions VBA (EDUCATIONAL)

Masha92

Board Regular
Joined
Jan 27, 2019
Messages
51
Office Version
  1. 365
Hi ALL,

Hope you are doing well!

I am new to VBA(started beginning of January) but i made great progress (in my opinion). I have a couple of questions that will help me greatly in my career.

a) I have two dynamic array (1D) A, B. I need to know what is in array A and not in B, the result would be added to array C. And what is not in Array A but it is in Array B, the result would be added to array D.

b) I have a 2D array. How to vlookup inside the 2d Array?

d) How to test if a certain value is in a 1D array?

c) I have duplicated values in a 1D array, how to remove the duplicate ones?

Please note that, i need the solution/code to address exactly what is needed. Ie no collections or any other solution that would do the same results. I need to understand and manipulate arrays. And no other pages, i just need the codes with explanation.

If you can add explanatory comments, i would really appreciate it.


Many thanks to you all! I wish one day i will be like you and help others!
Masha
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
re D:
If the array is of strings or number, you can use MATCH to find if an element is in an array

Code:
If IsNumeric(Application.Match(someElement, myArray,0)) Then
    MsgBox "Its there !"
Else
    MsgBox "not"
End If
Note that if the term isn't there, Application.Match will return an error value that can be tested. Application.WorksheetFunction.Match will cause a VB error that will crash the routine.

re C:
To remove duplicates from an Array. (Actually to create a new array without duplicate, after creation, that new array could be assigned to the old variable.)

Code:
Dim arrNoDuplicates as Variant, Pointer as Long

Redim arrNoDuplicates(LBound(rawArray) to UBound(rawArray))

Pointer = LBound(rawArray) - 1

For i = LBound(rawArray) to UBound(rawArray)
    If IsError(Application.Match(rawArray(i), arrNoDuplicates, 0) Then
        Pointer = Pointer + 1
        arrNoDuplicates(Pointer) = rawArray(i)
    End If
Next i

ReDim Preserve arrNoDuplicates(LBound(rawArray) to Pointer)
 
Last edited:
Upvote 0
Thank you so much for your reply.

For C:

What is Rawarray? I am a bit confused. You didnt declare this variable.

Another thing, is it possible to do it without using Excel functions? I.e Application.worksheetfunction...etc

Many thanks!
Masha
 
Upvote 0
You can filter a 1d array like
Code:
Sub Masha92()
   Dim Ary1 As Variant, Ary2 As Variant, Ary3 As Variant
   Dim i As Long
   
   Ary1 = Array("John", "Paul", "George", "Ringo")
   Ary2 = Array("Peter", "Paul", "Mary")
   Ary3 = Ary2
   For i = 0 To UBound(Ary1)
      Ary3 = Filter(Ary3, Ary1(i), False, vbTextCompare)
   Next i
End Sub
But this will filter on partial matches, rather than an exact match
 
Upvote 0
Thank you so much for your reply.

For C:

What is Rawarray? I am a bit confused. You didnt declare this variable.

Another thing, is it possible to do it without using Excel functions? I.e Application.worksheetfunction...etc

Many thanks!
Masha

rawArray is a variable that I created to represent the mixed array that the remove-duplicates routine works on.

As far as avoiding Excel functions, why? you are working in Excel VBA and those functions are as "legit" (and perhaps faster) than the other VBA functions or routines.
 
Last edited:
Upvote 0
Because I am a bit good with excel functions. I need to practice more on VBA functions and techniques. So i am aware of worksheetfunction.match...etc

But thanks anyway :)
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,214
Members
453,024
Latest member
Wingit77

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