Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a refactored <code>Sub</code> </p> <p>Points to note:</p> <ul> <li><p>use variant arrays for the looping/data processing because looping through cells is slow</p></li> <li><p>you can change the value of <code>srcBlocks</code> to control how many blocks to process, or extract it from your source data</p></li> <li><p>refering to the destination sheet by index can be problematic if it is moved. Safer to refer to it by name <code>Worksheets("SheetName")</code></p></li> </ul> <p>.</p> <pre><code>Sub FormatData() Dim rw2 As Integer, rwA As Integer, colA As Integer Dim vDst() As Variant, vSrc As Variant Dim srcBlocks As Integer srcBlocks = 8 ' process 8 blocks of 12 rows vSrc = ActiveSheet.Range("A1:D" &amp; srcBlocks * 12) ReDim vDst(1 To srcBlocks * 4 + 1, 1 To 5) For rwA = 0 To srcBlocks * 12 - 1 Step 12 ' = 0, 12, 24, ... For colA = 1 To 4 ' 4 columns in Src rw2 = (rwA \ 12) * 4 + colA + 1 ' 4 Dst rws per block, = 2..5, 6..9, ... vDst(rw2, 2) = vSrc(rwA + 1, colA) vDst(rw2, 3) = vSrc(rwA + 2, colA) &amp; ". " &amp; _ vSrc(rwA + 3, colA) &amp; ". " &amp; _ vSrc(rwA + 4, colA) &amp; ". " &amp; _ vSrc(rwA + 5, colA) &amp; "." vDst(rw2, 4) = vSrc(rwA + 7, colA) vDst(rw2, 5) = vSrc(rwA + 10, colA) Next colA Next rwA Worksheets(2).Range("A1:E" &amp; CStr(srcBlocks * 4 + 1)) = vDst End Sub </code></pre> <p>To add flexability, for variable number of columns in Source data</p> <pre><code>Sub FormatData() Dim rw2 As Integer, rwA As Integer, colA As Integer Dim vDst() As Variant, vSrc As Variant Dim srcBlocks As Integer, srcColumns As Integer srcBlocks = 8 ' process 8 blocks of 12 rows ' srcColumns = 5 ' Columns in source data ' vSrc = ActiveSheet.Range( _ ActiveSheet.Cells(1, 1), _ ActiveSheet.Cells(srcBlocks * 12, srcColumns)) ReDim vDst(1 To srcBlocks * srcColumns + 1, 1 To 5) For rwA = 0 To srcBlocks * 12 - 1 Step 12 ' = 0, 12, 24, ... ' For colA = 1 To srcColumns ' srcColumns columns in Source ' rw2 = (rwA \ 12) * srcColumns + colA + 1 ' srcColumns rows in Destination per Source block ' vDst(rw2, 2) = vSrc(rwA + 1, colA) vDst(rw2, 3) = vSrc(rwA + 2, colA) &amp; ". " &amp; _ vSrc(rwA + 3, colA) &amp; ". " &amp; _ vSrc(rwA + 4, colA) &amp; ". " &amp; _ vSrc(rwA + 5, colA) &amp; "." vDst(rw2, 4) = vSrc(rwA + 7, colA) vDst(rw2, 5) = vSrc(rwA + 10, colA) Next colA Next rwA Worksheets(2).Range("A1:E" &amp; CStr(srcBlocks * 4 + 1)) = vDst End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload