VBA Running Slow

JeffK627

Active Member
Joined
Jun 22, 2005
Messages
313
I have a VBA Sub that copies data from one worksheet to another, transposing some of the columns (which have headers indicated the Quarter and number of units sold per product for that quarter) so that on the new sheet there's a column called "Quarter" and one called "Units". The problem is, it's running quite slowly - there are 25,000 rows in the source worksheet and after 24 hours it had only processed 16,000 rows. Since it will have to process all 25,000 rows 12 times to get all the quarters, it obviously needs to run faster. This is in Excel 2007. Here's the code, any feedback is appreciated:

Code:
Sub transposeData3()

'variables to locate rows and columns
Dim LastRow As Double, LastCol As Double, i As Double, q As Double, z As Double
'variables to refer to worksheets
Dim SourceSheet As Worksheet, ResultSheet As Worksheet

Set SourceSheet = ActiveWorkbook.Worksheets("iMANY FFS (per state per ndc11)")
Set ResultSheet = ActiveWorkbook.Worksheets("Sheet3")

LastRow = SourceSheet.Range("A65536").End(xlUp).Row
LastCol = SourceSheet.Range("Z1").End(xlToLeft).Column

z = 2

For q = 6 To LastCol
    For i = 2 To LastRow - 1
        ResultSheet.Range("A" & z).Value = SourceSheet.Range("A" & i).Value
        ResultSheet.Range("B" & z).Value = SourceSheet.Range("B" & i).Value
        ResultSheet.Range("C" & z).Value = SourceSheet.Range("D" & i).Value
        ResultSheet.Range("D" & z).Value = SourceSheet.Cells(1, q).Value
        ResultSheet.Range("E" & z).Value = SourceSheet.Cells(i, q).Value
        z = z + 1
    Next
Next

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
try this, works for me :)

<TABLE class=MsoNormalTable style="WIDTH: 100%; mso-cellspacing: 1.5pt" cellPadding=0 width="100%" border=0><TBODY><TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" colSpan=2><CODE>1</CODE><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

</TD><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>' Disable automatic calculation </CODE><o:p></o:p>

</TD></TR><TR style="mso-yfti-irow: 1; mso-yfti-lastrow: yes"><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>2</CODE><o:p></o:p>

</TD><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" colSpan=2><CODE>Application.Calculation = xlCalculationManual </CODE><o:p></o:p>

</TD></TR><TR height=0><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=29></TD><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=9></TD><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=710></TD></TR></TBODY></TABLE>
<o:p></o:p>
<TABLE class=MsoNormalTable style="WIDTH: 100%; mso-cellspacing: 1.5pt" cellPadding=0 width="100%" border=0><TBODY><TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>3</CODE><o:p></o:p>

</TD><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" colSpan=2><CODE>' do regular operation here </CODE><o:p></o:p>

</TD></TR><TR style="mso-yfti-irow: 1; mso-yfti-lastrow: yes"><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" colSpan=2><CODE>4</CODE><o:p></o:p>

</TD><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>' Force a calculation </CODE><o:p></o:p>

</TD></TR><TR height=0><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=45></TD><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=9></TD><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=693></TD></TR></TBODY></TABLE>
<o:p></o:p>
<TABLE class=MsoNormalTable style="WIDTH: 100%; mso-cellspacing: 1.5pt" cellPadding=0 width="100%" border=0><TBODY><TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" colSpan=2><CODE>5</CODE><o:p></o:p>

</TD><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>Application.Calculate </CODE><o:p></o:p>

</TD></TR><TR style="mso-yfti-irow: 1; mso-yfti-lastrow: yes"><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>6</CODE><o:p></o:p>

</TD><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" colSpan=2><CODE>' Then remember to run automatic calculations back on </CODE><o:p></o:p>

</TD></TR><TR height=0><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=25></TD><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=10></TD><TD style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent" width=713></TD></TR></TBODY></TABLE>
<o:p></o:p>
<TABLE class=MsoNormalTable style="WIDTH: 100%; mso-cellspacing: 1.5pt" cellPadding=0 width="100%" border=0><TBODY><TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes"><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>7</CODE><o:p></o:p>

</TD><TD style="BORDER-RIGHT: #ece9d8; PADDING-RIGHT: 2.25pt; BORDER-TOP: #ece9d8; PADDING-LEFT: 2.25pt; PADDING-BOTTOM: 2.25pt; BORDER-LEFT: #ece9d8; PADDING-TOP: 2.25pt; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"><CODE>Application.Calculation = xlCalculationAutomatic</CODE><o:p></o:p>

</TD></TR></TBODY></TABLE>
 
Upvote 0
Try

Code:
Sub transposeData3()

'variables to locate rows and columns
Dim LastRow As Double, LastCol As Double, i As Double, q As Double, z As Double
'variables to refer to worksheets
Dim SourceSheet As Worksheet, ResultSheet As Worksheet

With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With

Set SourceSheet = ActiveWorkbook.Worksheets("iMANY FFS (per state per ndc11)")
Set ResultSheet = ActiveWorkbook.Worksheets("Sheet3")

LastRow = SourceSheet.Range("A65536").End(xlUp).Row
LastCol = SourceSheet.Range("Z1").End(xlToLeft).Column

z = 2

For q = 6 To LastCol
    For i = 2 To LastRow - 1
        ResultSheet.Range("A" & z).Value = SourceSheet.Range("A" & i).Value
        ResultSheet.Range("B" & z).Value = SourceSheet.Range("B" & i).Value
        ResultSheet.Range("C" & z).Value = SourceSheet.Range("D" & i).Value
        ResultSheet.Range("D" & z).Value = SourceSheet.Cells(1, q).Value
        ResultSheet.Range("E" & z).Value = SourceSheet.Cells(i, q).Value
        z = z + 1
    Next
Next

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

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