Buffering Delay<\/p><\/div>\n
\nSo let’s learn some faster technique to solve the above task. In the above task performance does not really matters when Array is smaller in size. But when Array size is bigger (like in Thousands..) then you will realize the performance issue.<\/p>\n
\nSo without wasting any more time let’s come to the point. In this article I will give you both the codes to spread the values of an array towards Rows or Columns. At the end of the article you will find a downloadable which will show you the performance difference.<\/p>\n
1. Slower Method – Array values across columns – Using Loop<\/h3>\n
Below is the traditional code to traverse an array value using loop and using this all the values of an array will be spread across columns.
\n <\/p>\n
\n\nSub SpreadUsingLoop()\n'This function will spread the array values using For loop\n \n Dim myArr(10000) As Variant\n Dim TimeDuration As Long\n Dim StartTime As Date\n Dim EndTime As Date\n \n StartTime = Now 'Start time is captured here\n \n'fill values in this array\n For i = 0 To UBound(myArr) - 1\n myArr(i) = \"This is my data \" & i\n Next\n'myArr has 5000 values in it.\n'to spread this 5000 values, here we will use loop\n Rows(6).Clear\n \n For i = 0 To UBound(myArr) - 1\n Cells(6, i + 1).Value = myArr(i)\n Next\n EndTime = Now ' End time is captured\n'Calculate the total time duration in seconds\n TimeDuration = DateDiff(\"s\", CDate(StartTime), CDate(EndTime))\n'Show the time taken in Message Box\n MsgBox TimeDuration & \" Second(s)\"\nEnd Sub\n\n<\/code><\/pre>\n2. Faster Method – Array values across columns – Using Array to Range<\/h3>\n
Below is the BEST <\/strong>way to spread an array values across columns.<\/p>\n <\/p>\n
\nSub SpreadUsingArrayToRange()\n \n'This function will spread the array values using For Array to Range\n \n Dim myArr(10000) As Variant\n Dim TimeDuration As Long\n Dim StartTime As Date\n Dim EndTime As Date\n \n StartTime = Now 'Start time is captured here\n \n'fill values in this array\n For i = 0 To UBound(myArr) - 1\n myArr(i) = \"This is my data \" & i\n Next\n'myArr has 5000 values in it.\n'to spread this 10000 values, here we will use array to range method\n Rows(11).Clear\n Range(Cells(11, 1), Cells(11, UBound(myArr))).Value = myArr\n EndTime = Now ' End time is captured\n'Calculate the total time duration in Seconds\n TimeDuration = DateDiff(\"s\", CDate(StartTime), CDate(EndTime))\n'Show the time taken in Message Box\n MsgBox TimeDuration & \" Second(s)\"\n \nEnd Sub\n<\/code><\/pre>\n <\/p>\n
How is the second method better? <\/h3>\n i) Simpler in coding – Single line code <\/h3>\n
In the second method you need not to write a looping code of 3 lines with a traversing variable “i”. Thus it makes your coding easy, short and understandable.<\/p>\n
ii) Faster – Better Performance <\/h3>\n
This is the Major benefit of the second method. This method is way faster than the First one. For 5000 Records you can see the difference by running both the codes. My observation is that – First code always takes 2 to 3 seconds for 5000 records while the second method completes in ZERO seconds.[\/et_pb_text][et_pb_cta title=”FREE Download – View Performance of Spread Arrays across columns” button_url=”\/excel\/wp-content\/downloads\/Array_Traverse_-_Better_performance.xlsm” button_text=”Download Now” _builder_version=”4.17.6″ _module_preset=”a50a16dd-d05f-4ea2-acab-1468d2e4010e” hover_enabled=”0″ global_colors_info=”{}” sticky_enabled=”0″]To see the performance and play around with the code, you can download the below excel workbook with both the codes with a User Interface button.<\/p>\n
VBA Animation to see – Array Traverse Performance<\/p><\/div>[\/et_pb_cta][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"All my avid readers from Computer Programming backgrounds must be thinking is it a big deal? Like other programming languages use loop to traverse all the values of an array and spread them across rows or column. Right? If you are thinking so… you are absolutely right. You can do it using the loop. […]<\/p>\n","protected":false},"author":45,"featured_media":242477,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"All my avid readers from Computer Programming backgrounds must be thinking is it a big deal? Like other programming languages use loop<\/a><\/strong><\/em> to traverse all the values of an array and spread them across rows or column. Right?\r\n\u00a0\r\nIf you are thinking so... you are absolutely right. You can do it using the loop.\r\n\u00a0\r\nBut what if I tell you that there is a very easy and faster way to do so? Excited??\r\n\u00a0\r\nGenerally, as a developer, we tend to forget the performance aspect while coding... be it a code for a small task or a big software. Performance matters. None of us like the below screen while watching an exciting Cricket Match on Internet or a Movie etc. Then why ignore the performance aspect??\r\n\u00a0\r\n\r\n[caption id=\"attachment_242472\" align=\"aligncenter\" width=\"570\"] Buffering Delay[\/caption]\r\n\r\n\u00a0\r\nSo let's learn some faster technique to solve the above task. In the above task performance does not really matters when Array is smaller in size. But when Array size is bigger (like in Thousands..) then you will realize the performance issue.\r\n\r\n\u00a0\r\nSo without wasting any more time let's come to the point. In this article I will give you both the codes to spread the values of an array towards Rows or Columns. At the end of the article you will find a downloadable which will show you the performance difference.\r\n\r\n1. Slower Method - Array values across columns - Using Loop<\/h3>\r\nBelow is the traditional code to traverse an array value using loop and using this all the values of an array will be spread across columns.\r\n\u00a0\r\n\r\n\r\nSub SpreadUsingLoop()\r\n'This function will spread the array values using For loop\r\n \r\n Dim myArr(10000) As Variant\r\n Dim TimeDuration As Long\r\n Dim StartTime As Date\r\n Dim EndTime As Date\r\n \r\n StartTime = Now 'Start time is captured here\r\n \r\n'fill values in this array\r\n For i = 0 To UBound(myArr) - 1\r\n myArr(i) = \"This is my data \" & i\r\n Next\r\n'myArr has 5000 values in it.\r\n'to spread this 5000 values, here we will use loop\r\n Rows(6).Clear\r\n \r\n For i = 0 To UBound(myArr) - 1\r\n Cells(6, i + 1).Value = myArr(i)\r\n Next\r\n EndTime = Now ' End time is captured\r\n'Calculate the total time duration in seconds\r\n TimeDuration = DateDiff(\"s\", CDate(StartTime), CDate(EndTime))\r\n'Show the time taken in Message Box\r\n MsgBox TimeDuration & \" Second(s)\"\r\nEnd Sub\r\n\r\n<\/code><\/pre>\r\n\r\n\r\n2. Faster Method - Array values across columns - Using Array to Range<\/h3>\r\nBelow is the BEST <\/strong>way to spread an array values across columns.\r\n\r\n\u00a0\r\n\r\n\r\nSub SpreadUsingArrayToRange()\r\n \r\n'This function will spread the array values using For Array to Range\r\n \r\n Dim myArr(10000) As Variant\r\n Dim TimeDuration As Long\r\n Dim StartTime As Date\r\n Dim EndTime As Date\r\n \r\n StartTime = Now 'Start time is captured here\r\n \r\n'fill values in this array\r\n For i = 0 To UBound(myArr) - 1\r\n myArr(i) = \"This is my data \" & i\r\n Next\r\n'myArr has 5000 values in it.\r\n'to spread this 10000 values, here we will use array to range method\r\n Rows(11).Clear\r\n Range(Cells(11, 1), Cells(11, UBound(myArr))).Value = myArr\r\n EndTime = Now ' End time is captured\r\n'Calculate the total time duration in Seconds\r\n TimeDuration = DateDiff(\"s\", CDate(StartTime), CDate(EndTime))\r\n'Show the time taken in Message Box\r\n MsgBox TimeDuration & \" Second(s)\"\r\n \r\nEnd Sub\r\n<\/code><\/pre>\r\n\r\n\u00a0\r\n