vb文本框写入D盘已有的EXCEL文件中
发布网友
发布时间:2024-04-26 05:07
我来回答
共3个回答
热心网友
时间:2024-04-27 12:29
Private Sub Command1_Click()
If Not IsNumeric(Text1.Text) Then
MsgBox "请输入数字。"
Text1.SetFocus
Exit Sub
End If
Dim Row As Long
Dim ExlApp As Object
Dim ExlBook As Object
Dim ExlSheet As Object
Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.WorkBooks.Open("D:\123.xls")
Set ExlSheet = ExlBook.WorkSheets(1)
Row = 1
Do Until ExlSheet.Cells(Row, 1).Text = ""
Row = Row + 1
Loop
ExlSheet.Cells(Row, 1).Value = Val(Text1.Text)
ExlApp.DisplayAlerts = False
ExlApp.Save
ExlApp.Quit
Set ExlSheet = Nothing
Set ExlBook = Nothing
Set ExlApp = Nothing
MsgBox "保存成功"
End Sub
热心网友
时间:2024-04-27 12:32
工程-引用-Microsoft Excel 11.0 Object Library
下面给个简单的例子
Private Sub Command1_Click()
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim AppExcel As Object
Set xlExcel = CreateObject("Excel.Application")
xlExcel.Workbooks.Open "C:\123.xls"
Set xlBook = xlExcel.Workbooks("123.xls")
Set xlSheet = xlBook.Worksheets(1)
Text1.Text = xlSheet.Cells(1, 1).Value
Text1.Text = Text1.Text & "ADD"
xlSheet.Cells(1, 1).Value = Text1.Text
Application.DisplayAlerts = False
xlBook.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub
热心网友
时间:2024-04-27 12:34
特别注意:先在程序页面的最顶上加:
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
再在按钮下写:
If Dir("F:\zyn\vb\vb graduate\bbbb.bz") = "" Then '判断EXCEL是否打开
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("F:\bbb.xls") '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = "编号 "
xlsheet.Cells(1, 2) = "姓名 "
xlsheet.Cells(1, 3) = "性别 "
xlsheet.Cells(1, 4) = "出生日期 "
xlsheet.Cells(1, 5) = "所在单位 "
xlsheet.Cells(1, 6) = "籍贯 "
xlsheet.Cells(1, 7) = "职务 "
xlsheet.Cells(1, 8) = "职称 "
xlsheet.Cells(1, 9) = "工龄 "
xlsheet.Cells(1, 10) = "家庭住址 "
xlsheet.Cells(1, 11) = "联系方式 "
xlsheet.Cells(1, 12) = "备注"
For i = 2 To inta
xlsheet.Cells(i, 1) = myflexgrid.TextMatrix(i - 1, 0) '给单元格1行驶列赋值
xlsheet.Cells(i, 2) = myflexgrid.TextMatrix(i - 1, 1)
xlsheet.Cells(i, 3) = myflexgrid.TextMatrix(i - 1, 2)
xlsheet.Cells(i, 4) = myflexgrid.TextMatrix(i - 1, 3)
xlsheet.Cells(i, 5) = myflexgrid.TextMatrix(i - 1, 4)
xlsheet.Cells(i, 6) = myflexgrid.TextMatrix(i - 1, 5)
xlsheet.Cells(i, 7) = myflexgrid.TextMatrix(i - 1, 6)
xlsheet.Cells(i, 8) = myflexgrid.TextMatrix(i - 1, 7)
xlsheet.Cells(i, 9) = myflexgrid.TextMatrix(i - 1, 8)
xlsheet.Cells(i, 10) = myflexgrid.TextMatrix(i - 1, 9)
xlsheet.Cells(i, 11) = myflexgrid.TextMatrix(i - 1, 10)
xlsheet.Cells(i, 12) = myflexgrid.TextMatrix(i - 1, 11)
Next i
xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL中的启动宏
Else
MsgBox ("EXCEL已打开")
End If