VBA For Loop Optimization. Loop slow in excel 2013.

jrwrita

Board Regular
Joined
May 7, 2015
Messages
206
Hi all,

I have a code below that works perfectly fine in excel 2010. I've upgraded to 2013 and now my excel gets the not responding issue, along with the excel not working. I need help optimizing this for use in excel 2013.

Any help would be appreciated.

#Empty is a string btw.


Code:
For thisScen = 1 To UBound(stressScenMapping, 1)


        thisEqShocks = filterIn(eqShocks, 2, stressScenMapping(thisScen, 1), keepcols)


        If thisEqShocks(1, 1) = "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Empty]#Empty[/URL] " Then
            For i = 2 To nRows
                If dataCols(i, 4) <> "Excel" And dataCols(i, 4) <> "OBI" And (dataCols(i, 1) = "value1" Or dataCols(i, 1) = "value2") Then
                    dataWs.Cells(i, stressScenMapping(thisScen, 3)).Value = "No shock found"
                End If
            Next i
        Else                                     'calculate shocks
            Call quicksort(thisEqShocks, 3, 1, UBound(thisEqShocks, 1))
            For i = 2 To nRows
                If dataCols(i, 4) <> "Excel" And dataCols(i, 4) <> "ITS" And (dataCols(i, 1) = "value1" Or dataCols(i, 1) = "value2" Or dataCols(i, 1) = "value3") Then
                    thisCurrRow = findInArrCol(dataCols(i, 3), 3, thisEqShocks)
                    If thisCurrRow = 0 Then      'could not find currency so use generic shock
                        thisCurrRow = findInArrCol("OTHERS", 3, thisEqShocks)
                    End If
                    If thisCurrRow = 0 Then
                        dataWs.Cells(i, stressScenMapping(thisScen, 3)).Value = "No shock found"
                    Else
                        dataWs.Cells(i, stressScenMapping(thisScen, 3)).Value = Replace(dataCols(i, 2), "-", 0) * (thisEqShocks(thisCurrRow, 4) - 1)
                    End If
                End If
            Next i
        End If


    Next thisScen

quicksort function:

Code:
	Sub quicksort(ByRef arr() As Variant, ByVal sortCol As Long, ByVal left As Long, ByVal right As Long)


    If right > left Then
        Dim pivotIndex As Long
        pivotIndex = left + Int((right - left) / 2)


        Dim pivotIndexNew As Long
        pivotIndexNew = partition(arr, sortCol, left, right, pivotIndex)
        Call quicksort(arr, sortCol, left, pivotIndexNew - 1)
        Call quicksort(arr, sortCol, pivotIndexNew + 1, right)
    End If


End Sub
 
try changing the write back of the array to values only:
Code:
.Range(.Cells(1, 1), .Cells(lastrow, lastcol)).value = datawsarray
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I have tried what I suggested in #21 and it doesn't work. the solution is to write the variant array back to the table. Note that to do this you must define the rangfe where you pick up DatawSarray to be coincident with the data in the table
then use this code to write it back:
Code:
Dim myTable As ListObject

Worksheets("database").select
  Set myTable = ActiveSheet.ListObjects("YourtableName")
   myTable.DataBodyRange = DataWSarray
 
Last edited:
Upvote 0
Where the array datawsarray is written back to the worksheet. (From my android phone)
 
Upvote 0
Code:
' now write the entire sheet back again
Worksheets("database").select
  Set myTable = ActiveSheet.ListObjects("tbl_database")
   myTable.DataBodyRange = DataWSarray
.Range(.Cells(1, 1), .Cells(lastRowN, lastColN)) = datawsarray

This is what I currently have, but for some reason no table is being created. Is there anyway to do it without removing the table?
Because i have formulas that reference that table and they are being ruined with the removal of it.
 
Last edited:
Upvote 0
try getting rid of the last line because that is what overwrites the tasble e.g change the above code to:
Code:
' now write the entire sheet back again
Worksheets("database").select
  Set myTable = ActiveSheet.ListObjects("tbl_database")
   myTable.DataBodyRange = DataWSarray
 
Last edited:
Upvote 0
nice it worked, but for some reasons its duplicating the headers.. in row 1 and 2 we get the headers repeated.
 
Upvote 0
I think the reason is you didn't do what I suggested in post#22
For example if the data in your table starts on row 2 then change the line where you pick up the worksheet into the array, the code:


Code:
datawsarray = .Range(.Cells([COLOR=#ff0000]2[/COLOR], 1), .Cells(lastrow, lastcol))

Then you also need to change every line that references the array because element 1 in the array now refers to row 2 in the worksheet


Code:
                        datawsarray(i-1, stressScenMapping(thisScen, 3)).Value = "No shock found"




                        datawsarray(i-1, stressScenMapping(thisScen, 3)).Value = "No shock found"
                    Else
                        datawsarray(i-1, stressScenMapping(thisScen, 3)).Value = Replace(dataCols(i, 2), "-", 0) * (thisEqShocks(thisCurrRow, 4) - 1)

I note that your i index already starts at 2 so this should be fine
Then when you write the array back it won't include the headers
 
Last edited:
Upvote 0
thanks alot, if you have any page I can follow you, or a blog or training series I would be interested. Thanks again.
 
Upvote 0
No I don't have anything like that, but I do help out on here and some other Excel websites. Glad I could assist.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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