VBA Two Dimensional Array Search in Memory (MS Excel 2007)

byehp

New Member
Joined
Mar 31, 2011
Messages
10
Hi Gurus,

May I ask if is it possible to search in a two dimensional array in memory in MS Excel 2007?
What I mean is that my array table values were searched cells which are scattered in the Excel sheet (just one worksheet). Or I need to print the array table then VLOOKUP on it?

To elaborate, the main functions should be:
1. VBA searches for the Company Code and Company Description (Columns B and C) - I've succcessfully captured the two dimensional arrays but can it be improved?
NOTE: The number of Company Codes is dynamic

Code:
Dim strHeadCoCode As String, strHeadCoDesc As String 'Two variables for Company Code and Description
Dim vCompany As Variant, Sws As Worksheet
Dim lngArrCount As Long, lngRw As Long, intCount As Integer

ReDim vCompany(1 To lngRw, 1 To 2)

    'Array for Company Code and Company Code Description
    For lngArrCount = 1 To lngRw
        With Sws
            If .Cells(lngArrCount, "A").Value = "Parent:" And Len(.Cells(lngArrCount, "B")) = 4 Then 'Condition to get the Company Code and Description
                'Table Array
                vCompany(intCount, 1) = .Cells(lngArrCount, "B").Value
                vCompany(intCount, 2) = .Cells(lngArrCount, "C").Value
                Debug.Print (vCompany(intCount, 1)) 'Test if all Company Codes were captured
                Debug.Print (vCompany(intCount, 2)) 'Test if all Company Code Descriptions were captured
                intCount = intCount + 1
                lngArrCount = lngArrCount + 1
            End If
        End With
     Next

2. Search the matching variable Company Number among the searched Company Number and Company Description array table. Company Number will be the search value and the output should be the Company Code Description.

Hope anyone could help,
- bye
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I think it would be useful if you showed an example of your worksheet with the existing data and then show us, for that same example data, the output you want from it.
 
Upvote 0
Hi Rick,

Thanks for your prompt response.
Here's a sample input and output file (I've used my "unfinished macro" - I've manually inputted the "Company Code Description" for both output sheets: "1195" and "3333").

In the sample input sheet, the Company Code appears before it has been declared as a parent or the Description (Company Code "3333").
For ease, I've manually highlighted the two company codes (both in the input and output) to show mapping from the input sheet.

*Need help, I can't seem to find the attach file function.. I can email it to you just in case.

Cheers,
- bye
 
Upvote 0
This is harder to undestand than having the attached file but hope this sample helps..
I am able to find the Company Code (strHeadCoCode) but I've just manually inputted the "Company Code Description" on the first row per sheet.
Bold font for the two Company Codes.

INPUT WORKSHEET:
xxxx</SPAN></SPAN>

xxxxx</SPAN></SPAN>







DUMMY Company</SPAN></SPAN>











Sample Report</SPAN></SPAN>

Elephant</SPAN></SPAN>















Base</SPAN></SPAN>

. . . Document . . .</SPAN></SPAN>





Inv Date</SPAN></SPAN>

Co</SPAN></SPAN>

Curr</SPAN></SPAN>

Ty</SPAN></SPAN>

Number</SPAN></SPAN>

Customer PO / Ref.</SPAN></SPAN>

Parent:</SPAN></SPAN>

1234567</SPAN></SPAN>

Company Number But In 7 Digits</SPAN></SPAN>







Customer:</SPAN></SPAN>

1234567</SPAN></SPAN>

Custome Number 1</SPAN></SPAN>







28/10/2012</SPAN></SPAN>

2222</SPAN></SPAN>

IDR</SPAN></SPAN>

RI</SPAN></SPAN>

88888888</SPAN></SPAN>

Sample PO Ref 1</SPAN></SPAN>

3/11/2012</SPAN></SPAN>

2222</SPAN></SPAN>

IDR</SPAN></SPAN>

RI</SPAN></SPAN>

88888888</SPAN></SPAN>

Sample PO Ref 2</SPAN></SPAN>

USD</SPAN></SPAN>

U.S. Dollar</SPAN></SPAN>









1234567</SPAN></SPAN>

Company Number But In 7 Digits</SPAN></SPAN>









1193</SPAN></SPAN>

Company Code 1193</SPAN></SPAN>









XXXXXXX</SPAN></SPAN>















