注册 登录
编程论坛 VB6论坛

怎么把变量写入excel单元格(已经解决)

tianskon 发布于 2013-08-14 08:48, 622 次点击
我在界面那里画了很多textbox,我希望运行的时候,输入text后,VB帮我写入excel指定格中,请帮我看一下。
Private Sub Form_Load()
Dim d1 As String
Dim d3 As Integer
Dim d4 As String
Dim d5 As String '这里把d1,d4,d5定义为文本,d3定义为数字
Combo1.ListIndex = 0
End Sub

Sub biaoge
Dim excelID As Excel.Application
Set excelID = CreateObject("Excel.Application")
excelID.Workbooks.Open ("C:\22.xls")
excelID.Worksheets(1).Select
excelID.Cells(11, 1) = Text12.Text '这个地方写进去了
d3 = Len(d1) '这里格子长度是16个字符,我想判断它如果大于16个字符,就分开写,d1的值在子程序bianlian那里
If d3 > 16 Then
d4 = Left(d1, 16)
d5 = Right(d1, d3 - 16)
excelID.Cells(12, 4) = d4 '从这里开始写不进去
excelID.Cells(13, 1) = d5
Else
excelID.Cells(12, 4) = d1
End If
excelID.Workbooks.Close
excelID.Quit
End Sub

Sub bianlian
d1=Text2.Text & "hao" & Text3.Text
End Sub

Private Sub Command1_Click()
Call bianlian
Call biaoge
End Sub

新建一个模块,把变量放入模块中,然后引用

[ 本帖最后由 tianskon 于 2013-8-14 16:44 编辑 ]
2 回复
#2
tianskon2013-08-14 10:38
我在sub biaoge这一句后面加了一行:msgbox d1
结果发现弹出来的框是空白的,也就是说子程序的值没传过来,要怎么传呢,我试过把sub bianlian()改成public sub bianlian(),也试过public function bianlian(),好像都传不过来。(子程序bianlian里面按计划还会有c1,c2,c3等等的文本要传过来)
有没有高手看一下要怎么改呢
#3
风吹过b2013-08-14 17:46
Dim d1 As String
Dim d3 As Integer
Dim d4 As String
Dim d5 As String '这里把d1,d4,d5定义为文本,d3定义为数字

这一堆变量,如果需要 在程序所有的地方都使用,需要到 所有过程函数 前面定义。

不能在 Private Sub Form_Load  里面定义。
在这里面定义的,只在这个过程里 才生效。
1