Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save stevesohcot/4fcfaa94df872cd1a3dab76793a9fc8f to your computer and use it in GitHub Desktop.

Select an option

Save stevesohcot/4fcfaa94df872cd1a3dab76793a9fc8f to your computer and use it in GitHub Desktop.
VBA Loop through Excel rows for Database Import
Public Function ImportData(sheet As String)
Dim i As Integer
Dim strSQL As String
Dim totalRows As Long
totalRows = HowManyRows(sheet)
strInsertHeader = GetInsertHeader()
Dim strIndividualRows As String
strIndividualRows = ""
For i = 2 To totalRows
strIndividualRows = strIndividualRows & getSQLForSingleRow(sheet, i)
' add comma at the end no matter what
' will remove after if needed
strIndividualRows = strIndividualRows & ","
Next
strSQL = strInsertHeader & " VALUES " & strIndividualRows
strSQL = RemoveLastCharacterIfComma(strSQL)
Call RunSQL(strDbConn, strSQL)
Range("A1").Select
MsgBox "Complete"
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment