Single Column --> 2d Grid

James_B

Active Member
Joined
Jun 11, 2009
Messages
447
Since it seems to come up every now and then, here's a quick generic routine/idea for reshaping single columns of data:

Code:
Option Explicit
' --------------------------------------------------------
'  DECLARE APIs
' --------------------------------------------------------
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
   nDestPtr As Any, _
   nSrcPtr As Any, _
   ByVal nLenB As Long _
)
Private Sub Example()
   Dim Rng As Range
   Set Rng = ActiveSheet.Cells(1).Resize(1000)
 
   Rng.Formula = "=1 + MOD(ROW(A1) - 1, 10)"
   Rng(, 3).Resize(10, 100).Value = Reshape(Rng, nColLen:=10)
End Sub
Private Function Reshape(rSrc As Range, nColLen As Long) As Variant
   ' -----------------------------------------------------------------
   '  RETURNS:    An array holding the contents of rSrc reshaped
   '              as an (nColLen x AppropWidth) array
   ' -----------------------------------------------------------------
   ' Validate arguments
   If (rSrc.Cells.Count / nColLen) <> (rSrc.Cells.Count \ nColLen) Then Exit Function
 
   ' Read input
   Dim vSrc As Variant
   vSrc = rSrc.Value
   ' Get location of vSrc's SafeArray structure
   Dim nPtrToSA As Long
   CopyMemory nPtrToSA, ByVal VarPtr(vSrc) + 8, 4
 
   ' Alter number of rows
   CopyMemory ByVal nPtrToSA + 16, rSrc.Cells.Count \ nColLen, 4
 
   ' Alter number of cols
   CopyMemory ByVal nPtrToSA + 24, nColLen, 4
 
   ' Return altered array
   Reshape = vSrc
End Function
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Not that I know of, no. Though you have to be careful when tinkering with a SafeArray struct like this. There are only a limited number of moves you can make. In this case, since I'm starting with an array read from a range, there are a lot more knowns.
 
Upvote 0
For safety's sake, by the way, I'd recommend the following (I've put a guard around the CopyMemory functions in case a null pointer is in play)

Code:
Option Explicit
' --------------------------------------------------------
'  DECLARE APIs
' --------------------------------------------------------
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
   nDestPtr As Any, _
   nSrcPtr As Any, _
   ByVal nLenB As Long _
)
Private Sub Example()
   Dim Rng As Range
   Set Rng = ActiveSheet.Cells(1).Resize(1000)
 
   Rng.Formula = "=1 + MOD(ROW(A1) - 1, 10)"
   Rng(, 3).Resize(10, 100).Value = Reshape(Rng, nColLen:=10)
End Sub
Private Function Reshape(rSrc As Range, nColLen As Long) As Variant
   ' -----------------------------------------------------------------
   '  RETURNS:    An array holding the contents of rSrc reshaped
   '              as an (nColLen x AppropWidth) array
   ' -----------------------------------------------------------------
   ' Validate arguments
   If (rSrc.Cells.Count / nColLen) <> (rSrc.Cells.Count \ nColLen) Then Exit Function
 
   ' Read input
   Dim vSrc As Variant
   vSrc = rSrc.Value
   ' Get location of vSrc's SafeArray structure
   Dim nPtrToSA As Long
   CopyMemory nPtrToSA, ByVal VarPtr(vSrc) + 8, 4
 
   if nPtrToSA then
      ' Alter number of rows
      CopyMemory ByVal nPtrToSA + 16, rSrc.Cells.Count \ nColLen, 4
 
      ' Alter number of cols
      CopyMemory ByVal nPtrToSA + 24, nColLen, 4
 
      ' Return altered array
      Reshape = vSrc
   end if
End Function
 
Upvote 0

Forum statistics

Threads
1,214,837
Messages
6,121,883
Members
449,057
Latest member
Moo4247

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