检查单元格中的内容并输入系统时间

时间:2016-06-24 19:49:45

标签: excel-vba vba excel

我希望我的代码检查单元格中的内容。如果它是空白的,我希望它跳过单元格并转到下一个并执行相同的操作。如果它不是空白,我想要选择另一个单元格并输入当前时间。当我只检查一个单元格时,下面的代码工作,但是当我添加多个单元格(将要检查32个单元格)时,我得到'Next without For'。然后我迷路了。

Sub time()
If Range("f2") <> "" Then
     ActiveSheet.Range("f8").Select
     ActiveCell = Format(TimeValue(Now), "h:mm a/p")
Next
If Range("h2") <> "" Then
     ActiveSheet.Range("h8").Select
     ActiveCell = Format(TimeValue(Now), "h:mm a/p")
Next
If Range("j2") <> "" Then
     ActiveSheet.Range("j8").Select
     ActiveCell = Format(TimeValue(Now), "h:mm a/p")

Else
End If
End Sub

1 个答案:

答案 0 :(得分:0)

循环时仅使用def _sum(data, start=0): """_sum(data [, start]) -> (type, sum, count) Return a high-precision sum of the given numeric data as a fraction, together with the type to be converted to and the count of items. If optional argument ``start`` is given, it is added to the total. If ``data`` is empty, ``start`` (defaulting to 0) is returned. Examples -------- >>> _sum([3, 2.25, 4.5, -0.5, 1.0], 0.75) (<class 'float'>, Fraction(11, 1), 5) Some sources of round-off error will be avoided: >>> _sum([1e50, 1, -1e50] * 1000) # Built-in sum returns zero. (<class 'float'>, Fraction(1000, 1), 3000) Fractions and Decimals are also supported: >>> from fractions import Fraction as F >>> _sum([F(2, 3), F(7, 5), F(1, 4), F(5, 6)]) (<class 'fractions.Fraction'>, Fraction(63, 20), 4) >>> from decimal import Decimal as D >>> data = [D("0.1375"), D("0.2108"), D("0.3061"), D("0.0419")] >>> _sum(data) (<class 'decimal.Decimal'>, Fraction(6963, 10000), 4) Mixed types are currently treated as an error, except that int is allowed. """ count = 0 n, d = _exact_ratio(start) partials = {d: n} partials_get = partials.get T = _coerce(int, type(start)) for typ, values in groupby(data, type): T = _coerce(T, typ) # or raise TypeError for n,d in map(_exact_ratio, values): count += 1 partials[d] = partials_get(d, 0) + n if None in partials: # The sum will be a NAN or INF. We can ignore all the finite # partials, and just look at this special one. total = partials[None] assert not _isfinite(total) else: # Sum all the partial sums using builtin sum. # FIXME is this faster if we sum them in order of the denominator? total = sum(Fraction(n, d) for d, n in sorted(partials.items())) return (T, total, count) 。由于您的单元格是随机的,因此它们是独立测试,每个Next需要使用If

关闭
End If

做循环:

Sub time()

    If Range("f2") <> "" Then
       ActiveSheet.Range("f8") = Format(TimeValue(Now), "h:mm a/p")
    End If

    If Range("h2") <> "" Then
       ActiveSheet.Range("h8") = Format(TimeValue(Now), "h:mm a/p")
    End If

    If Range("j2") <> "" Then
       ActiveSheet.Range("j8") = Format(TimeValue(Now), "h:mm a/p")
    End If

End Sub