gpt4 book ai didi

excel - 如何在我的 Excel 文件中找到特定数据,例如日期,然后使用 VB 6.0 在其旁边的列上写入时间?

转载 作者:行者123 更新时间:2023-12-04 20:53:08 25 4
gpt4 key购买 nike

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

关于excel - 如何在我的 Excel 文件中找到特定数据,例如日期,然后使用 VB 6.0 在其旁边的列上写入时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53578297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com