I am very new to the InStr and this was what I was able to create, but it is slow. Is there something else I should have used? How do I speed it up?
![]()
Please Login or Register to view this content.
I am very new to the InStr and this was what I was able to create, but it is slow. Is there something else I should have used? How do I speed it up?
![]()
Please Login or Register to view this content.
Part two
![]()
Please Login or Register to view this content.
I wonder if it would speed up if you made the items you are searching an array and place the array in the Instr function. It would definitely shorten the codes(s).
Alan עַם יִשְׂרָאֵל חַי
Change an Ugly Report with Power Query
Database Normalization
Complete Guide to Power Query
Man's Mind Stretched to New Dimensions Never Returns to Its Original Form
I wondered about that, but since each search goes has a different destination, i didn't know where to begin.
Use the If-Then-Else aspect of VBA. Create a different Range to search for each array and location to place results. It won't be short code and will probably still take a while as you are running several searches. Add
and![]()
Please Login or Register to view this content.
Turn off any auto calc at the beginning and reactivate at the end of your code. These will speed things up a bit.![]()
Please Login or Register to view this content.
1) do all the work inside an array
2) find the lastrow so you don't accidentally get 1 million rows
(obviously "set rng" like you had - if you stay with using the sheet range)![]()
Please Login or Register to view this content.
Last edited by scottiex; 10-26-2017 at 06:21 PM.
If you want something done right... find a forum and ask an online expert.
Time flies like an arrow. Fruit flies like a banana.
OK, if then Else is new to me, so I'll explore that, but can I put the False at the start at the beginning of my very first macro and then at the end of the very last, or do I need to do it in each macro.
To get it all in an array we have quite a bit more work to do so keeping it simple just put this form
wherever you have this form![]()
Please Login or Register to view this content.
and see if that helps.![]()
Please Login or Register to view this content.
It seems to me that you are doing a lot of recursive stuff just for nothing.
Do all the values you are looking up occur once or multiple times in the searchrange ?
If they occur only once it's better to use something like Application.Match or Find to lookup your value and copy the corresponding value.
If they occur multiple times it's better to lookup the last occurence because it's corresponding value will be the one that finally will be in your destination range.
Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.
This is slow
This is fast, but moves only values not formatting.![]()
Please Login or Register to view this content.
![]()
Please Login or Register to view this content.
_
...How to Cross-post politely...
..Wrap code by selecting the code and clicking the # or read this. Thank you.
Bakerman2,
i attached a workbook that shows what I start with and then what I end up with. The number of items listed under each person in the pivot table may change so an exact position isn't possible.
Given the loops in the code, at the very least it looks as though you should probably be using Exit For after finding the values you are looking for- otherwise you carry on looping through the ranges for nothing. I suspect also that you could simply use GETPIVOTDATA formulas and not loop at all.
Don
Please remember to mark your thread 'Solved' when appropriate.
Looping cells in a range is the absolute slowest way to evaluate contents. Sometimes its the best way regardless, because its easy to implement and isnt prone to issues.![]()
Please Login or Register to view this content.
You could speed things up by using range.find instead, and if need be in a loop range.findnext. Basically find returns a range object, you set that to a variable and then do a range.value = range.value to transfer its value to another cell. Ironically I see now you use this in the next sub.
Depending on your data set you may also benefit from filter instead (either auto or advanced). You can filter on partial strings and then take the results and move them together (assuming you need more than 1 match for each term you seek).
Also, For loops are good for when you need to do something x number of times regardless of the result in each iteration. If you need to loop until something is found or while something is true/false, instead of using exit for to leave the loop, just use a Do while or Do until loop. This will allow the loop to only iterate as many times as required.
Changing references for ranges to be only the required range instead of full columns will help too, especially when using a for loop. In multiple places I see H:H instead of H2:H100 for example. If your looping every cell in H:H thats a million cells to check instead of 100, 1000, etc. Do this 20 times and its 20 millions cells to loop instead of 20,000. Also using constants can speed up code. Set a constant string for your range and then use the constant instead of quoted text strings manually entered multiple times. Use constants for text strings you repeat elsewhere in the code too.
copy/paste is slow and sometimes buggy. Instead do range.value = range.value. You can combine that with copy formatting or just code to format the destination.
Use variables for objects like sheets and workbook instead of repeatedly doing worksheets("")... do a worksheet object and assign it to a variable.
Turning off screenupdating will improve speed and so will turning off automatic calculation until your macros complete.
Overall, my guess is the biggest issue is the combination of whole column references combined with for loops checking each cell in those columns. Fix that and your code should run much faster.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks