Hi all,
I am fairly new to VBA and have coded a small program that actually works, but needs a lot of execution time. I guess it is just not coded in an efficient way.
I have information of items with different characteristics and want to create a matrix that I want to analyse in MATLAB afterwards.
On my second worksheet, "O", I have the stored Information which looks like this:
ITEM OPID
001682 XYLU52T
001682 XYWT8
001682 XYInk
001682 ATL
001682 XYFDCP
003266 LCT
003266 LW1
003266 LTF
003266 LW1
003266 INK
003266 ATL
003266 FIDL
003268 CV
003268 ST2
...
On my first worksheet "M", column-wise, I have listed all items, while row-wise, I have all characteristics that any item might have. My Macro assigns an "1" if, the item has the characteristic.
001682 003266 003268
XYLU52T 1
XYWT8 1
XYInk 1
ATL 1 1 1
XYFDCP 1
LCT 1
LW1 1
...
This is my code:
Sub Matrixvalues()
Dim iValue As Integer
Dim iCounterMP As Integer REM counting Items on first Worksheet
Dim iCounterMO As Integer REM counting Characteristics on first Worksheet
Dim iCounterOP As Integer REM counting Assignments on second Worksheet
Dim iEndMP As Integer
Dim iEndMO As Integer
Dim iEndOP As Integer
iEndMP = 300
iEndMO = 568
iEndOP = 2340
For iCounterMP = 2 To iEndMP
For iCounterMO = 2 To iEndMO
For iCounterOP = 2 To iEndOP
If (Worksheets("M").Cells(1, iCounterMP) = Worksheets("O").Cells(iCounterOP, 1)) And (Worksheets("M").Cells(iCounterMO, 1) = Worksheets("O").Cells(iCounterOP, 2)) Then
Worksheets("M").Cells(iCounterMO, iCounterMP) = 1
End If
Next iCounterOP
Next iCounterMO
Next iCounterMP
I see that my nested loop needs a lot of calculations, the calculation time for this little bit was round about 2 hours! How do I make this code more efficient?
Thank you for your help!
Bookmarks