3

我正在尝试使用 VB 在 excel 表中绘制图表。

所以现在我按照 这里给出的说明

1-我在VS2010中开始了一个新的VB项目,叫做Excelgraph。

2- 默认情况下,我得到了 Form1.vb[Design]。

3- 在这个表单上,我通过从工具箱中拖动它来创建一个按钮。

4-我双击它并打开新的 Form1.vb。

5-我删除了该文件中自动生成的所有内容,即 Form1.vb 文件并粘贴了以下代码:

更新代码

这是另一个代码,是最新的,与 Visual Basic 6.0 兼容。

 Public Class Form1



  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As 
  System.EventArgs) Handles Button1.Click

    Dim oXL As Object        ' Excel application
    Dim oBook As Object      ' Excel workbook
    Dim oSheet As Object     ' Excel Worksheet
    Dim oChart As Object     ' Excel Chart

    Dim iRow As Integer      ' Index variable for the current Row
    Dim iCol As Integer      ' Index variable for the current Row

    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series


    ReDim aTemp(0 To cNumRows, 0 To cNumCols)

    'Start Excel and create a new workbook
    oXL = CreateObject("Excel.application")
    oBook = oXL.Workbooks.Add
    oSheet = oBook.Worksheets.Item(1)

    ' Insert Random data into Cells for the two Series:
    Randomize(Now().ToOADate())
    For iRow = 1 To cNumRows
        For iCol = 1 To cNumCols
            aTemp(iRow, iCol) = Int(Rnd() * 50) + 1
        Next iCol
    Next iRow
    oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp

    'Add a chart object to the first worksheet
    oChart = oSheet.ChartObjects.Add(50, 40, 300, 200).Chart
    oChart.SetSourceData(Source:=oSheet.Range("A1").Resize(cNumRows, cNumCols))

    ' Make Excel Visible:
    oXL.Visible = True

    oXL.UserControl = True

    End Sub



End Class

更新

我更新了如上所示的代码。

错误

    'aTemp' is not declared. It may be inaccessible due to its protection level.    
     c:\users\ybf4 \documents\visual studio 2010\Projects\Excelgraph2
     \Excelgraph2\Form1.vb

我设法消除了另外两个错误。如何消除此错误?

我在 Visual Studio 2010 上编译上述代码,Office 是 Office 2007。

4

3 回答 3

3

一个简单、琐碎的程序揭示了我所怀疑的错误——你不能改变不存在的东西的大小!

正如 Derek 所说,您需要将 ReDim 更改为 Dim - 或者您需要先声明它。

失败:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series

    ReDim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub

通过:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series
    Dim aTemp

    ReDim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub

通过:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Const cNumCols = 10      ' Number of points in each Series
    Const cNumRows = 2       ' Number of Series

    Dim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub

将鼠标悬停在 aTemp 上应该会告诉您这一点 - 它也应该用蓝色波浪线加下划线以指示问题。

于 2012-10-08T00:53:39.487 回答
1

It's been a long time since I did this, but just looking at the code I suspect you need to change:

ReDim aTemp(0 To cNumRows, 0 To cNumCols) 

To:

Dim aTemp(0 To cNumRows, 0 To cNumCols)

ReDim is used to re-dimension an array after it has been dimensioned (using the Dim statement)

于 2012-10-08T00:27:40.300 回答
0

那是非常古老的代码(它用于 VB3,因此比第一个 VB.Net 和 Excel 5 早了 4 代)

但是,如果您只是注释掉指示的两行,我相信您的代码应该可以正常运行。

添加到工作表中的条目的随机性可能会有所不同,但是由于无论如何您都将替换该代码(在哪里Rnd()使用),所以这无关紧要。(我假设您的目的不是生成随机图)

至于DoEvents,我不确定这是否有必要。

于 2012-10-05T07:04:24.123 回答