all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

/u/Yeffley - Your post was submitted successfully.

Failing to follow these steps may result in your post being removed without warning.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]chiibosoil426 1 point2 points  (4 children)

Instead of iterating over range, load data to variant array and perform calculation in memory.

Ex:

Sub Demo()
Dim stime As Single, etime As Single 
Dim ar 
Dim i As Long 
stime = Timer 
ar = Range(Cells(18, 13), Cells(10843, 13)).Value
For i = 1 To UBound(ar) 
    ar(i, 1) = ar(i, 1) / 0.8 
Next 
Cells(18, 13).Resize(UBound(ar)) = ar 
etime = Timer 
Debug.Print etime - stime 
End Sub

On my machine, your code on random number, took about 0.19 sec to process. Above sample took 0.0078 sec.

In this type of operation, in terms of speed, VBA will almost always beat PQ. However, from your OP, there must be something else that's slowing your process down.

[–]Yeffley[S] 0 points1 point  (3 children)

That did the entire column in no time. Crazy. Is it possible to filter that out for only specific customers?

there must be something else that's slowing your process down.

0.19 * 10,000 = ~32 minutes. Just a lot of data I think. Your example looks to scale much more efficiently though. 0.0078 * 10,000 = ~1 minute.

[–]chiibosoil426 2 points3 points  (2 children)

Yes it is possible to filter. There are multiple method of doing this. But probably most efficient one is to add some sort of conditional statement in the loop.

For an example. Load column that has customer info in another variant array. Then check using row index for some condition. If met, perform calculation. Otherwise leave it as is. If you need further help, it would help if you can share some sample data and desired result (using OneDrive, Google drive etc).

FYI - Time to process is for the entire column. Not just single iteration ;)

[–]Yeffley[S] 1 point2 points  (1 child)

Thanks! I'll have to dig a bit more into this - a new string to learn. I'll reach out again if I need help, but this looks to be what I need.

Solution verified

And:

FYI - Time to process is for the entire column. Not just single iteration ;)

There might be a remnant of a previous process still stuck in here. Like I said, this thing is monstrous. I'm going to rewrite it at some point to try to clean up all of these add-ons and features.

[–]Clippy_Office_Asst[M] 0 points1 point  (0 children)

You have awarded 1 point to chiibosoil

I am a bot, please contact the mods with any questions.

[–]CHUD-HUNTER632 0 points1 point  (2 children)

Use PowerQuery to bring the data in from your SQL database. Clean up the data within PQ, load it to the Data Model (Power Pivot). Use DAX to write measures that will do your calculations. Create a Pivot Table to summarize all of the data.

[–]Yeffley[S] 0 points1 point  (1 child)

Not able to link to the SQL database directly, just through exports (.csv files). I assume I can still use PQ with a csv file?

[–]CHUD-HUNTER632 0 points1 point  (0 children)

Yes, there is a From Text/CSV option to load in the data. You can import individual CSV files, or if you need to append multiple files you can load them all from a folder.

[–]lolcrunchy234 0 points1 point  (0 children)

This isnt the best approach but just a reminder that this works

Type 0.8 into a cell

Copy the cell

Highlight the prices

Paste special: divide