Base</SPAN></SPAN>

. . . Document . . .</SPAN></SPAN>





Inv Date</SPAN></SPAN>

Co</SPAN></SPAN>

Curr</SPAN></SPAN>

Ty</SPAN></SPAN>

Number</SPAN></SPAN>

Customer PO / Ref.</SPAN></SPAN>

Parent:</SPAN></SPAN>

2222</SPAN></SPAN>

Second Company </SPAN></SPAN>







Customer:</SPAN></SPAN>

2222223</SPAN></SPAN>

Customer Number Two</SPAN></SPAN>







25/09/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

DN</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 3</SPAN></SPAN>

25/10/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

DN</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 4</SPAN></SPAN>

1/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 5</SPAN></SPAN>

5/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 6</SPAN></SPAN>

5/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 7</SPAN></SPAN>

5/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 8</SPAN></SPAN>

12/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 9</SPAN></SPAN>

12/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 10</SPAN></SPAN>

26/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 11</SPAN></SPAN>

26/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 12</SPAN></SPAN>

27/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 13</SPAN></SPAN>

27/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 14</SPAN></SPAN>

27/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 15</SPAN></SPAN>

27/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

RI</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 16</SPAN></SPAN>

28/11/2012</SPAN></SPAN>

3333</SPAN></SPAN>

MYR</SPAN></SPAN>

DN</SPAN></SPAN>

99999999</SPAN></SPAN>

Sample PO Ref 17</SPAN></SPAN>

USD</SPAN></SPAN>

U.S. Dollar</SPAN></SPAN>









2222223</SPAN></SPAN>

Customer Number Two</SPAN></SPAN>









Parent:</SPAN></SPAN>

3333</SPAN></SPAN>

Third Company</SPAN></SPAN>







Customer:</SPAN></SPAN>

1111113</SPAN></SPAN>

Third Customer</SPAN></SPAN>







18/01/2012</SPAN></SPAN>

1195</SPAN></SPAN>

MYR</SPAN></SPAN>

DN</SPAN></SPAN>

55555555</SPAN></SPAN>

Sample PO Ref 18</SPAN></SPAN>

13/03/2012</SPAN></SPAN>

1195</SPAN></SPAN>

MYR</SPAN></SPAN>

DN</SPAN></SPAN>

55555555</SPAN></SPAN>

Sample PO Ref 19</SPAN></SPAN>


<TBODY>
</TBODY>

OUTPUT WORKSHEET ("2222"):

Second Company</SPAN>
2222</SPAN>
Intercompany AR Detail</SPAN>
Customer Number</SPAN>
Inv Date</SPAN>
Base Curr</SPAN>
Due Date</SPAN>
Foreign Curr</SPAN>
Foreign Open</SPAN>
1234567</SPAN>
Company Number But In 7 Digits</SPAN>
1234567</SPAN>
16/01/2013</SPAN>
IDR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
1234567</SPAN>
16/01/2013</SPAN>
IDR</SPAN>
16/01/2013</SPAN>
USD</SPAN>

<TBODY>
</TBODY>


OUTPUT WORKSHEET ("3333"):
Third Company</SPAN>
3333</SPAN>
Intercompany AR Detail</SPAN>
Customer Number</SPAN>
Inv Date</SPAN>
Base Curr</SPAN>
Due Date</SPAN>
Foreign Curr</SPAN>
Foreign Open</SPAN>
2222</SPAN>
Second Company </SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>
2222223</SPAN>
16/01/2013</SPAN>
MYR</SPAN>
16/01/2013</SPAN>
USD</SPAN>

<TBODY>
</TBODY>
 
Upvote 0
Hi Gurus,

To help in understanding my request (excuse me for this follow up email)..
What's Needed:
Search the strHeadCocode (Company Code) among the searched Array table (based on the VBA code mentioned earlier).
Then output the strHeadCoDesc (Company Description).

The strHeadCocode (Company Code) has been successfully searched and also the Array table successfully captured all the Company Code and Company Description (just in memory though). Just one problem, unable to MATCH or VLOOKUP the Company Code for the Company Description to be displayed.

If searching in array memory is not possible, would you suggest to output the table in a input sheet so that I can just search (using MATCH or VLOOKUP?) it? If so, could you help me understand how to do it?
- bye
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,285
Members
449,218
Latest member
Excel Master

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