所以我试图让用户向电子表格提交时间并让它更新外部工作簿。更新工作正常,但我无法工作的是外部工作簿关闭之前的If语句,检查您正在更新的单元格是否为空。 如果单元格为空,请继续并更新,否则返回Message。
到目前为止代码:获取运行时错误424(需要对象)
Private Sub submit_Click()
Application.ScreenUpdating = False
Workbooks.Open Filename:="Test_Book.xlsx"
ThisWorkbook.Activate
Application.ScreenUpdating = True
Dim fDate As Date
Dim rw As Long
Dim srchRange As Range
Dim book2Name As String, book2 As Workbook
book2Name = "Test_Book.xlsx" ' Name of Source File
Set book2 = Workbooks(book2Name)
fDate = Format(CDate(Month.Text & "/" & Day.Text & "/2015"), "dd/mm/yy")
fTime = Format(Hour.Text & ":" & Min.Text, "hh:mm")
Set srchRange = book2.Sheets("TB").Range("B:B") ' Search Range
If Application.CountIf(srchRange, fDate) Then
rw = Application.Match(CLng(fDate), srchRange, 0) ' Return row of result
Else
MsgBox Format(fDate, "dd/mm/yy") & " not found."
End If
sCell = book2.Sheets("TB").Range("D" & rw)
If IsEmpty(sCell.Value) Then
sCell.Value = fTime
Else
MsgBox ("There is already a time in this cell")
End If
Application.DisplayAlerts = False
Workbooks("Master_Timesheet.xlsx").Close SaveChanges:=True
Exit Sub
End Sub
答案 0 :(得分:0)
sCell不是一个对象,它是一个包含值的变体:
尝试
Dim sCell as range
Set sCell=book2.Sheets("TB").Range("D" & rw)
If IsEmpty(sCell) then