我正在制作一个 excel 项目,用户将在其中选择日期并填写服务器/数据库名称,数据将从该名称填充到 Excel 工作簿中已经存在的具有正确列名的工作表中。现在将创建两个工作表(填充 sql 数据)和我要创建的每个工作表的枢轴。这是名为 sheet 的产品的第一个宏
Sub Prod()
ActiveWorkbook.Sheets("UserInput").Activate ' a sheet where date picker and db/server names are taken from user
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim slctqry As String
Dim dealdate As String
Dim today As String
Dim msg As String
msg = "Sorry, for this date data is yet to come"
today = Range("B2").Value 'B2 cell has today() function
today = Format(today, "yyyy-mm-dd")
dealdate = Range("B1").Value 'date picker is linked to this cell
dealdate = Format(dealdate, "yyyy-mm-dd")
con.ConnectionString = "Provider=SQLOLEDB;Data Source=sql123abce\sql01;Initial Catalog=sqldb;User ID=abcd;Password=Windows;Integrated Security=SSPI"
con.Open
If (dealdate > today) Then
MsgBox msg
ElseIf (dealdate = today) Then
slctqry = "select Number,Premium, TransactionID, money from traders(nolock)"
slctqry = slctqry & " where convert(date,tradedate,103)='" & dealdate & "'"
Set rs.ActiveConnection = con
rs.Open slctqry
ActiveWorkbok.Sheets("Prod").Activate ' prod named worksheet where data will be copied from SQL db
Range("A2").CopyFromRecordset (rs)
ElseIf (dealdate < today) Then
slctqry = "select Number,Premium, TransactionID, money from tradersaudit(nolock)"
slctqry = slctqry & " where convert(date,tradedate,103)='" & dealdate & "'"
Set rs.ActiveConnection = con
rs.Open slctqry
'Dim ws4 As Worksheet
ActiveWorkbook.Sheets("Prod").Activate
Range("A2").CopyFromRecordset (rs)
End If
con.Close
End Sub
对于从用户获取的 db/server 和数据将填写在名为 Test 的工作表中,使用的宏是
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Function GetConnectionString() As String
Dim strCn As String
strCn = "Provider=sqloledb;"
strCn = strCn & "Data Source=" & Range("Server") & ";"
strCn = strCn & "Initial Catalog=" & Range("Database") & ";"
If (Range("UserID") <> "") Then
strCn = strCn & "User ID=" & Range("UserID") & ";"
strCn = strCn & "password=" & Range("Pass")
Else
strCn = strCn & "Integrated Security = SSPI"
End If
GetConnectionString = strCn
End Function
Sub Test()
ActiveWorkbook.Sheets("UserInput").Activate
Dim ws As Worksheet
Dim Sql As String
Dim dealdate As String
Dim today As String
Dim msg As String
msg = "Sorry, for this date data is yet to come"
today = Range("B2").Value
today = Format(today, "yyyy-mm-dd")
dealdate = Range("B1").Value
dealdate = Format(dealdate, "yyyy-mm-dd")
' open connection
cn.ConnectionTimeout = 100
cn.Open GetConnectionString()
If (dealdate > today) Then
MsgBox msg
ElseIf (dealdate = today) Then
Sql = "select Number,Premium, TransactionID, money from traders(nolock)"
Sql = Sql & " where convert(date,tradedate,103)='" & dealdate & "'"
Set rs.ActiveConnection = con
rs.Open Sql
ActiveWorkbook.Sheets("Test").Activate ' test sheet is there alerady with proper column names
Range("A2").CopyFromRecordset rs
ElseIf (dealdate < today) Then
Sql = "select Number,Premium, TransactionID, money from traders(nolock)"
Sql = Sql & " where convert(date,tradedate,103)='" & dealdate & "'"
Set rs.ActiveConnection = cn
rs.Open Sql
ActiveWorkbook.Sheets("Test").Activate
Range("A2").CopyFromRecordset rs
End If
cn.Close
End Sub
现在数据已成功填充到工作表和测试表中。下一个宏用于创建 Pivot。
Dim bReport As Workbook, Report As Worksheet, pivotSheet As Worksheet 'To set up my workbook & worksheet variables.
Set bReport = Excel.ActiveWorkbook
Set Report = bReport.Worksheets.Add 'Create the worksheet to place the SQL data
Set pivotSheet = bReport.Worksheets.Add 'Create the worksheet to place the Pivot Table
Dim pivotSource As Range 'To set up the variable representing your pivot data.
Set pivotSource = Report.UsedRange 'You can define a specific range, but this assumes the data is the only thing on the Report sheet.
Dim tableName As String
tableName = "Pivot_Prod" 'name of pivot report i wanted to create from data in sheet Prod
bReport.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=pivotSource).CreatePivotTable TableDestination:=pivotSheet.Cells(1, 1), _
tableName:=tableName
Set pt = pivotSheet.PivotTables(tableName)
pivotSheet.PivotTableWizard TableDestination:=pivotSheet.Cells(1, 1)
Set pOne= pt.PivotFields("Number")
Set pTwo = pt.PivotFields("Premium")
Set pthree = pt.PivotFields("TransactoinID")
Set pFour = pt.PivotFields("money")
pOne.Orientation = xlRowField 'This assigns the orientation of a given field to xlRowField.
pTwo.Orientation = xlRowField
pTwo.Subtotals(1) = False 'This denotes there will be no subtotal for this field.
pThree.Orientation = xlRowField
pThree.Subtotals(1) = False
pFour.Orientation = xlDataField
pFour.NumberFormat = "$#,##0.00"
测试表也一样。
@loplateral-在您的代码文件中,您正在创建新工作表以从 sql db 获取数据。但是我应该在哪里以及如何将我的产品和测试表链接到缓存以使它们成为枢轴源?我的意思是在你的方法中,如果我调用宏 prod() 或 Test() 来获取我们在此处添加的名为 report 的工作表中的数据。那么在那种情况下我们怎么能继续呢?在上面的代码中,我在 pivotcache.add 代码行中遇到错误。好像需要小改动,看看能不能改正。