Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 89212

Optimizing macro for organizing data

$
0
0

I've got some csv export from a Database that I want to organise, I've made a VBA macro in excel that takes around 40 min to finish and I would like to know how to optimise it (Mainly to learn).

Imagine that you got different fruit shops that sells different fruits and the csv you get is like:

Worksheets("Temp1")=

Shop 1 ¦ Apple ¦ 10
Shop 1 ¦ Melon ¦ 20
Shop 2 ¦ Apple ¦ 30
Shop 3 ¦ Mango ¦ 40
Shop 1 ¦ Mango ¦ 50

I've already created a sheet like:

Worksheets(NameOfWorkbook) =

¦Shop 1¦Shop 2¦Shop 3 
Apple
Melon
Mango

And I want a macro that populate the last sheet like:

Worksheets(NameOfWorkbook) =

¦Shop 1¦Shop 2¦Shop 3 
Apple ¦10    ¦30
Melon ¦20    ¦
Mango ¦50    ¦¦40

So the macro I use is a triple for loop as:

For i = 1 To 1500
    For j = 1 To 150
       For k = 1 To 300
       If Worksheets("Temp1").Cells(i, 1) = Worksheets(NameOfWorkbook).Cells(1, j) And Worksheets("Temp1").Cells(i, 2) = Worksheets(NameOfWorkbook).Cells(k, 1) Then
            Worksheets(NameOfWorkbook).Cells(k, j) = Worksheets("Temp1").Cells(i, 3)
           End If
      Next k
    Next j
Next i

I would like to know away to optimize the code, any help would be much appreciated.

Thanks very much.

Kind Regards.


Viewing all articles
Browse latest Browse all 89212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>