简体   繁体   中英

Excel VBA: Inserting value in cell using xlDown and Offset

I'm having some trouble getting my code to insert a value from an InputBox. The offending lines in particular are the following:

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

What this should do is find the last row of a spreadsheet, the first available cell to the right, and insert the stored value nPointVal . However, instead of doing so, it simply doesn't insert anything. Any help is greatly appreciated, and my full code is below.

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

End Sub

So I found a simpler solution:

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

This references the same row in which the inserted name is on, which is what we want because the point value is related to this person. Someone please give advice if there could be some issues with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM