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(                               _
                             replace(c, "'", "''"),                 _
                                        "&", "' || chr(38) || '")   _
               & "'"                                              : _
          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.
The menu item Get Data from Tabale/Range… of the context menu on a worksheet.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Microsof...', 1758194005, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Microsoft/Office/Excel/Object-Model/Application/selection/to-SQL-insert-statements(66): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78