Excel VBA时间卡 - 时钟输出功能时出错

时间:2017-01-04 03:14:36

标签: excel vba excel-vba

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

  1. 我创建的VBA似乎需要一个"填充物"中间空格以便使其工作。 IE空白,填充数据,空白,填充数据等。否则,它将不会在运行宏按钮时初始化。有没有更好的方法来执行此操作或在我的代码中导致错误?
  2. 我想知道是否也可以修改此代码以允许特定范围而不是H1到H21。我想移动实际的考勤卡以便于打印。
  3. 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
    

1 个答案:

答案 0 :(得分:0)

如果您专门查看范围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