【发布时间】:2011-09-03 16:24:47
【问题描述】:
已编辑
如果你们有一个可以帮助解决这个问题的链接,我真的很想阅读它,因为到目前为止我还没有看到任何有用的东西。
在访问中,我正在尝试将任意数据导出到 Excel,创建多个图表(现在只是处理饼图),格式化这些图表,然后将它们发送到空白(图表)工作表。到目前为止,我已经导出了数据并能够创建图表,但我只是不知道如何格式化它们。
我要做的格式化是去掉图例,把数据标签加上名称、值和百分比,然后将其移动到“图表”工作表中。
编辑我现在可以去掉图例,并插入带有名称、值和百分比的数据标签。我仍然坚持将 Chart 对象移动到新工作表,代码在底部。
我也尝试在 excel 中记录一个宏,稍微编辑它,然后将其移至访问,但我一直出错,通常出现类似于“此对象没有该方法”的错误。下面我将包含一个我可能会得到的测试表以及我如何创建饼图。
代码:
Function Excel_Export_Two_Column()
Dim db As DAO.Database, rs As DAO.Recordset
Dim WBO As Object, WSO As Object, WSO2 As Object, XLO As Object, oChart As Object
Dim x As Long, y As Long, z As Integer, strTab As String, strcompany As String
Dim endTable As Long
Dim tempName As String, tempNum1 As Long, tempNum2 As Long, totalEnd As Long
z = 1
Set db = CurrentDb()
Set rs = db.OpenRecordset("QRY2Col")
Set XLO = CreateObject("Excel.Application")
XLO.Application.Workbooks.Add
Set WBO = XLO.Application.ActiveWorkbook
Set WSO = WBO.Worksheets(1)
Set WSO2 = WBO.Worksheets(2)
WSO.Name = Left("export", 31)
For y = 0 To rs.Fields.Count - 1
WSO.Cells(1, 1) = "Num"
WSO.Cells(1, y + 2) = rs(y).Name
Next y
x = 1
Do While Not rs.EOF()
x = x + 1
WSO.Cells(x, 1) = x - 1
For y = 0 To rs.Fields.Count - 1
WSO.Cells(x, y + 2) = Trim(rs(y))
Next y
rs.MoveNext
DoEvents
Loop
WSO.Cells.Rows(1).AutoFilter
WSO.Application.Cells.Select
WSO.Cells.EntireColumn.AutoFit
x = 1
Do While WSO.Cells(x, 1) <> ""
x = x + 1
Loop
endTable = x - 1
WSO2.Cells(1, 1) = "Name"
WSO2.Cells(1, 2) = "Num"
totalEnd = 2
For x = 2 To endTable
If (WSO.Cells(x, 2) <> "") Then
tempName = WSO.Cells(x, 2)
tempNum1 = WSO.Cells(x, 3)
For y = 2 To totalEnd
If (WSO2.Cells(y, 1) = tempName) Then
tempNum2 = WSO2.Cells(y, 2)
WSO2.Cells(y, 2) = tempNum1 + tempNum2
Exit For
ElseIf (y = totalEnd) Then
WSO2.Cells(y, 1) = tempName
WSO2.Cells(y, 2) = tempNum1
totalEnd = totalEnd + 1
End If
Next y
End If
Next x
Set oChart = WSO2.ChartObjects.Add(500, 100, 500, 300).Chart
oChart.SetSourceData Source:=WSO2.Range("A1").Resize(totalEnd - 1, 2)
oChart.ChartType = 5
strcompany = "Export"
If Dir(CurrentProject.Path & "\COLA_AR_" & Format(Date, "yyyymm") & "_XXX_" & strcompany & ".xlsx") <> "" Then
Kill CurrentProject.Path & "\COLA_AR_" & Format(Date, "yyyymm") & "_XXX_" & strcompany & ".xlsx"
End If
Call WBO.SaveAs(CurrentProject.Path & "\COLA_AR_" & Format(Date, "yyyymm") & "_test_2_Col.xlsx")
WBO.Close savechanges:=True
Set WBO = Nothing
XLO.Application.Quit
Set XLO = Nothing
rs.Close
db.Close
End Function
表:注意这个表在Access中的一个Query(名为“QRY2Col”)中
Field1 Field2
CTOD 64646515
BFTBC2 6656532
WTOW 451512355
DT3 684321818
STC2 652553548
BFTBC2 12
DT3 84954987
ATCR 99999999
CTOD 64185435
BFTBC2 321569846
STC2 6543518
STC2 3518684
ATCR 35481354
数据标签代码
Set oChart = WSO2.ChartObjects.Add(500, 100, 500, 300).Chart
oChart.SetSourceData Source:=WSO2.Range("A1").Resize(totalEnd - 1, 2)
' Number corresponds to a pie chart
oChart.ChartType = 5
' Adds data Labels
oChart.SeriesCollection(1).HasDataLabels = True
' Format chart
oChart.SeriesCollection(1).DataLabels.ShowCategoryName = True
oChart.SeriesCollection(1).DataLabels.ShowPercentage = True
oChart.SeriesCollection(1).HasLeaderLines = True
oChart.Legend.Delete
尝试移动图表的代码
以下是我记录的示例(通过添加“oChart”进行编辑),但这仍然不起作用。突出显示的问题是“xlLocationAsNewSheet”,而 VBA 表示“未定义变量”。
oChart.Location Where:=xlLocationAsNewSheet
谢谢,
杰西·斯莫莫
【问题讨论】:
-
您是否有理由对所有这些代码使用后期绑定?使用早期绑定,您至少可以了解哪些方法可供您使用......即
Dim WBO As Excel.Application而不是Dim WBO As Object等 -
智能感知/早期绑定适用于开发人员。后期绑定适用于用户,因为这意味着您的应用在部署时更加健壮。大多数经验丰富的 Access 开发人员在早期绑定和后期绑定之间来回切换,即早期开发,后期部署。
-
@David 遗憾的是,我不得不在 VBA 编程方面尽我所能,我完全同意你的看法……但似乎 Jesse 仍处于开发阶段,早期绑定在这个阶段更合适......
-
每当我将变量声明为 Excel.Application 或任何类似的东西时,我都会得到“未定义用户定义的类型”。我尝试了 Excel.Workbook 和 Excel.Worksheet 但它们也不起作用。对象恰好工作(“工作”意思是“编译”)
-
那么,您有 Excel 对象库的参考资料吗?如果你不这样做,那么,不,你不能使用该类型库中定义的类型。您可能想阅读 Tony Toews 关于早期与晚期绑定的讨论:granite.ab.ca/access/latebinding.htm
标签: excel vba ms-access-2007 charts