Skip to content
Advertisement

Insert Into Redshift with Excel VBA

I have a requirement to load data from excel to Redshift. I am getting errors because there are numeric columns and the conversion of the values in each cell of a row into an array is then becoming a string. This is fine, except for the numeric columns which need a Null and not ”.

Here is the code.

Join2D is something I had from a previous ask, but in that case everything was a string. Is there a way to modify this to give an output where if the insertValues element is ” it changes it to Null? I tried a for loop with an if statement to try and change any element that is ” to Null and it gave a type mismatch.

I am also open to other ways to get the data to Redshift which don’t require row by row insertion if that is an easier option.

Advertisement

Answer

Consider the industry best practice of SQL parameterization to avoid the messy and potentially dangerous need to concatenate and punctuate data values into SQL statements. And also avoid combining disparate data types with array methods.

Assuming your Excel database API is ADO, consider the Command object for parameterization. This requires you to extend .CreateParameter() calls for all 36 fields (A-AJ columns) and explicitly define data types. Empty cells may translate as NULL entities for insertion.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement