Sql 多个文档上的数据透视图在文档为空时返回0作为总和
我对多个excel文档和透视图有一个奇怪的问题: 我使用此报告文档通过多张excel表格获取信息: 问题是,如果我的一个文件实际上没有数据,我的数据透视图的结果完全是0! 我试着用一个Sql 多个文档上的数据透视图在文档为空时返回0作为总和,sql,excel,vba,odbc,Sql,Excel,Vba,Odbc,我对多个excel文档和透视图有一个奇怪的问题: 我使用此报告文档通过多张excel表格获取信息: 问题是,如果我的一个文件实际上没有数据,我的数据透视图的结果完全是0! 我试着用一个 `table" & i & "` WHERE `table" & i & "`.`Stunden` != 0 或 最后,结果是一样的。 作为workarround,它将数据表中的所有行设置为0,或者在开头添加一个伪字段,该字段的值为0 是否可以通过查询检查整个表是否为emtpy
`table" & i & "` WHERE `table" & i & "`.`Stunden` != 0
或
最后,结果是一样的。
作为workarround,它将数据表中的所有行设置为0,或者在开头添加一个伪字段,该字段的值为0
是否可以通过查询检查整个表是否为emtpy并返回excel可以理解的值
谢谢 reox
这很难看,但我认为它有效。它为每个工作簿创建测试数据透视缓存和数据透视表,并且仅当测试数据透视缓存中至少有on记录时,才会将工作簿添加到SQL。你可以用DAO做一些更短的事情
Sub MergeFiles()
Dim PT As Excel.PivotTable
Dim PC As Excel.PivotCache
Dim strConTest As String
Dim pcTest As Excel.PivotCache
Dim ptTest As Excel.PivotTable
Dim boolSheetHasRecords As Boolean
Dim strSQLTest As String
Dim arrFiles As Variant
Dim strSheet As String
Dim strPath As String
Dim strSQL As String
Dim strCon As String
Dim rng As Range
Dim i As Long
strPath = CurDir
ChDirNet ThisWorkbook.Path
arrFiles = Application.GetOpenFilename("Excel Workbooks (*.xls), *.xls", , , , True)
strSheet = "Sheet1"
If Not IsArray(arrFiles) Then Exit Sub
Application.ScreenUpdating = False
If Val(Application.Version) > 11 Then DeleteConnections_12
Set rng = ThisWorkbook.Sheets(1).Cells
rng.Clear
For i = 1 To UBound(arrFiles)
strConTest = _
"ODBC;" & _
"DSN=Excel Files;" & _
"DBQ=" & arrFiles(i) & ";" & _
"DefaultDir=" & "" & ";" & _
"DriverId=790;" & _
"MaxBufferSize=2048;" & _
"PageTimeout=5"
Set pcTest = ThisWorkbook.PivotCaches.Add(SourceType:=xlExternal)
strSQLTest = "SELECT * FROM `" & arrFiles(i) & "`.[" & strSheet & "$]"
With pcTest
.Connection = strConTest
.CommandType = xlCmdSql
.CommandText = strSQLTest
Set rng = ThisWorkbook.Sheets(1).Cells
rng.Clear
Set ptTest = .CreatePivotTable(TableDestination:=rng(6, 1))
If pcTest.RecordCount > 0 Then
boolSheetHasRecords = True
Else
boolSheetHasRecords = False
End If
End With
Set ptTest = Nothing
Set pcTest = Nothing
If boolSheetHasRecords Then
If strSQL = "" Then
strSQL = "SELECT * FROM `" & arrFiles(i) & "`.[" & strSheet & "$]"
Else
strSQL = strSQL & " UNION ALL SELECT * FROM `" & arrFiles(i) & "`.[" & strSheet & "$]"
End If
End If
Next i
If strSQL <> "" Then
strCon = _
"ODBC;" & _
"DSN=Excel Files;" & _
"DBQ=" & arrFiles(1) & ";" & _
"DefaultDir=" & "" & ";" & _
"DriverId=790;" & _
"MaxBufferSize=2048;" & _
"PageTimeout=5"
Set PC = ThisWorkbook.PivotCaches.Add(SourceType:=xlExternal)
With PC
.Connection = strCon
.CommandType = xlCmdSql
.CommandText = strSQL
Set rng = ThisWorkbook.Sheets(1).Cells
rng.Clear
Set PT = .CreatePivotTable(TableDestination:=rng(6, 1))
End With
With PT
With .PivotFields(1) 'Rep
.Orientation = xlRowField
.Position = 1
End With
.AddDataField .PivotFields(8), "Sales", xlSum 'Total
With .PivotFields(3) 'Region
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields(2) 'Date
.Orientation = xlColumnField
.Position = 1
.DataRange.Cells(1).Group _
Start:=True, _
End:=True, _
Periods:=Array(False, False, False, False, True, False, True)
End With
End With
End If
'Clean up
Set PT = Nothing
Set PC = Nothing
ChDirNet strPath
Application.ScreenUpdating = True
End Sub
子合并文件()
将PT设置为Excel.PivotTable
将PC设置为Excel.PivotCache
作为字符串的Dim strConTest
将pcTest设置为Excel.PivotCache
将ptTest设置为Excel.PivotTable
Dim Boolsheeths记录为布尔值
Dim strSQLTest作为字符串
将文件视为变体
将标准表变暗为字符串
将strPath设置为字符串
作为字符串的Dim strSQL
作为字符串的Dim strCon
变暗rng As范围
我想我会坚持多久
strPath=CurDir
ChDirNet ThisWorkbook.Path
arrFiles=Application.GetOpenFilename(“Excel工作簿(*.xls)、*.xls”、True)
strSheet=“Sheet1”
如果不是IsArray(arrFiles),则退出Sub
Application.ScreenUpdating=False
如果Val(Application.Version)>11,则删除连接
设置rng=ThisWorkbook.Sheets(1).Cells
明白了
对于i=1到UBound(ARR文件)
strConTest=_
“ODBC;”和_
“DSN=Excel文件;”&_
“DBQ=”&arrFiles(i)&“&”_
“DefaultDir=”&“&”;“&”_
“DriverId=790;”和_
“MaxBufferSize=2048;”&_
“页面超时=5”
Set pcTest=thiswoolk.PivotCaches.Add(SourceType:=xlExternal)
strSQLTest=“从`.&arrFiles(i)&`.[&strSheet&“$”中选择*”
用pcTest
.Connection=strConTest
.CommandType=xlCmdSql
.CommandText=strSQLTest
设置rng=ThisWorkbook.Sheets(1).Cells
明白了
设置ptTest=.CreatePivotTable(TableDestination:=rng(6,1))
如果pcTest.RecordCount>0,则
boolSheetHasRecords=True
其他的
boolSheetHasRecords=False
如果结束
以
设置ptTest=Nothing
设置pcTest=Nothing
如果boolSheetHasRecords那么
如果strSQL=“”,则
strSQL=“从文件(i)和”`.[&strSheet&“$”中选择*”
其他的
strSQL=strSQL&“联合所有选择文件(i)&”`.[&strSheet&“$]”
如果结束
如果结束
接下来我
如果是strSQL“”,则
strCon=_
“ODBC;”和_
“DSN=Excel文件;”&_
“DBQ=“&arrFiles(1)&””&_
“DefaultDir=”&“&”;“&”_
“DriverId=790;”和_
“MaxBufferSize=2048;”&_
“页面超时=5”
设置PC=ThisWorkbook.PivotCaches.Add(SourceType:=XExternal)
使用PC
.连接=strCon
.CommandType=xlCmdSql
.CommandText=strSQL
设置rng=ThisWorkbook.Sheets(1).Cells
明白了
Set PT=.CreatePivotTable(TableDestination:=rng(6,1))
以
与PT
带有.PivotFields(1)代表
.Orientation=xlRowField
.位置=1
以
.AddDataField.PivotFields(8),“销售”,xlSum'总计
带.PivotFields(3)'区域
.Orientation=xlPageField
.位置=1
以
带有.PivotFields(2)的日期
.Orientation=xlColumnField
.位置=1
.DataRange.Cells(1).组_
开始:=真_
结束:=正确_
句点:=数组(假、假、假、假、真、假、真)
以
以
如果结束
“清理
设置PT=无
设置PC=无
ChDirNet strPath
Application.ScreenUpdating=True
端接头
Sub MergeFiles()
Dim PT As Excel.PivotTable
Dim PC As Excel.PivotCache
Dim strConTest As String
Dim pcTest As Excel.PivotCache
Dim ptTest As Excel.PivotTable
Dim boolSheetHasRecords As Boolean
Dim strSQLTest As String
Dim arrFiles As Variant
Dim strSheet As String
Dim strPath As String
Dim strSQL As String
Dim strCon As String
Dim rng As Range
Dim i As Long
strPath = CurDir
ChDirNet ThisWorkbook.Path
arrFiles = Application.GetOpenFilename("Excel Workbooks (*.xls), *.xls", , , , True)
strSheet = "Sheet1"
If Not IsArray(arrFiles) Then Exit Sub
Application.ScreenUpdating = False
If Val(Application.Version) > 11 Then DeleteConnections_12
Set rng = ThisWorkbook.Sheets(1).Cells
rng.Clear
For i = 1 To UBound(arrFiles)
strConTest = _
"ODBC;" & _
"DSN=Excel Files;" & _
"DBQ=" & arrFiles(i) & ";" & _
"DefaultDir=" & "" & ";" & _
"DriverId=790;" & _
"MaxBufferSize=2048;" & _
"PageTimeout=5"
Set pcTest = ThisWorkbook.PivotCaches.Add(SourceType:=xlExternal)
strSQLTest = "SELECT * FROM `" & arrFiles(i) & "`.[" & strSheet & "$]"
With pcTest
.Connection = strConTest
.CommandType = xlCmdSql
.CommandText = strSQLTest
Set rng = ThisWorkbook.Sheets(1).Cells
rng.Clear
Set ptTest = .CreatePivotTable(TableDestination:=rng(6, 1))
If pcTest.RecordCount > 0 Then
boolSheetHasRecords = True
Else
boolSheetHasRecords = False
End If
End With
Set ptTest = Nothing
Set pcTest = Nothing
If boolSheetHasRecords Then
If strSQL = "" Then
strSQL = "SELECT * FROM `" & arrFiles(i) & "`.[" & strSheet & "$]"
Else
strSQL = strSQL & " UNION ALL SELECT * FROM `" & arrFiles(i) & "`.[" & strSheet & "$]"
End If
End If
Next i
If strSQL <> "" Then
strCon = _
"ODBC;" & _
"DSN=Excel Files;" & _
"DBQ=" & arrFiles(1) & ";" & _
"DefaultDir=" & "" & ";" & _
"DriverId=790;" & _
"MaxBufferSize=2048;" & _
"PageTimeout=5"
Set PC = ThisWorkbook.PivotCaches.Add(SourceType:=xlExternal)
With PC
.Connection = strCon
.CommandType = xlCmdSql
.CommandText = strSQL
Set rng = ThisWorkbook.Sheets(1).Cells
rng.Clear
Set PT = .CreatePivotTable(TableDestination:=rng(6, 1))
End With
With PT
With .PivotFields(1) 'Rep
.Orientation = xlRowField
.Position = 1
End With
.AddDataField .PivotFields(8), "Sales", xlSum 'Total
With .PivotFields(3) 'Region
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields(2) 'Date
.Orientation = xlColumnField
.Position = 1
.DataRange.Cells(1).Group _
Start:=True, _
End:=True, _
Periods:=Array(False, False, False, False, True, False, True)
End With
End With
End If
'Clean up
Set PT = Nothing
Set PC = Nothing
ChDirNet strPath
Application.ScreenUpdating = True
End Sub