By using vba.booksticle.com you agree to our cookie policy, We and our partners operate globally and use cookies, for multiple purposes

948 Steps to 6 Figures by Learning Excel-VBA and Other Skills



Step 449 - SQL insert rows from data


The following code snippet is useful to insert data in a sheet to a table in a database

Requires the RunQuery and FindLastRow UDF. Also the "Set Catalog" was for a specific database. In SQL Server the "Use db" would be the statement to use.

r = findlastrow(1)
For i = 2 to r
    sSQL = "set catalog preprod_db; " ' use statement for a different db ??
    sSQL = sSQL & "Insert into main_db..tbl_schedule values "
    sSQL = sSQL & "(" & cells(i, 1) & ",'" & cells(i,2) & "','" & cells(i,3) & "','','" & Cells(i, 5) & "')"
    x = RunQuery(sSQL)
    if Instr(Lcase(x),"error") > 0 then
       stop_it
    end if

next



   Table of Contents | Send us your feedback | © 2025 All Rights Reserved | Edit