Excel基于列

时间:2018-02-16 16:07:11

标签: sql excel-2016

我知道这可能不是最佳做法,但我的公司用户有时会向我发送带有值的电子表格以生成手动SQL。我很想写出来。我正在制作一个他们填写的电子表格,其中包含表名,列和值。它几乎总是插入。如果你看下面你会看到我的列插入SQL,它基本上只是跨越行,并选择列名称,然后选择值。

但是,有时列数可能会有所不同。因此它可能不会进入P7,它可能只是O7或Z7。

有没有办法获取在某种函数中填充的所有列?

= "INSERT INTO "&B1&"("&A7&","&B7&","&C7&","&D7&","&E7&","&F7&","&G7&","&H7&","&I7&","&J7&","&K7&","&L7&","&M7&","&N7&","&O7&","&P7&") SELECT "&A8&","&B8&","&C8&","&D8&","&E8&","&F8&","&G8&","&H8&","&I8&","&J8&","&K8&","&L8&","&M8&","&N8&","&O8&","&P8&" "

谢谢!

1 个答案:

答案 0 :(得分:0)

出于其他目的,我有一些非常相似的东西,但复杂程度更高。这就是我的代码的样子,也许你可以对你的文档提出一些想法。如果您需要任何附加信息,请告诉我。

Dim CounterA As Integer
For CounterA = 1 To Sheets.Count
    Dim CounterB As Integer
    CounterB = 0
    Dim CounterC As Integer
    CounterC = 0
    Dim break As Boolean
    break = False
    Dim break1 As Boolean
    break1 = False

    'Loop all column names
    Dim allColumns As String
    allColumns = ""
    CounterB = 0
    Do
        CounterB = CounterB + 1
        ColumnName = Sheets.Item(CounterA).range(Utils.Col_Letter(CounterB) & 1).Value

        If ColumnName = "" Then
            break = True
        Else
            If allColumns = "" Then
                allColumns = ColumnName
            Else
                allColumns = allColumns & ", " & ColumnName
            End If
        End If
    Loop While Not break
    CounterB = 1
    break = False

    'Loop all details
    Do
        CounterB = CounterB + 1

        Dim firstValue As String
        firstValue = Sheets.Item(CounterA).range("A" & CounterB).Value
        If firstValue = "" Then
            break1 = True
        Else
            'Loop all columns of a detail
            Dim allDetailValues As String
            allDetailValues = ""
            CounterC = 0
            break = False
            Do
                CounterC = CounterC + 1
                ColumnValue = Sheets.Item(CounterA).range(Utils.Col_Letter(CounterC) & CounterB).Text
                ColumnName = Sheets.Item(CounterA).range(Utils.Col_Letter(CounterC) & 1).Value

                ColumnValue = RTrim(ColumnValue)
                ColumnValue = Replace(ColumnValue, "'", "''")

                If ColumnName = "" Then
                    break = True
                Else
                    If allDetailValues = "" Then
                        allDetailValues = "'" + ColumnValue + "'"
                    Else
                        If ColumnValue <> "null" Then ColumnValue = "'" & ColumnValue & "'"
                        allDetailValues = allDetailValues & ", " & ColumnValue
                    End If
                End If
            Loop While Not break

            Dim insertValid As Boolean
            insertValid = InsertRecord(CS, libb_dest, Sheets.Item(CounterA).Name, allColumns, allDetailValues)
            If Not insertValid Then
                Exit Sub
            End If
        End If
    Loop While Not break1
Next

ps,你可能也需要这个

Public Function Col_Letter(ByVal lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function
相关问题