VBA+SQL transform pivot union联合查询的基础应用

Sub 项目状态查询()

'如果“项目状态”是未转运营那么实施状态是不能选择的,因为还没有实施。
'如果“项目状态”选择状态后,那么项目名称里面只显示该状态的项目名称。如果“项目状态”选择的不是未转运营,那么“实施状态”也要选择,并确定项目名称的选项。
'如果“统计值”是项目数,那么标题行就是客户角色。如果是实施时长,标题行就是实施的时间。
lst = Range("a1048576").End(xlUp).Row
If lst >= 4 Then
Range("a4:afd" & lst).ClearContents
End If

sAddress = ThisWorkbook.FullName

If Range("b1") = "未转运营" Or Range("b1") = "全部" Then
sAddress1 = Sheets("销售").Range("a3").CurrentRegion.Address(0, 0)

If Range("b1") = "全部" Then
temp1 = "辅助列"
Else
temp1 = "项目状态"
End If

If Range("d1") = "全部" Then
temp2 = "辅助列"
Else
temp2 = "城市维度"
End If

If Range("d2") = "全部" Then
temp3 = "辅助列"
Else
temp3 = "加盟类型"
End If

If Range("h2") = "全部" Then
temp4 = "辅助列"
Else
temp4 = "项目名称"
End If



Set cnn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open "Provider=microsoft.Ace.oledb.12.0;Extended Properties=Excel 12.0;Data Source=" & sAddress

