简体   繁体   English

Excel中的VBA访问格式图

[英]VBA Access Format Chart in Excel

EDITED已编辑

If you guys have a link that might help with this problem I'd really like to read it, because so far I haven't seen anything very useful.如果你们有一个可能有助于解决这个问题的链接,我真的很想阅读它,因为到目前为止我还没有看到任何非常有用的东西。

In access I'm trying to export arbitrary data to excel, create multiple charts (right now just working on a pie chart), format these charts and then send them to a blank (Chart) sheet.在访问中,我试图将任意数据导出到 excel,创建多个图表(现在只是在饼图上工作),格式化这些图表,然后将它们发送到空白(图表)表。 So far I've exported the data and am able to create the charts, I just have no idea how to format them.到目前为止,我已经导出了数据并能够创建图表,但我只是不知道如何格式化它们。

The formatting I want to do is to get rid of the legend, put data labels with the name, value, and percentage, and move it to a "Chart" sheet.我想要做的格式化是去掉图例,将数据标签与名称、值和百分比一起放置,并将其移动到“图表”工作表中。

Edit I am now able to get rid of the legend as well as insert the data labels with name, value and percentage.编辑我现在能够摆脱图例以及插入带有名称、值和百分比的数据标签。 I am still stuck on moving the Chart object to a new sheet, code at bottom.我仍然坚持将图表 object 移动到新工作表,代码在底部。

I've also tried to record a macro in excel, edit it slightly and then move it over to access but I keep erroring out, usually with an error similar to "This object doesn't have that method".我还尝试在 excel 中记录一个宏,稍微编辑它然后将其移至访问,但我一直出错,通常出现类似于“此 object 没有该方法”的错误。 Below I'll include a test table that I might get and how I create the pie chart.下面我将包含一个我可能会得到的测试表以及我如何创建饼图。

Code:代码:

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

Table: Note that this table is in a Query (named "QRY2Col") in Access表:注意这个表在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

Code for data labels数据标签代码

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

Attempted code to move chart试图移动图表的代码

Below is an example of what I recorded (edited by adding "oChart") but this still doesn't work.下面是我记录的示例(通过添加“oChart”进行编辑),但这仍然不起作用。 The problem that gets highlighted is the "xlLocationAsNewSheet" and VBA says that the "Variable is not defined".突出显示的问题是“xlLocationAsNewSheet”,VBA 表示“未定义变量”。

oChart.Location Where:=xlLocationAsNewSheet

Thank you,谢谢,

Jesse Smothermon杰西·斯莫莫

For the last part, try this:对于最后一部分,试试这个:

oChart.Location Where:=1

' xlLocationAsNewSheet = 1
' xlLocationAsObject = 2
' xlLocationAutomatic = 3

As David pointed out, you cannot use the types/enums etcetera defined in the Excel object library without a reference to it, thus you are stuck using integer constants instead.正如大卫指出的那样,您不能使用 Excel object 库中定义的类型/枚举等,而不引用它,因此您无法使用 integer 常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM