基本的VB语言,要被自己气哭了。
运行起来,提示这个,其实代码的功能基本都实现了。
Function openfiledlg() As String
With CommonDialog1
.DialogTitle = "打开水准观测文件"
.Filter = "(Excel File)|*.xlsx;*.xls"
.ShowOpen
End With
openfiledlg = CommonDialog1.filename
End Function
Private Sub 坝面组Input_Click(Index As Integer)
Dim xlapp As Object, i As Integer, n As Integer, filename As String, filepath As String, rng As Range, LDdate As String
Set xlapp = CreateObject("Excel.Application")
Dim xlbook As New Excel.Workbook '定义工作簿
Dim xlsheet As Excel.Worksheet '定义工作表
'Dim exlApp As New Excel.Application '定义exlapp为Excel文件
Dim exlbook As Workbook '定义exlbook为工作表文件
filename = openfiledlg()
Set exlbook = xlapp.Workbooks.Open(filename) 'Excel文件路径及文件名
xlapp.Visible = True '显示Excel窗口
'表头处理
LDdate = Application.InputBox("请输入观测日期,以便于一次性填入指定位置", "输入观测时间", "始")
For i = 1 To exlbook.Worksheets.Count
'表尾处理
Set rng = Sheets(i).Range("A1045876").End(xlUp).Offset(-3, 0)
rng.Value = "观测:白义鑫" **这里提示:应用程序定义或对象定义错误**
Sheets(i).Range("B4").Value = "Dini03"
Sheets(i).Range("D4").Value = LDdate
Sheets(i).Range("G3").Value = "土质:"
Sheets(i).Range("H3").Value = "砼(地面)"
Sheets(i).Range("I2").Value = "水准仪编号:"
Next
exlbook.Save '保存工作簿
xlapp.Quit '关闭Excel
xlapp.DisplayAlerts = False
End Sub