
VB 编程 将数据写入EXCEL
VB编程问题,将随机数据,写入特定的EXCEL文件中。现有一个VB编程的问题,就是随机产生1~36之间的整数,并将随机产生的整数,写入特定的EXCEL位置,比如产生6这个...
VB 编程问题,将随机数据,写入特定的EXCEL文件中。
现有一个VB编程的问题,就是随机产生1~36之间的整数,并将随机产生的整数,写入特定的EXCEL位置,比如产生6这个数,就写入X行7列这个位置,产生数字3,就写入X行4列这个位置,以此类推,代码如下:
Dim a As Integer
Dim Row As Long
Dim t As Long
Dim d As Integer
Dim ExlApp As Object
Dim ExlBook As Object
Dim ExlSheet As Object
Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.Workbooks.Open("d:\测试\测试数据.xls")
Set ExlSheet = ExlBook.Worksheets(1)
d = InputBox("请输入注数", "注数输入")
For Row = 2 To d
a = Rnd * 36
Text1.Text = a
t = a + 1
ExlSheet.Cells(Row, t).Value = a
ExlSheet.Cells(Row, 59).Value = Date
ExlSheet.Cells(Row, 60).Value = Time
ExlApp.DisplayAlerts = False
ExlApp.Save
ExlApp.Quit
Set ExlSheet = Nothing
Set ExlBook = Nothing
Set ExlApp = Nothing
Next
运行时一直出现“实时错误91,对象变量或with块变量未设置,这是为什么呀?? 展开
现有一个VB编程的问题,就是随机产生1~36之间的整数,并将随机产生的整数,写入特定的EXCEL位置,比如产生6这个数,就写入X行7列这个位置,产生数字3,就写入X行4列这个位置,以此类推,代码如下:
Dim a As Integer
Dim Row As Long
Dim t As Long
Dim d As Integer
Dim ExlApp As Object
Dim ExlBook As Object
Dim ExlSheet As Object
Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.Workbooks.Open("d:\测试\测试数据.xls")
Set ExlSheet = ExlBook.Worksheets(1)
d = InputBox("请输入注数", "注数输入")
For Row = 2 To d
a = Rnd * 36
Text1.Text = a
t = a + 1
ExlSheet.Cells(Row, t).Value = a
ExlSheet.Cells(Row, 59).Value = Date
ExlSheet.Cells(Row, 60).Value = Time
ExlApp.DisplayAlerts = False
ExlApp.Save
ExlApp.Quit
Set ExlSheet = Nothing
Set ExlBook = Nothing
Set ExlApp = Nothing
Next
运行时一直出现“实时错误91,对象变量或with块变量未设置,这是为什么呀?? 展开
2个回答
展开全部
Option Explicit
Dim ExlApp As Object
Dim ExlBook As Object
Dim ExlSheet As Object
Private Sub Command1_Click()
If Not IsNumeric(Text1.Text) Then
MsgBox "请输入数字。"
Text1.SetFocus
Exit Sub
End If
Dim Row As Long
Row = 1
Do Until ExlSheet.Cells(Row, 1).Text = ""
Row = Row + 1
Loop
ExlSheet.Cells(Row, 1).Value = Val(Text1.Text)
MsgBox "保存成功"
End Sub
Private Sub Form_Load()
Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.WorkBooks.Open("C:\DATA.xls")
Set ExlSheet = ExlBook.WorkSheets(1)
ExlApp.DisplayAlerts = False
ExlApp.Visible = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
ExlApp.Save
ExlApp.Quit
Set ExlSheet = Nothing
Set ExlBook = Nothing
Set ExlApp = Nothing
End Sub
希望能解决您的问题。
Dim ExlApp As Object
Dim ExlBook As Object
Dim ExlSheet As Object
Private Sub Command1_Click()
If Not IsNumeric(Text1.Text) Then
MsgBox "请输入数字。"
Text1.SetFocus
Exit Sub
End If
Dim Row As Long
Row = 1
Do Until ExlSheet.Cells(Row, 1).Text = ""
Row = Row + 1
Loop
ExlSheet.Cells(Row, 1).Value = Val(Text1.Text)
MsgBox "保存成功"
End Sub
Private Sub Form_Load()
Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.WorkBooks.Open("C:\DATA.xls")
Set ExlSheet = ExlBook.WorkSheets(1)
ExlApp.DisplayAlerts = False
ExlApp.Visible = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
ExlApp.Save
ExlApp.Quit
Set ExlSheet = Nothing
Set ExlBook = Nothing
Set ExlApp = Nothing
End Sub
希望能解决您的问题。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-08-29
展开全部
For Row = 2 to d 对应的Next语句位置不对
在ExlApp.DisplayAlerts = False前面一行插入 Next 就可以了
最后的Next删掉
在ExlApp.DisplayAlerts = False前面一行插入 Next 就可以了
最后的Next删掉
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询