我怎样才能找到特定的数据,比如我的 Excel 文件中的日期,然后使用 VB 6.0 在它旁边的列上写时间?

How can I find a specific data, such as a Date in my Excel file, then write time on the column next to it using VB 6.0?

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 文件中是否已经保存了当前日期和时间,如果有则存储午餐外出时间。我的下一个问题是如果我明天计时,我的程序如何在下一行存储数据?

您可以简单地检查您尝试写入的当前单元格是否有值 (.Value = "")。如果没有,那么您可以继续。如果有值,你需要做一些步骤:

a- Select 当前列中的最后一个现有值(即: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