'首先要将“工程-引用”中的Mic… Excel…选中
Private Sub Command1_Click()
Dim S() As String, i As Integer, j As Integer
Dim xlApp
Dim xlBook
Dim xlSheet
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.DisplayAlerts = False '不显示对话框
Set xlBook = xlApp.Workbooks.Open("c:\1.xls") '打开已经存在的EXCEL工件簿文件
'Set xlBook = xlApp.Workbooks.open("c:\1.xls", password:="123") '打开已经存在的含有密码的EXCEL工件簿文件
'Set xlBook = xlApp.Workbooks.Add '新建工件簿
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets("Sheet1") '设置活动工作表
xlSheet.Cells(1, 1) = 1 '给单元格(row,col)赋值
'变量 = xlSheet.Cells(1, 1) '将单元格(row,col)中的值读取到一个变量
'xlSheet.Range("A1:B2").CopyPicture 1, 2 '将单元格A1至B2的内容复制到剪切板
'xlSheet.PrintOut '打印工作表
xlBook.SaveAs FileName:="c:\1.xls" '保存工作表,结束时一定别忘了保存
xlBook.Close (True) '关闭工作簿 这里的True表示退出时保存修改
xlApp.quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Sub
Option Explicit
Dim iCount As Long
Dim arrStr() As String
Private Sub Form_Load()
ReDim arrStr(0 To 255, 0 To 20) As String
iCount = 0
End Sub
Private Sub Command1_Click()
If iCount > 255 Then
MsgBox "超过行数限制"
Exit Sub
End If
Dim i As Long
For i = 0 To 20
arrStr(iCount, i) = Text1(i).Text
Next i
iCount = iCount + 1
For i = 0 To 20
Text1(i).Text = ""
Next i
End Sub
Private Sub Command3_Click() '保存(不合格)
If iCount > 255 Then Exit Sub
Dim Ex As Object
Dim ExBook As Object
Dim ExSheet As Object
Set Ex = CreateObject("Excel.Application")
Set ExBook = Ex.Workbooks.Add
Set ExSheet = ExBook.Worksheets("Sheet1") '打开
ExSheet.Activate '激活工作表
Ex.Visible = True
ExSheet.Range("A1:U1").Value = Array("产品型号", "测试日期", "测试时间", "峰峰值", "均方根值", "频率", "周期", "上升时间", "下降时间", "正脉宽", "负脉宽", "正占空比", "负占空比", "最大值", "最小值", "平均值", "幅度", "顶端值", "底端值", "过冲", "预冲")
Dim i As Long
Dim j As Long
For i = 0 To iCount - 1
For j = 1 To 21
ExSheet.Cells(i + 2, j) = arrStr(i, j - 1)
Next j
Next i
On Error Resume Next
Ex.ActiveWorkbook.SaveAs ("H:\测试数据保存(不合格).xls")
Set ExSheet = Nothing
Set ExBook = Nothing
Ex.Quit
Set Ex = Nothing
End Sub
其中iCount的限制你可以自行修改
简单。做的太多了,你先百度查查吧。