Vba code for adding information to the right place

2019-09-06 07:05发布

I am trying to find solution for my code, where first the user is asked a player's name and then the macro searches for the name in my database. If the name is there then the macro asks how many goals the player has made. Then the number of goals written to the inputBox is added to the player's information.

My problem is that the macro doesn't add the number of goals for the player that has been searched. It adds the number of goals and replaces the players name

Here is my code right now:

Sub maalit()

   Dim ws As Worksheet
   Dim lRow As Long
   Dim strSearch As String
    Set ws = Worksheets("Data")


Dim etsi As String
etsi = InputBox("Etsi Jäsen", "maalien lisääminen")   'asks the players name


If Trim(etsi) <> "" Then
        With Sheets("Data").Range("A:A")
            Set Rng = .Find(What:=etsi, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then

   tulos = InputBox("Anna pelaajan maalienmäärä", "maalien lisääminen")
    Rng.Value = tulos        'asks the number of goals but this is the problem place the bacause it adds them to the wrong column i want them to be in column G


        Else
                MsgBox "Jäsentä ei löytynyt"
            End If
        End With
    End If
End Sub

2条回答
乱世女痞
2楼-- · 2019-09-06 07:36

You can use OFFSET, for example:
Change Rng.Value = tulos to Rng.Offset(0, 1).Value = tulos to move the input 1 column to the right. Just change the 1 to however many columns you need to hop in order to end up in column G.

Alternatively, you can construct a new range:
Change Rng.Value = tulos to Range("G" & rng.Row).Value = tulos

查看更多
混吃等死
3楼-- · 2019-09-06 07:54

You should use Offset so replace:

 Rng.Value = tulos 

By:

Rng.Offset(, 6).Value = tulos

查看更多
登录 后发表回答