|
楼主 |
发表于 2010-12-26 15:19
|
显示全部楼层
调出窗体后就可以选择字段生成单个工作表的短SQL语句:
Private Sub CommandButton1_Click() '显示短sql
Dim s$, i%
s = "SELECT "
If CheckBox1.Value Then s = s & "*,"
For i = 0 To d.Count - 1 '逐个动态添加的复选框对应着字典记录的字段名
If Me.Controls("CheckBox" & i + 2).Value Then s = s & d.Keys(i) & ","
Next
TextBox1.Text = Left(s, Len(s) - 1) & " FROM " & IIf(flag, "[工作表名]", "`路径`.`工作表名`")
End Sub
生成短SQL语句后可以进行编辑,如调整字段顺序、修改别名、添加where语句等。
下面是根据单个工作表短SQL语句生成SQL命令文本代码:
Private Sub CommandButton3_Click() '生成长sql
Dim s$, a, arr(), i%, j%, m%, temp$, str1$, s1$, s2$
s = TextBox1.Text
s1 = Split(s, ",")(0)
s2 = Split(s, "FROM")(1)
With ListView1
If InStr(s, ",") > 0 And InStr(s, "*") = 0 Then '含有字段名,不含有"*"
If InStr(Split(s, "FROM")(0), "as") = 0 Then '含有as
a = Split(Split(s, " ")(1), ",") '字段数组
Else
a = Split(Split(Split(s, "as")(UBound(Split(s, "as"))), " ")(1), ",")
End If
For i = 1 To .ListItems.Count '逐行ListView1数据
If .ListItems(i).Checked = True Then '选中
m = m + 1
ReDim Preserve arr(1 To m) '重新定义数组,以保存第m个工作表的SQL语句
For j = 0 To UBound(a) '逐个用逗号隔开的字段
If d.Exists(a(j)) Then '字典存在,即确认是工作表中存在的字段
If InStr(ds(.ListItems(i).Text & .ListItems(i).SubItems(1)), a(j) & ",") Then '该工作表存在该字段
arr(m) = arr(m) & a(j) & ","
Else
arr(m) = arr(m) & "0 as " & a(j) & "" & "," '该工作表不存在该字段要添加 0 as 字段
End If
End If
Next
If InStr(s, "SELECT ""工作簿名""") > 0 Then
temp = Replace(s1, "工作簿名", .ListItems(i).Text) & "," '用真正的工作簿名替换“工作簿名”
ElseIf InStr(s, "SELECT ""工作表名""") > 0 Then
temp = Replace(s1, "工作表名", .ListItems(i).SubItems(1)) & "," '用真正的工作表名替换“工作表名”
Else
temp = "SELECT "
End If
str1 = Replace(s2, "路径", Mypath & .ListItems(i).Text) '用真正的路径替换“路径”
str1 = Replace(str1, "工作表名", .ListItems(i).SubItems(1)) '用真正的工作表名替换from后面的“工作表名”
If CheckBox102.Value Then '工作表别名
arr(m) = temp & Left(arr(m), Len(arr(m)) - 1) & " FROM" & str1 & " `" & .ListItems(i).SubItems(1) & "`"
Else
arr(m) = temp & Left(arr(m), Len(arr(m)) - 1) & " FROM" & str1 & ""
End If
End If
Next
ElseIf InStr(s, "*") > 0 Then '含有"*",如果含有字段名也不做判断是否在工作表中存在,假定如果有字段也是都有的
For i = 1 To .ListItems.Count
If .ListItems(i).Checked = True Then
m = m + 1
ReDim Preserve arr(1 To m)
If InStr(s, "SELECT ""工作簿名""") > 0 Then
temp = Replace(s1, "工作簿名", .ListItems(i).Text) & ","
ElseIf InStr(s, "SELECT ""工作表名""") > 0 Then
temp = Replace(s1, "工作表名", .ListItems(i).SubItems(1)) & ","
Else
temp = "SELECT "
End If
str1 = Replace(s2, "路径", Mypath & .ListItems(i).Text)
str1 = Replace(str1, "工作表名", .ListItems(i).SubItems(1))
If CheckBox102.Value Then '工作表别名
arr(m) = temp & "* FROM" & str1 & " `" & .ListItems(i).SubItems(1) & "`"
Else
arr(m) = temp & "* FROM" & str1
End If
End If
Next
End If
End With
If m > 0 Then TextBox2.Text = Join(arr, " UNION ALL ") Else TextBox2.Text = "" '各个工作表的SQL语句用UNION ALL连接显示在TextBox2
End Sub
其他子程序就不再赘述了,附件中都标注着它们的用途。
由于对数据透视表理解不深,本方法就算是一个尝试吧,还请数据透视表高手来完善此方案。
附件中用了下面二位的附件:
yanjie版主的《透视表汇总5个工作簿》:http://club.excelhome.net/viewth ... 3208&highlight=
www8358坛友的《数据透视合并计算》:http://club.excelhome.net/thread-666030-1-1.html
在此表示衷心感谢。
[ 本帖最后由 zhaogang1960 于 2010-12-26 17:46 编辑 ] |
评分
-
1
查看全部评分
-
|