strsql = "select * from [销售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "'"

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , count(*) as 统计 from (select * from [销售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 统计 from (select * from [销售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 统计 from (select * from [销售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 统计 from (select * from [销售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 统计 from (select * from [销售$" & sAddress1 & "] where " & temp1 & " = '" & Range("b1") & "' and " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "') group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


'sum(iif(isnull(项目名称),0,1))

rs.Open (strsql), cnn
Cells(5, 1).CopyFromRecordset cnn.Execute(strsql)


For Each Field In rs.Fields
aa = Field.Name
[a4].Offset(0, i) = Field.Name
i = i + 1
Next


Set rs = Nothing
Set cnn = Nothing
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ElseIf Range("b1") = "销售转运营" And Range("h1") = "项目数" Then
sAddress1 = Sheets("实施").Range("a3").CurrentRegion.Address(0, 0)



If Range("d1") = "全部" Then
temp2 = "辅助列"
Else
temp2 = "城市维度"
End If

If Range("d2") = "全部" Then
temp3 = "辅助列"
Else
temp3 = "加盟类型"
End If

If Range("h2") = "全部" Then
temp4 = "辅助列"
Else
temp4 = "项目名称"
End If

If Range("f1") = "全部" Then
temp5 = "辅助列"
Else
temp5 = "实施进度"
End If

If Range("f2") = "全部" Then
temp6 = "辅助列"
Else
temp6 = "实施类型"
End If



Set cnn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open "Provider=microsoft.Ace.oledb.12.0;Extended Properties=Excel 12.0;Data Source=" & sAddress

strsql = "select * from [实施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "'"

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , count(*) as 统计 from (select * from [实施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 统计 from (select * from [实施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 统计 from (select * from [实施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b3").Value & " as 统计 from (select * from [实施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform count(*) select " & Range("b2").Value & " as 统计 from (select * from [实施$" & sAddress1 & "] where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' and " & temp4 & " = " & "'" & Range("h2") & "' and " & temp5 & " = '" & Range("f1") & "' and " & temp6 & " = " & "'" & Range("f2") & "') group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


'sum(iif(isnull(项目名称),0,1))

rs.Open (strsql), cnn
Cells(5, 1).CopyFromRecordset cnn.Execute(strsql)


For Each Field In rs.Fields
aa = Field.Name
[a4].Offset(0, i) = Field.Name
i = i + 1
Next


Set rs = Nothing
Set cnn = Nothing


'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ElseIf Range("b1") = "销售转运营" And Range("h1") <> "项目数" Then
sAddress1 = Sheets("实施").Range("a3").CurrentRegion.Address(0, 0)
If Range("d1") = "全部" Then
temp2 = "辅助列"
Else
temp2 = "城市维度"
End If

If Range("d2") = "全部" Then
temp3 = "辅助列"
Else
temp3 = "加盟类型"
End If

If Range("f1") = "全部" Then
temp4 = "辅助列"
Else
temp4 = "实施进度"
End If

If Range("f2") = "全部" Then
temp5 = "辅助列"
Else
temp5 = "实施类型"
End If

If Range("h2") = "全部" Then
temp6 = "辅助列"
Else
temp6 = "项目名称"
End If


Set cnn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnn.Open "Provider=microsoft.Ace.oledb.12.0;Extended Properties=Excel 12.0;Data Source=" & sAddress

str1 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',项目开始时间,项目结束时间) as 天数,运营经理人数 as 人数,运营经理成本 as 成本,'运营经理' as 岗位,'城市运营组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str2 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',仓调研开始日期,仓调研结束日期) as 天数,仓调研人数 as 人数,仓调研成本 as 成本,'仓调研' as 岗位,'仓运营组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str3 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',配调研开始日期,配调研结束日期) as 天数,配调研人数 as 人数,配调研成本 as 成本,'配调研' as 岗位,'配运营组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str4 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',仓库规划实施开始日期,仓库规划实施结束日期) as 天数,仓规划人数 as 人数,规划成本 as 成本,'仓规划' as 岗位,'仓库规划组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str5 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',实施筹备开始日期,系统上线日期) as 天数,实施人数 as 人数,实施人员成本 as 成本,'实施' as 岗位, '实施组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str6 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',B2B进件资料审核开始时间,商品B2B平台发布上架日期) as 天数,电商人数 as 人数,null as 成本,'电商客服' as 岗位,'客服组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"

str7 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',首次入仓日期,首次发货日期) as 天数,库房人数 as 人数,null as 成本,'库房' as 岗位,'仓库运营组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"


str8 = "select 销售转运营日期,周期维度,项目名称,城市维度,加盟类型,客户角色,仓库名,实施进度,项目阶段,阶段状态,实施类型,辅助列,月份维度," & _
" datediff('d',驻场日期,离场日期) as 天数,驻场人数 as 人数,null as 成本,'驻场' as 岗位,'城市运营组' as 组名 from [实施$" & sAddress1 & "] " & _
" where " & temp2 & " = '" & Range("d1") & "' and " & temp3 & " = '" & Range("d2") & "' " & _
" and " & temp4 & " = " & "'" & Range("f1") & "' and " & temp5 & " = " & "'" & Range("f2") & "' " & _
" and " & temp6 & " = " & "'" & Range("h2") & "'"


strtotal = str1 & " union all " & str2 & " union all " & str3 & " union all " & str4 & " union all " & str5 & " union all " & str6 & " union all " & str7 & " union all " & str8

If Range("h1") = "实施人天(avg)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , avg(人数*天数) as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform avg(人数*天数) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform avg(人数*天数) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(人数*天数) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(人数*天数) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


ElseIf Range("h1") = "实施人天(sum)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , sum(人数*天数) as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform sum(人数*天数) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform sum(人数*天数) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(人数*天数) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(人数*天数) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


ElseIf Range("h1") = "成本(avg)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , avg(成本) as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform avg(成本) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform avg(成本) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(成本) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform avg(成本) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""

ElseIf Range("h1") = "成本(sum)" Then

strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , sum(成本) as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""

strsql = "transform sum(成本) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""

strsql = "transform sum(成本) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(成本) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""

strsql = "transform sum(成本) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""

'ElseIf Range("h1") = "项目数" Then
'
'strsql = "select " & Range("b3").Value & " , " & Range("b2").Value & " , count(*) as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & ""
'
'strsql = "transform count(*) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b3").Value & ""
'
'strsql = "transform count(*) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " , " & Range("b2").Value & " pivot " & Range("b2").Value & ""
'
'strsql = "transform count(*) select " & Range("b3").Value & " as 统计 from (" & strtotal & ") group by " & Range("b3").Value & " pivot " & Range("b2").Value & ""
'
'strsql = "transform count(*) select " & Range("b2").Value & " as 统计 from (" & strtotal & ") group by " & Range("b2").Value & " pivot " & Range("b3").Value & ""


End If

 


rs.Open (strsql), cnn
Cells(5, 1).CopyFromRecordset cnn.Execute(strsql)


For Each Field In rs.Fields
aa = Field.Name
[a4].Offset(0, i) = Field.Name
i = i + 1
Next


Set rs = Nothing
Set cnn = Nothing

 


Else

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 

End If

lst = Range("a1048576").End(xlUp).Row
If Range("b2") = "月份维度" Then

Range("A5:A" & lst).Select
Selection.NumberFormatLocal = "yyyy""-""m"
Range("A4:afd4").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("项目状态查询").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("项目状态查询").AutoFilter.Sort.SortFields.Add Key:=Range _
("A4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("项目状态查询").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ElseIf Range("b2") = "周期维度" Then
'Selection.NumberFormatLocal = "0""周"""
Range("A5:A" & lst).Select
Selection.NumberFormatLocal = "0""周"""
Range("A4:afd4").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("项目状态查询").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("项目状态查询").AutoFilter.Sort.SortFields.Add Key:=Range _
("A4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("项目状态查询").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End If

 


End Sub

转载于:https://www.cnblogs.com/shida-liu/p/9887354.html

SQL+数据透视表+VBA 使数据透视表走向更灵活,更智能,更适用。 这个是我和师傅一撇首度合作,他提供了文件并提出了要求,我帮他实现其效果 下面从几个方面解释一下: 1、功能 一个源文件和一个通过用SQL查询生成的数据透视表 将源文件拖到电脑的任意位置,甚至将文件名也改掉,用VBA配上代码和窗体找到文件,数据透视表仍然能够正常工作 2、套用 现在来讲讲怎么使做出来的东东适应大家的需要 2、1 用OLE DB窗口引用工作表或写SQL语句,因为用这个方法同VBA相通,copy下来代码区的的语句 2、2 打开透视表文件,将透视表中的字段全部拖出来,也就是变成一个空数据透视表。 右击下面工作表图标 或者 工具》宏》visual basic 编辑器,点击模块看到代码区 2、3 将2、1步骤copy的语句commandtext的数据Array中的引号中 .CommandText = Array(" ") 可能不同版本会有一些差别,同时SQL语句中如果添加了文本生成新字段,双引号要成对翻倍 如:"出库" AS 表单选项 要改成 ""出库"" AS 表单选项 2、4 语句太长的处理:在代码区如果你想好看一些,你可以插入“ _”来换行,当然不能插在一个单词或自动名等中间。 2、5 将文件存盘,重新打开就会有了数据,你可以将字段拖入数据透视表中,创建你自己的数据透视表, 2、6 这样文件就可以使用,相信VBA的引导不用教就可以交给别人使用了 下面附上代码,包含3个区: 1、 工作簿去,打开文件时工作 Private Sub Workbook_Open() Dim OP If Dir(Sheets("path").Range("A1")) = "" Then OP = MsgBox("源文件已被移走,请选择下列选项" + Chr(10) + "1、选择是,重新输入文件全名" + Chr(10) + "2、选择否,打开原有的数据透视表" + Chr(10) + "3、选择取消,关闭文件", vbYesNoCancel, "Scarlett温馨提示") If OP = vbYes Then UserForm1.Show End If If OP = vbNo Then ActiveWorkbook.Close True End If If OP = vbCancel Then Exit Sub End If Else Call refreshpv End If End Sub 2、窗体区,实现文件的查找 Private Sub CommandButton1_Click() Dim fopen As FileDialog Set fopen = Application.FileDialog(msoFileDialogFilePicker) fopen.Show TextBox1.Value = fopen.SelectedItems(1) Set fopen = Nothing End Sub Private Sub CommandButton2_Click() If InStr(TextBox1.Value, ".") > 0 Then Sheets("path").Range("A1") = TextBox1.Value Call refreshpv unload me Else MsgBox "文件名要带路径含后缀的文件名", "Scarlett_88温馨提示" TextBox1.SetFocus End If End Sub Private Sub CommandButton3_Click() Unload Me End Sub Private Sub TextBox1_Change() End Sub Private Sub UserForm_Activate() End Sub Private Sub UserForm_Click() TextBox1.Value = Sheets("path").Range("A1") End Sub 3、模块区,实现SQL语句的地址更新和刷新数据透视表的数据源 Sub refreshpv() With ActiveSheet.PivotTables("数据透视表1").PivotCache .Connection = Array( _ "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Sourc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值