首页 > 解决方案 > 偏移量输入值不会在 VBA 中“打印”

问题描述

做一些VBA时有趣的问题;运行代码时,它不会“打印”第一个空行上的值。使用 f8 跳过低谷代码时,它确实有效。似乎在最后一个命令行中。代码是:

Call SlotjeEraf (Unlock/Unhide VBA)
Dim Answer As Variant
  Dim NextCell As Range
With Sheets("Table")
    Set NextCell = Cells(Rows.Count, "H").End(xlUp)
    If NextCell.Row > 1 Then NextCell = NextCell.Offset(1, 0)

    Answer = InputBox("Medewerkersnaam:")

    NextCell = Answer

    Call SlotjeErop (Lock VBA/Hide Sheet)

End With
End Sub

我个人觉得 NextCell = Answer 线是问题,但我似乎找不到它。

标签: excelvba

解决方案


结束(xlUp)与查找

这篇文章指的是OP代码的以下部分:

Dim NextCell As Range
Set NextCell = Cells(Rows.Count, "H").End(xlUp)
If NextCell.Row > 1 Then NextCell = NextCell.Offset(1, 0)

这种病

在行

If NextCell.Row > 1 Then NextCell = NextCell.Offset(1, 0)     ' Wrong

你忘了使用Set(NextCell是一个对象)。改用这个:

If NextCell.Row > 1 Then Set NextCell = NextCell.Offset(1, 0) ' Correct?

为什么“正确?” ?

如果NextCell是 cell H1,则可能是它为空并且代码将写入它。下一次' End(xlUp)'将再次返回H1现在不为空的单元格并将写入它......一次又一次......得出结论:

如果单元格H1是否为空且所有其他单元格为空,则代码将写入单元格H1,关键是您通常在第一行中有标题,并且代码不会将数据添加到以下行。

另一方面,如果至少在第 2 行中有数据(例如第 2 行中的标题),则代码将起作用。

提醒

使用时End(xlUp),您始终必须牢记以下几点:

  1. 如果某列没有数据或只有第一
    行有数据,则返回该列的第一个单元格(在FirstxlUp1FirstxlUp2、 中表示FirstFind)。

  2. 当行被隐藏或过滤(仅参考)时,它也可能返回不需要的结果FirstFind

治愈

以下是一些避免上述问题的解决方案。为了更好地理解,它们已被简化。

Option Explicit

Sub FirstxlUp1()
    Dim NextCell As Range
    With ThisWorkbook.Worksheets("Sheet1")
        Set NextCell = .Cells(.Rows.Count, "H").End(xlUp)
    End With

    If NextCell.Row = 1 Then
        If NextCell.Value <> "" Then Set NextCell = NextCell.Offset(1)
    Else
        Set NextCell = NextCell.Offset(1)
    End If

    NextCell.Value = "TestxlUp1"
End Sub

Sub FirstxlUp2()
    Dim NextCell As Range
    With ThisWorkbook.Worksheets("Sheet1")
        Set NextCell = .Cells(.Rows.Count, "H").End(xlUp)
    End With

    If NextCell.Row > 1 Then
        Set NextCell = NextCell.Offset(1)
    Else
        If NextCell.Value <> "" Then Set NextCell = NextCell.Offset(1)
    End If

    NextCell.Value = "TestxlUp2"
End Sub

Sub FirstFind()
    Dim NextCell As Range
    With ThisWorkbook.Worksheets("Sheet1")

        ' Try to find a non-empty cell in the column; 'xlFormulas'
        ' includes finding cells with formulas evaluating to "".
        Set NextCell = .Columns("H").Find(What:="*", LookIn:=xlFormulas, _
          SearchDirection:=xlPrevious)
        ' Or:
        'Set nextCell = .Columns("H").Find("*", , xlFormulas, , , xlPrevious)

        ' Check if a cell was found. If a non-empty cell was not found,
        ' the range evaluates to 'Nothing'.
        If Not NextCell Is Nothing Then
            ' Found the last non-empty (bottom-most) cell in the column.
            Set NextCell = NextCell.Offset(1)
        Else
            ' All cells in the column are empty.
            Set NextCell = .Cells(1, "H")
        End If

    End With
    NextCell.Value = "TestFind"
End Sub

真正的治愈

在实践中,您应该始终使用 headers。然后你可以避免所有这些'If-ing'并使用以下单行在这样的列中找到'第一个'空单元格:

Set NextCell = Cells(Rows.Count, "H").End(xlUp).Offset(1)
' or:
Set NextCell = Columns("H").Find(What:="*", LookIn:=xlFormulas, _
  SearchDirection:=xlPrevious).Offset(1)
' Or:
'Set nextCell = Columns("H").Find("*", , xlFormulas, , , xlPrevious).Offset(1)

如果您要隐藏或过滤行,请专门使用 Find 方法。


推荐阅读