Follow this structured strategy to create a VBA library for handling data from multiple sources like Excel, SQL Server, and Tableau.
Organize your .bas
files into logical modules for maintainability and reusability:
DataConnection.bas
: Handles connections to data sources.DataRetrieval.bas
: Functions for fetching data from SQL Server and reading data from spreadsheets.DataTransformation.bas
: Utilities for data cleaning and transformation.Utilities.bas
: General-purpose utilities like logging and error handling.DataConnection.bas
ModuleOption Explicit
' Connection strings
Public Const SQL_CONNECTION_STRING As String = "Your SQL Server connection string"
' Function to connect to SQL Server
Public Function GetSQLConnection() As Object
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = SQL_CONNECTION_STRING
conn.Open
Set GetSQLConnection = conn
End Function
DataRetrieval.bas
ModuleThis module handles pulling data from SQL Server tables and reading data from existing spreadsheets.
Option Explicit
' Function to retrieve data from SQL Server and write it to a table in the current workbook
Public Sub FetchDataToTable(sqlQuery As String, targetSheetName As String)
Dim conn As Object, rs As Object, targetSheet As Worksheet, rowNum As Long, colNum As Long
Set conn = GetSQLConnection()
Set rs = CreateObject("ADODB.Recordset")
rs.Open sqlQuery, conn
Set targetSheet = ThisWorkbook.Sheets(targetSheetName)
targetSheet.Cells.Clear
' Write headers
For colNum = 1 To rs.Fields.Count
targetSheet.Cells(1, colNum).Value = rs.Fields(colNum - 1).Name
Next colNum
' Write data
rowNum = 2
Do Until rs.EOF
For colNum = 1 To rs.Fields.Count
targetSheet.Cells(rowNum, colNum).Value = rs.Fields(colNum - 1).Value
Next colNum
rowNum = rowNum + 1
rs.MoveNext
Loop
rs.Close
conn.Close
End Sub
' Function to read data from a specific tab in an Excel workbook on the network and write it to a tab in the current workbook
Public Sub ReadNetworkTabToSheet(filePath As String, sourceSheetName As String, targetSheetName As String)
Dim xlApp As Object, xlBook As Object, xlSheet As Object, targetSheet As Worksheet, data As Variant
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(filePath)
Set xlSheet = xlBook.Sheets(sourceSheetName)
data = xlSheet.UsedRange.Value
Set targetSheet = ThisWorkbook.Sheets(targetSheetName)
targetSheet.Cells.Clear
targetSheet.Cells(1, 1).Resize(UBound(data, 1), UBound(data, 2)).Value = data
xlBook.Close False
xlApp.Quit
End Sub
' Function to create INSERT statements from a table in a sheet and write them to a specified sheet
Public Sub CreateInsertStatements(tableName As String, sourceSheetName As String, targetSheetName As String)
Dim sourceSheet As Worksheet, targetSheet As Worksheet, rowNum As Long, colNum As Long, lastRow As Long, lastCol As Long
Dim header As Variant, dataRow As Variant, insertStatement As String
' Set source and target sheets
Set sourceSheet = ThisWorkbook.Sheets(sourceSheetName)
On Error Resume Next
Set targetSheet = ThisWorkbook.Sheets(targetSheetName)
If targetSheet Is Nothing Then
Set targetSheet = ThisWorkbook.Sheets.Add
targetSheet.Name = targetSheetName
End If
On Error GoTo 0
targetSheet.Cells.Clear
' Get headers and data
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, 1).End(xlUp).Row
lastCol = sourceSheet.Cells(1, sourceSheet.Columns.Count).End(xlToLeft).Column
header = sourceSheet.Cells(1, 1).Resize(1, lastCol).Value
' Write INSERT statements
rowNum = 1
For rowNum = 2 To lastRow
dataRow = sourceSheet.Cells(rowNum, 1).Resize(1, lastCol).Value
insertStatement = "INSERT INTO " & tableName & " ("
For colNum = 1 To lastCol
insertStatement = insertStatement & header(1, colNum) & ", "
Next colNum
insertStatement = Left(insertStatement, Len(insertStatement) - 2) & ") VALUES ("
For colNum = 1 To lastCol
insertStatement = insertStatement & "'" & Replace(dataRow(1, colNum), "'", "''") & "', "
Next colNum
insertStatement = Left(insertStatement, Len(insertStatement) - 2) & ");"
targetSheet.Cells(rowNum - 1, 1).Value = insertStatement
Next rowNum
End Sub
DataTransformation.bas
ModuleOption Explicit
' Function to clean data
Public Function CleanData(data As Variant) As Variant
Dim i As Long
For i = LBound(data) To UBound(data)
data(i) = UCase(Trim(data(i)))
Next i
CleanData = data
End Function
' Function to format billing data
Public Function FormatBillingData(data As Object) As Object
' Apply transformations to the recordset
Set FormatBillingData = data
End Function
Utilities.bas
Option Explicit
' Logging function
Public Sub LogMessage(message As String)
Dim logFile As String
logFile = ThisWorkbook.Path & "\Log.txt"
Open logFile For Append As #1
Print #1, Now & ": " & message
Close #1
End Sub
' Error handling utility
Public Function HandleError(err As Object) As String
LogMessage "Error " & err.Number & ": " & err.Description
HandleError = "Error occurred: " & err.Description
End Function
.bas
files.' Fetch data from SQL Server table to a sheet
Call FetchDataToTable("SELECT * FROM BillingData", "TargetSheet")
' Read data from an Excel sheet on the network to a sheet
Call ReadNetworkTabToSheet("\\network_path\file.xlsx", "SourceSheet", "TargetSheet")
' Create INSERT statements from a table and write them to a sheet
Call CreateInsertStatements("TargetTable", "SourceSheet", "InsertStatementsSheet")
' Log activities
LogMessage "Data successfully retrieved and processed."
This strategy ensures your VBA library is modular, maintainable, and scalable for integrating data from various sources into your billing processes.