0
我在获取代码以从InputBox中插入值时遇到了一些麻烦。特别是有问题的线路如下:Excel VBA:使用xlDown和Offset插入单元格中的值
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设置为任何值。如果您正在尝试查找列中的最后一个单元格,则最好从表格的最后一行开始往上走。 – Kyle
除了我觉得你想'如果c是没有和d是没有然后'而不是'如果c和d是没有然后' – user3598756
'在错误恢复下一步'几乎总是一个非常糟糕的主意。它通常用作“On Error Mislead Programmer”。删除它,以便您可以查看错误的位置。 –