Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
stevesohcot / import-data.vba
Created July 31, 2021 01:38
VBA Import data from Excel to Database
Public Function ImportData(sheet As String)
Dim i As Long
Dim strSQL As String
Dim totalRows As Long
totalRows = HowManyRows(sheet)
strInsertHeader = GetInsertHeader()
@stevesohcot
stevesohcot / vba-excel-get-row-count.vba
Created July 31, 2021 01:16
VBA Excel - Get Row Count
Public Function HowManyRows(sheetName As String) As Long
'Find out how many row there are
' Assumes the first column does NOT have a blank in it
Sheets(sheetName).Select
Range("A1").Select
Selection.End(xlDown).Select
@stevesohcot
stevesohcot / import-vba-main.vba
Created July 30, 2021 01:16
VBA Import Data from Excel - Main sub
Public Sub autoImport()
' First delete existing data
Dim strSQL As String
strSQL = "DELETE FROM myTableName" ' Could potentially TRUNCATE instead
Call RunSQL(strDbConn, strSQL)
' Import the new data
Call ImportData("tabWithData") ' Be sure to pass in the name of your worksheet, or get it dynamically
@stevesohcot
stevesohcot / vba-loop-through-excel-rows-for-database-import.vba
Last active July 31, 2021 01:32
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()
@stevesohcot
stevesohcot / vba-nz.vba
Created July 27, 2021 14:48
VBA Null Zero NZ()
Function Nz(value As String) As Double
If IsNull(value) Or (value = "") Then
Nz = 0
Else
Nz = value
End If
End Function
@stevesohcot
stevesohcot / vba-replace-single-quote.vba
Created July 27, 2021 14:46
VBA Replace Single Quote
Public Function ReplaceSingleQuote(str As String) As String
If Len(Trim(str)) > 0 Then
ReplaceSingleQuote = Replace(str, "'", "''")
Else
ReplaceSingleQuote = str
End If
End Function
@stevesohcot
stevesohcot / vba-remove-last-character-if-comma.vba
Created July 27, 2021 14:34
VBA Remove Last Character if Comma
Public Function RemoveLastCharacterIfComma(str As String) As String
str = Trim(str)
If Right(str, 1) = "," Then
RemoveLastCharacterIfComma = Left(str, Len(str) - 1)
Else
RemoveLastCharacterIfComma = str
End If
Public Function getSQLForSingleRow(sheet As String, rowNumber As Long) As String
' Be sure to update any formatting; ex date or number
fieldWithText = ReplaceSingleQuote(Worksheets(sheet).Range("A" & rowNumber).FormulaR1C1)
fieldWithDate = Format(Worksheets(sheet).Range("B" & rowNumber).FormulaR1C1, "mm/dd/yyyy")
fieldWithNumbers = CDbl(Nz(Worksheets(sheet).Range("C" & rowNumber).FormulaR1C1))
Dim strSQL As String
@stevesohcot
stevesohcot / excel-row-count-vba.vba
Created July 27, 2021 13:32
VBA how many rows in Excel
Public Function HowManyRows(sheetName As String) As Integer
'Find out how many row there are
Dim i As Integer
Sheets(sheetName).Select
Range("A1").Select
Do
ActiveCell.Offset(1, 0).Select
@stevesohcot
stevesohcot / run-sql-in-vba-header.vba
Last active July 31, 2021 01:31
Run SQL in VBA - Header
Public Function GetInsertHeader() As String
Dim strHeader As String
strHeader = ""
strHeader = strHeader & " INSERT INTO myTableName ("
strHeader = strHeader & " [field_one]"
strHeader = strHeader & " ,[field_two]"
strHeader = strHeader & " ,[field_three]"
strHeader = strHeader & " )"