【问题标题】:Excel VBA: Inserting value in cell using xlDown and OffsetExcel VBA:使用 xlDown 和 Offset 在单元格中插入值
【发布时间】:2016-09-24 14:37:39
【问题描述】:

我在让我的代码从 InputBox 插入值时遇到了一些问题。特别是有问题的行如下:

Set p = nPoints.End(xlDown).Offset(1, 0)
            p.Value = nPointVal

这个应该做的是找到电子表格的最后一行,右边第一个可用的单元格,然后插入存储的值nPointVal。但是,它没有这样做,它根本不插入任何东西。非常感谢任何帮助,我的完整代码如下。

Sub takeTwo()

On Error Resume Next

Dim fNameString As Variant
Dim lNameString As Variant
Dim sEmailString As Variant
Dim nPointVal As Integer
Dim sEventName As String
Dim n As Integer, r As Long, c As Range, d As Range, e As Range, p As Range, sE As Range
Dim fName As Range, lName As Range, sEmail As Range, nPoints As Range
Dim lEvent As Integer
Set fName = ActiveSheet.Range("FirstName")
Set lName = ActiveSheet.Range("LastName")
Set sEmail = ActiveSheet.Range("eMailAddr")


fNameString = Split(Application.InputBox("First Names in comma delimited format.", Type:=2), ",")
lNameString = Split(Application.InputBox("Last Names in comma delimited format.", Type:=2), ",")
sEmailString = Split(Application.InputBox("Email Addresses in comma delimited format.", Type:=2), ",")
nPointVal = InputBox("Please enter a point value for this event")
sEventName = InputBox("Please enter the name of the event.")

lEvent = NextEmptyColumn(Range("A1"))
Set sE = Range("A1").Offset(0, lEvent)
sE.Value = sEventName
' sEventPos = sE.Offset(0, lEvent)

If fNameString <> False And lNameString <> False Then

    For i = LBound(fNameString) To UBound(fNameString)

        fNameString(i) = Trim(fNameString(i)) ' Trim off leading and trailing whitespace.
        lNameString(i) = Trim(lNameString(i)) ' Trim off leading and trailing whitespace.

        Set c = fName.Find(fNameString(i), LookIn:=xlValues, LookAt:=xlWhole)
        Set d = lName.Find(lNameString(i), LookIn:=xlValues, LookAt:=xlWhole)

        If c And d Is Nothing Then

            Set c = fName.End(xlDown).Offset(1, 0)
            c.Value = fNameString(i)
            Set d = lName.End(xlDown).Offset(1, 0)
            d.Value = lNameString(i)
            Set e = sEmail.End(xlDown).Offset(1, 0)
            e.Value = sEmailString(i)
            Set p = nPoints.End(xlDown).Offset(1, 0)
            p.Value = nPointVal

            Dim s As Range ' Our summation range
            Set s = Range(c.Offset(0, 5), c.Offset(0, c.EntireRow.Columns.Count - 1))

            ' c.Offset(1, 3).Formula = "=((" & s.Address & ""

        End If

    Next


End If

结束子

【问题讨论】:

  • 您永远不会将nPoints 的范围设置为任何值。如果您要查找列中的最后一个单元格,最好从工作表中的最后一行开始往上走。
  • 除了我觉得你想要If c Is Nothing And d Is Nothing Then而不是If c And d Is Nothing Then
  • On Error Resume Next 几乎总是一个非常糟糕的主意。它通常用作On Error Mislead Programmer。删除它,以便您可以查看错误所在。
  • 缓和我的评论:On Error Resume Next 在代码中间,紧随其后的是尝试打开文件以进行阅读,紧接着是像 If Err.Number &gt; 0 Then ... 这样的条件,紧接着是 @987654331 @, 是一个足够常见的错误处理习惯用法,并且没有问题。你的使用不是那样的——它是全局的,具有隐藏所有运行时错误的效果,使代码不必要地难以调试。不要全局抑制错误——尽可能消除它们,否则处理它们。
  • @Kyle:如果该范围尚未由相同的代码创建,我会将nPoints 设置为什么范围?

标签: vba excel


【解决方案1】:

于是我找到了一个更简单的解决方案:

Set p = fName.End(xlDown).Offset(0, lEvent)
            p.Value = nPointVal

这引用了插入名字所在的同一行,这是我们想要的,因为点值与这个人有关。如果这可能存在一些问题,请有人提出建议。

【讨论】:

    猜你喜欢
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2022-11-22
    相关资源
    最近更新 更多