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:

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

James_B

Active Member
Joined
Jun 11, 2009
Messages
447
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.
 

James_B

Active Member
Joined
Jun 11, 2009
Messages
447
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,008
Messages
5,599,324
Members
414,304
Latest member
sarao18592

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
Top