2016-09-24 130 views
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 

末次

+3

您不會將範圍nPoints設置爲任何值。如果您正在嘗試查找列中的最後一個單元格,則最好從表格的最後一行開始往上走。 – Kyle

+1

除了我覺得你想'如果c是沒有和d是沒有然後'而不是'如果c和d是沒有然後' – user3598756

+1

'在錯誤恢復下一步'幾乎總是一個非常糟糕的主意。它通常用作「On Error Mislead Programmer」。刪除它,以便您可以查看錯誤的位置。 –

回答

0

所以,我發現了一個簡單的解決方案:

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

此引用同一行中插入的名字是,這是我們所希望的,因爲點值與此人有關。如果可能存在某些問題,請提供建議。

相關問題