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」。刪除它,以便您可以查看錯誤的位置。 –