Search notes:

Excel: Turn selected region into SQL Insert Statements

The following code, when executed in the immediate window, turns the selected region (i.e. the selection property of the application object. into SQL insert statements:
tableName = "xxx"

for each r in selection.rows                                      : _
    r_= application.transpose(application.transpose(r.value))     : _
    c_ = r_                                                       : _
    i = 1                                                         : _
    for each c in r.value                                         : _
        select case vartype(c      )                              : _
          case vbInteger, vbLong, vbSingle, vbDouble              : _
               c_(i) = c                                          : _
          case vbDate                                             : _
               c_(i) = "date '" & format(c, "yyyy-mm-dd") & "'"   : _
          case vbString                                           : _
               c_(i) = "'" & replace(c, "'", "''") & "'"          : _
          case vbEmpty                                            : _
               c_(i) = "null"                                     : _
          case else                                               : _
               c_(i) = "? " & vartype(c) & " ? "                  : _
        end select                                                : _
        i    = i+1                                                : _
    next c                                                        : _
    debug.print("insert into " & tableName & " values(" &           _
          join(c_, ",")  & ");")                                  : _ 
next r

See also

The VBA function excelRangeToJson which creates a JSON document from a range object.

Index