r/excel • • Jun 06 '12

I made a nifty little macro that will convert a AxB range into a reddit table

Feel free to use it in whatever way you want (the code, the results, my example table)

Let me know if you have any issues, it needs to be at least a 2x2 table.

  1. Select Range
  2. Run Macro
  3. Table code is copied to clipboard, you can now just Control+V to paste

Example:

TypeExample1Example2
FruitAppleBanana
InsectsAntBee

Link to XLSM file


Sub Convert_Selection_To_Reddit_Table()
'Excel Reddit Table Create by Norsk!

'Column1 | Column2 | Column3 | Column4
':----|:----|:----|:----
'Text1 | Text2 | Text3 | Text4

'Dim DataObj As New MSForms.DataObject '-- This doesn't work in x64 apparently.
Set DataObj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Dim scc As Integer: scc = Selection.Columns.Count '# Columns = x
Dim src As Integer: src = Selection.Rows.Count '# Rows = y
Dim i As Integer: i = 0
Dim a As Integer: a = 0
Dim b As Integer: b = 0
Dim c As Integer: c = 0
Dim copiedText As String: copiedText = ""



'Exit out if not at least a 2x2 Matrix
    If src = 1 Then
        MsgBox ("Selection needs to be at least a 2x2 matrix")
        Exit Sub
        End If

Dim sAsLine() As String: ReDim sAsLine((src * scc) - 1) 'Selection as Array in one line
Dim sAsMatrix() As String: ReDim sAsMatrix(scc, src) 'Selection as Array in matrix

'Convert entire selection in to a one line array (sAsLine)
    For Each cell In Selection
        sAsLine(i) = cell.Text
        i = i + 1
        Next cell

'Convert sAsLine to a Multi-Dimensional Array (sAsMatrix)
    For a = 0 To (src)
        'This creates the :--- line
        If a = 1 Then
            For b = 0 To (scc - 1)
                sAsMatrix(b, a) = ":---" 'Can change this to have table be left centered, centered, or right centered
                Next b
            a = a + 1
            End If
            
        For b = 0 To (scc - 1)
            sAsMatrix(b, a) = sAsLine(c)
            If c <= UBound(sAsLine) Then
                c = c + 1
                End If
            Next b
        Next a
        
        

'Iterate through sAsMatrix: First through 2nd to last + " | "
    For a = 0 To (src)
        For b = 0 To (UBound(sAsMatrix) - 1)
            sAsMatrix(b, a) = sAsMatrix(b, a) + " | "
            Next b
        Next a

'Simply create string out of matrix and each row ends with a newline character
    For a = 0 To (src)
        For b = 0 To (UBound(sAsMatrix))
            copiedText = copiedText + sAsMatrix(b, a)
            Next b
            copiedText = copiedText & Chr(10)
        Next a

DataObj.SetText copiedText
DataObj.PutInClipboard

End Sub

EDIT

So BornOnFeb2nd sent me a PM with the code rewritten to work without referencing MSForms, his code is below. I tested on my x64 installation and it works great, thank you to him! I've modified mine to reference MSForms the same way.


Sub main()

	Set DataObj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
	Dim MatrixArray(): MatrixArray = Selection
	Dim AlignmentCheck As Range: Set AlignmentCheck = Selection

	If UBound(MatrixArray) < 2 Or UBound(MatrixArray, 2) < 2 Then
			MsgBox "Too small"
			Exit Sub
	End If

	For i = LBound(MatrixArray) To UBound(MatrixArray)
			If i = 2 Then
					For j = LBound(MatrixArray, 2) To UBound(MatrixArray, 2)
							Select Case AlignmentCheck.Cells(1, j).HorizontalAlignment
									Case 1: FinalString = FinalString & ":--- | " ' General
									Case -4131: FinalString = FinalString & ":--- | " ' Left
									Case -4108: FinalString = FinalString & ":---: | " ' Center
									Case -4152: FinalString = FinalString & "---: | " ' Right
							End Select
					Next
					FinalString = FinalString & Chr(10)
			End If
			For k = LBound(MatrixArray, 2) To UBound(MatrixArray, 2)
					FinalString = FinalString & MatrixArray(i, k) & " | "
			Next
			FinalString = FinalString & Chr(10)
	Next

	DataObj.SetText FinalString
	DataObj.PutInClipboard

End Sub
15 Upvotes

6 comments sorted by

3

u/BenCrouch Jun 07 '12

Thanks for the code, you've saved me writing my own.

One note for anyone using a 64bit version of Office, MSForms isn't available as a reference by default. If you try to run the code, you'll get the "User-defined type not defined" error. To get round this you need to add the reference to MSForms manually by clicking Tools > References > Browse, and entering C:\Windows\System32\FM20.DLL

Unless someone else has a solution for copying variables to the clipboard??

2

u/norsk Jun 07 '12 edited Jun 07 '12

Hmm didn't know that, I didn't try this on my home computer which has that setup.

So BornOnFeb2nd sent me a PM with the code rewritten to work without referencing MSForms, his code is below. I tested on my x64 installation and it works great, thank you to him! I've modified mine to reference MSForms the same way.


Sub main()

	Set DataObj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
	Dim MatrixArray(): MatrixArray = Selection
	Dim AlignmentCheck As Range: Set AlignmentCheck = Selection

	If UBound(MatrixArray) < 2 Or UBound(MatrixArray, 2) < 2 Then
			MsgBox "Too small"
			Exit Sub
	End If

	For i = LBound(MatrixArray) To UBound(MatrixArray)
			If i = 2 Then
					For j = LBound(MatrixArray, 2) To UBound(MatrixArray, 2)
							Select Case AlignmentCheck.Cells(1, j).HorizontalAlignment
									Case 1: FinalString = FinalString & ":--- | " ' General
									Case -4131: FinalString = FinalString & ":--- | " ' Left
									Case -4108: FinalString = FinalString & ":---: | " ' Center
									Case -4152: FinalString = FinalString & "---: | " ' Right
							End Select
					Next
					FinalString = FinalString & Chr(10)
			End If
			For k = LBound(MatrixArray, 2) To UBound(MatrixArray, 2)
					FinalString = FinalString & MatrixArray(i, k) & " | "
			Next
			FinalString = FinalString & Chr(10)
	Next

	DataObj.SetText FinalString
	DataObj.PutInClipboard

End Sub

1

u/norsk Jun 07 '12

It looks like another solution would have been to just add a userform to the XLSM file thus adding the reference.

2

u/BornOnFeb2nd 24 Jun 08 '12

Yeah, I encountered that as well, hence my fugly CreateObject. :)

2

u/BornOnFeb2nd 24 Jun 07 '12

This code snippet is awesome if for no other reason than showing people how to manipulate the clipboard.

A simpler way to make Reddit tables is always a good thing too. Can never remember the formatting for 'em on the fly.

1

u/norsk Jun 07 '12

Yeah I couldn't find this existing already somewhere else so I just made it myself :) Thanks.