Excel VBA时间卡 – 时钟输出function出错

我已经创build了一个工作基本的时间卡,可以在多个用户每天运行excel。 我遇到了两个我正在寻求帮助的问题。

  1. 我创build的VBA似乎需要在两个空格之间填充一个“填充符”才能使其工作。 IE空白,填充数据,空白,填充数据等。否则,在运行macrosbutton时不会初始化。 有没有更好的方法来做到这一点,或者在我的代码中导致这个错误?
  2. 我想知道如果这个代码也可以修改,以允许一个特定的范围,而不是H1到H21。 我想移动实际的计时卡以便于打印。

Sub ClockInClockOut() 'This code looks for empty cells in the range H1:H21 'and allows the user to clock-in and clock-out. 'It looks at cells one by one and enters time stamp. Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer Dim currentRowValue As String sourceCol = 8 'column H has a value of 8 rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 'for every row, find the first blank cell and select it For currentRow = 1 To rowCount currentRowValue = Cells(currentRow, sourceCol).Value If IsEmpty(currentRowValue) Or currentRowValue = "" Then Cells(currentRow, sourceCol).Select Exit For End If Next ActiveCell.FormulaR1C1 = "=NOW()" Selection.NumberFormat = "[$-en-US]mm/dd/yyyy hh:mm AM/PM;@" Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False End Sub 

如果您正在专门查看范围H1:H21,下面的代码可能工作

 Sub ClockInClockOut2() Dim c As Range For Each c In Range("myTimestamps") If IsEmpty(c) Or c.Value = "" Then With c .Value = Now() .NumberFormat = "[$-en-US]mm/dd/yyyy hh:mm AM/PM;@" Exit For End With End If Next c End Sub