Using Arrays instead of this code

AlanAnderson

Board Regular
Joined
Jun 7, 2010
Messages
134
Hi,
I have never used arrays before and what I've read either scares or confuses me.

What I need to know is would the use of an array be faster and/or easier than the code below (Bit in question is between XXXX's)

This is just a very simple example to illustrate what is a far more complex issue I need to solve. If I can see how this simple examplwe would work I could then apply it to my real problem.

How would the following code translate into an effective array?


Code:
Sub Test2()
    Dim vProductCode As String
    Dim vTaxYN As String
    Dim vPrice As Double
    Dim wsProd As Worksheet
    Dim wsInv As Worksheet
    Dim FinalRowProd As Long
    Dim FinalRowInv As Long
 
    Set wsProd = Worksheets("Product")
    Set wsInv = Worksheets("Invoice")
 
    FinalRowProd = wsProd.Cells(Rows.Count, 1).End(xlUp).Row
    FinalRowInv = wsInv.Cells(Rows.Count, 1).End(xlUp).Row
    'XXXXXXXXXXX
        For i = 1 To FinalRowInv
        vProductCode = wsInv.Cells(i, 1)
            For j = 1 To FinalRowProd
                If vProductCode = wsInv.Cells(j, 1) Then
                    wsInv.Cells(i, 2) = wsProd.Cells(j, 2)
                    wsInv.Cells(i, 3) = wsProd.Cells(j, 3)
                End If
            Next j
        Next i
    'XXXXXXXXXXX
End Sub

There are just 2 worksheets in this example. "Product" - in col A is product code, Col B a "Y" or "N" and Col C a value.
In sheet "Invoice I have Col A with product code and need to fill in Cols B and C with relevant data from sheet "Product"

I hope this makes sense.

BTW I use 2003 but am trying to keep it compatible with 2007/2010

Thanks for the ongoing help from this forum

Alan
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi Aladin,
My example is very simple:

Product sheet
P1 Y 11
P2 N 12
P3 N 13
P4 Y 14
P5 N 15

The Invoice sheet starts with only col A filled in:

P1 Y 11
P3 N 13
P5 N 15
P4 Y 14
P2 N 12

I know this is VERY basic but I've deliberately built up a very simple example so that it is easier for me to see how the solution to this can be applied to the real case.

Thanks for the interest. I do appreciate it.

Regards,

Alan
 
Upvote 0
See if this helps, to me it looks like a simple match index . may be I am missing something...


Excel Workbook
DEF
21Data
22ProductAcceptQty
23P1Y11
24P2N12
25P3N13
26P4Y14
27P5N15
28
29Extracted
30ProductAcceptQty
31P1Y11
32P5N15
33P3N13
34P4Y14
35P2N12
Sheet1
#VALUE!
 
Upvote 0
Hi Snoopy,

Thank you for your help. Unfortunately the problem is a great deal more complex than the simple example I quoted. For simplicity sake I restricted example to only 3 columns and matched them against an identical col in the Invoice page. The reality is that the invoices will have a variety of codes input and depending on circumstances many calculations and look ups will need to be done.

Firstly it has to be done in VBA and not on the sheets, secondly I have a nasty feeling that I am either going to have to do plenty of loops within loops or probably use arrays.
Thank you for your suggestion.

Regards,

Alan
 
Upvote 0
Your example looked like a simple INDEX/MATCH or VLOOKUP should do it. Your description at the bottom of your first post also just described 3 columns where the data had to be retrieved based on the first column.

You now say that the problem is much more complex. My feeling is that potential helpers are, like me, wondering just what your data is like and what you are trying to do with it. Without that information it is hard for anybody to help.

So what about making up a bit more realistic sample and post some sample data, expected results and description in relation to that data/results?

Consider using one of the 3 methods in my signature block for posting sample data so that the layout is clear.
 
Upvote 0
Hi Peter,
I appreciate the point you are making - it does make sense.

However my problem is that I have written a procedure that, while it has worked every step of the way using my clumsy code, has now got too big to compile.

I really can't expect anyone on this forum to plough through this 64K of code.

So my purpose of writing this really simplistic example is that hopefully, if someone shows me how to rather do this as an array I can then apply this "lesson" to correcting the larger procedure.

Does this make sense?

Regards and thanks for the interest.

Alan
 
Upvote 0
This code example will place the values into an array (1st piece of code) - so run the code - then on the code window select 'View' then select Locals Window - now you can use the nodes to see what the array values are. The advantage of arrays is that you dont have the range limitation of 254 characters.

2nd code
So I may have an array of 600 Columns and 10K rows - The worksheetfunction.transpose will quickly place my array into my range string (Astr) in 2nd example. Not sure if this helps you - if you want all arrays to start with the 1 element (default is zero) - the in the declaration section use

Option base 1 ' This will make all arrays start at one - Excel is inconsistent - some things starts in element zero - others in 1.

Anyway - hope this helps


Range to array
Code:
Private Sub CommandButton3_Click()
Dim MyArray() As Variant
MyArray = ActiveSheet.Range("C5:C10", "D5:D10").Value 
End Sub


Array to range - Astr=my range string and SampleID is my array of data
Code:
ActiveSheet.Range(Astr).Value = Application.WorksheetFunction.Transpose(SampleID)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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