中易网

vb链接EXCEL表格的错误

答案:2  悬赏:0  
解决时间 2021-04-28 15:59
  • 提问者网友:無奈小影
  • 2021-04-27 21:37
本人菜鸟,写了如下代码,发现在表格中没有输出的字符,不知哪里出现了错误,请高手帮忙看看
代码如下:Private Sub Command1_Click()
Set xlapp = CreateObject("excel.application")
xlapp.Visible = True
Set xlbook = xlapp.Workbooks.open("F:\信息登入相关.xls")
Set xlsheet = xlbook.Worksheets(1)
For i = 2 To xlsheet.usedrange.rows.Count
xlsheet.Cells(i, 1) = Text1.Text
xlsheet.Cells(i, 2) = Text2.Text
xlsheet.Cells(i, 3) = Text3.Text
xlsheet.Cells(i, 4) = Text4.Text
xlsheet.Cells(i, 5) = Text5.Text
xlsheet.Cells(i, 6) = Text6.Text
xlsheet.Cells(i, 7) = Text7.Text
xlsheet.Cells(i, 8) = Text8.Text
xlsheet.Cells(i, 9) = Text9.Text
xlsheet.Cells(i, 10) = Text10.Text
Next i
If Text1 = "" Or Text2 = "" Or Text3 = "" Or Text4 = "" Or Text5 = "" Or Text6 = "" Or Text7 = "" Or Text8 = "" Or Text9 = "" Or Text10 = "" Then
MsgBox "您的信息有缺失!", , "提示"
End If
xlbook.Save
End Sub
Private Sub Form_Unload(Cancel As Integer)
xlbook.Close (True)
xlapp.Quit '结束EXCEL对象
Set xlsheet = Nothing
Set xlbook = Nothing
Set xlapp = Nothing '释放xlApp对象
End Sub
最佳答案
  • 二级知识专家网友:懂得ㄋ、沉默
  • 2021-04-27 22:04
我知道了,你是要把VB中的数据写到Excel中,对吧。
那你的
For i = 2 To xlsheet.usedrange.rows.Count
就不对,你要循环Vb中控件的数量,不能循环Excel中的数量。因为在写入数据之前,Excel里没有数据,是吧。你考虑下。
全部回答
  • 1楼网友:风格单纯
  • 2021-04-27 22:57
不要使用odbc的,直接使用microsoft ole db的连接串。 provider=microsoft.jet.oledb.4.0;data source=c:\1.xls;extended properties=\"excel 8.0;hdr=no;imex=0\"
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息