Last active
July 31, 2021 01:32
-
-
Save stevesohcot/4fcfaa94df872cd1a3dab76793a9fc8f to your computer and use it in GitHub Desktop.
VBA Loop through Excel rows for Database Import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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