Set oExcel =
CreateObject("Excel.Application")
Dim oBook As Object
Dim oSheet As Object
Dim fName As String 'full file path and name
fName = App.Path & "\Data" & "\" & Label4.Caption & "" & "\" & Label4.Caption & ".xls"
Set oBook = oExcel.Application.Workbooks.Open(fName)
'opens the fname workbook
Set oSheet = oExcel.Application.ActiveSheet 'activate the first worksheet
我设法用以下方法存储了当前日期(label2.caption)和时间到(label1.caption):
oSheet.Range("A1").Value = Label2.Caption
oSheet.Range("B1").Value = Label1.Caption
oExcel.Quit
但是问题是我希望我的程序检查excel文件中是否已经保存了当前日期和时间,并存储午餐时间(如果有的话)。我的下一个问题是,如果我明天下午几点,我的程序如何在下一行存储数据?
答案 0 :(得分:0)
您可以简单地检查您要写入的当前单元格是否具有值(.Value = ""
)。如果没有,那么您可以继续。如果有值,则需要执行一些步骤:
a-选择当前列中的最后一个现有值(即: Ctrl + Down )
b-下一个单元格到达一个空单元格
c-写入值
这是单元格A1的代码。我确定您可以将其复制到B列。
If oSheet.Range("A1").Value = "" Then
'Cell A1 is empty
oSheet.Range("A1").Value = Label2.Caption
Else
'Cell A1 has a previous value. Let's select the closest empty cell down
oSheet.Range("A1").Selection.End(oExcel.xlDown).Select
'Now, we are on the last existing cell
'lets move one cell down to have an empty cell
oSheet.ActiveCell.Offset(1,0).Select
'Now we can write the value
oSheet.ActiveCell.Value = Label2.Caption
End If