add functions to turn column number into letter, range to transposed string array, and build string array into sql values

This commit is contained in:
Paul Trowbridge 2017-09-29 11:40:14 -04:00
parent d886dd0f0c
commit 1b2cdf854b
2 changed files with 119 additions and 0 deletions

12
FL.bas
View File

@ -613,3 +613,15 @@ Sub markdown_whole_sheet()
End Sub End Sub
Sub sql_from_range()
Dim x As New TheBigOne
Dim wapi As New Windows_API
Dim r() As String
Call wapi.ClipBoard_SetData(x.SQLp_build_sql_values(x.ARRAYp_get_range_string(Selection), False))
End Sub

View File

@ -2160,4 +2160,111 @@ Function markdown_whole_sheet(ByRef sh As Worksheet) As String
markdown_whole_sheet = Me.markdown_from_table(tbl) markdown_whole_sheet = Me.markdown_from_table(tbl)
End Function
Function MISCe_colnum_to_letter(ByRef x As Long) As String
If x > 26 Then
MISCe_colnum_to_letter = Chr(x \ 26 + 64) & Chr((x / 26 - x \ 26) * 26 + 64)
Else
MISCe_colnum_to_letter = Chr(x + 64)
End If
End Function
Public Function SQLp_build_sql_values(ByRef tbl() As String, trim As Boolean) As String
Dim i As Long
Dim j As Long
Dim sql As String
Dim rec As String
Dim type_flag() As String
ReDim type_flag(UBound(tbl, 1))
For j = 0 To UBound(tbl, 1)
If IsNumeric(tbl(j, 1)) Then
If InStr(1, tbl(j, 1), ".") > 0 Then
type_flag(j) = "N"
Else
type_flag(j) = "S"
End If
Else
If IsDate(tbl(j, 1)) Then
type_flag(j) = "D"
Else
type_flag(j) = "S"
End If
End If
Next j
For i = 1 To UBound(tbl, 2)
rec = ""
If i <> 1 Then sql = sql & "," & vbCrLf
rec = rec & "("
For j = 0 To UBound(tbl, 1)
If j <> 0 Then rec = rec & ","
Select Case type_flag(j)
Case "N" '-------N = numeric but should probably be N for numeric----
If tbl(j, i) = "" Then
rec = rec & "NULL"
Else
rec = rec & Replace(tbl(j, i), "'", "''")
End If
Case "S" '-------S = string------------------------------------------
If trim Then
rec = rec & "'" & LTrim(RTrim(Replace(tbl(j, i), "'", "''"))) & "'"
Else
rec = rec & "'" & Replace(tbl(j, i), "'", "''") & "'"
End If
Case "D" '-------D = date---------------------------------------------
If LTrim(RTrim(tbl(j, i))) = "" Then
rec = rec & "CAST(NULL AS DATE)"
Else
rec = rec & "'" & tbl(j, i) & "'"
End If
Case Else '-------Assume text------------------------------------------
If trim Then
rec = rec & "'" & LTrim(RTrim(Replace(tbl(j, i), "'", "''"))) & "'"
Else
rec = rec & "'" & Replace(tbl(j, i), "'", "''") & "'"
End If
End Select
Next j
rec = rec & ")"
sql = sql & rec
Next i
SQLp_build_sql_values = sql
End Function
Public Function ARRAYp_get_range_string(ByRef r As range) As String()
Dim i As Long
Dim j As Long
Dim t1() As Variant
Dim t2() As String
t1 = r
'---convert to 0 lower bound array----
ReDim t2(UBound(t1, 1) - 1, UBound(t1, 2) - 1)
For i = 1 To UBound(t1, 1)
For j = 1 To UBound(t1, 2)
t2(i - 1, j - 1) = CStr(t1(i, j))
Next j
Next i
Call Me.ARRAYp_Transpose(t2)
ARRAYp_get_range_string = t2
End Function End Function