注册 登录
编程论坛 VB6论坛

求大神帮忙,谢谢哈,程序报错,说类型不匹配。报错 说为定义 Set excelsheet = excelbook.Worksheets("shee

yangfang577 发布于 2014-03-27 21:42, 381 次点击
Option Explicit

Dim excel As Application
Dim excelbook As Workbook
Dim excelsheet As Workbook
Dim x(1 To 4, 1 To 4) As Integer

Private Sub Command1_Click()
Randomize
Dim i, j As Integer
For i = 1 To 4
  For j = 1 To 4
    x(i, j) = 100 * Rnd()
  Next j
  Next i
  excel1.Range("a1:d4").Value = x
  excel1.Range("a1:d4").Select
  Set excelchart = excel.Charts.Add
  excelsheet.Application.Visible = True
  excelsheet.SaveAs App.Path + "\test.xls"

End Sub

Private Sub Form_Load()
Set excel = CreateObject("Excel.application")
Set excelbook = excel.Workbooks().Add
Set excelsheet = excelbook.Worksheets("sheet1")
End Sub

Private Sub form_unload(cancel As Integer)
excelsheet.Application.Quit
Set excelsheet = Nothing
End Sub

End Sub
3 回复
#2
lowxiong2014-03-27 22:18
Dim excel As excel.Application
Dim excelbook As excel.Workbook
Dim excelsheet As excel.Worksheet   '在这里你定义错误,所以类型不匹配
Dim x(1 To 4, 1 To 4) As Integer

Private Sub Command1_Click()
Randomize
Dim i, j As Integer
For i = 1 To 4
  For j = 1 To 4
    x(i, j) = 100 * Rnd()
  Next j
  Next i
  excelsheet.Range("a1:d4").Value = x
  excelsheet.Range("a1:d4").Select
  Set excelchart = excel.Charts.Add
  excelsheet.Application.Visible = True
  excelsheet.SaveAs App.Path + "\test.xls"

End Sub

Private Sub Form_Load()
Set excel = CreateObject("Excel.application")
Set excelbook = excel.Workbooks.Add
Set excelsheet = excelbook.Worksheets.Add
End Sub

Private Sub form_unload(cancel As Integer)
Set excelsheet = Nothing
excel.Quit
End Sub
#3
w3609894262014-03-27 22:24
版主正解
#4
yangfang5772014-03-28 10:22
谢谢大神
1