Excel VBA to generate markdown table


Option Explicit

Sub sbMakeMarkdown()
 ' https://daringfireball.net/projects/markdown/syntax#html
 ' https://stackoverflow.com/questions/72806674/how-copy-text-from-vba-to-windows-clipbaord/72857101#comment134719963_72857101
 ' https://stackoverflow.com/questions/76528307/how-to-transfer-data-from-table-to-another-in-2-different-worksheets
 ' https://learn.microsoft.com/en-us/office/vba/api/excel.range.text
 Dim iRows, iColumns, iRow, iColumn, iHeader As Long
 Dim sValue, sMarkdown, sText As String
 Dim rSelectedRange As Range
 Set rSelectedRange = Application.Selection
 iColumns = rSelectedRange.Columns.Count
 iRows = rSelectedRange.Rows.Count
 sMarkdown = ""
 For iRow = 1 To iRows
  If iRow = 2 Then
   For iHeader = 1 To iColumns
    sMarkdown = sMarkdown & " | ---"
   Next iHeader
   sMarkdown = sMarkdown & " | " & vbCrLf
  End If
  For iColumn = 1 To iColumns
   sText = Cells(iRow, iColumn).Text
   sMarkdown = sMarkdown & " | " & sText
  Next iColumn
  sMarkdown = sMarkdown & " | " & vbCrLf
 Next iRow
 CreateObject("htmlfile").ParentWindow.ClipboardData.SetData "text", sMarkdown
End Sub