Jerome Deriaz
Jerome Deriaz

Reputation: 1

Excel VBA set something in Range as ActiveCell.Offset

This is probably very basic for you guys, but I just started using VBA in Excel and eventhough I have a pretty good understanding of the logic behind the functions, I can't seem to find what I am looking for ...

Here is my code :

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

Set KeyCells = Range("A4:A27")

If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
    Dim myValue As Variant
    myValue = InputBox("Indiquer la quantité", "Quantité")
    Range("C4").Value = myValue

    If Cells("4", "C") > 1 Then
        MsgBox (myValue & " produits sélectionnés")
    End If

    If Cells("4", "C") < 1 Then

    End If
End If

Basically my document is a table where you select one cell after the other in range ("A4:A27"). I want the result of my InputBox to go in an Offset cell (0,2). I set "C4" so that I wouldn't get any error but obviously when I select A5 my result still goes in C4 when I would like it to go in C5....

What is the function to use ?

Thks for answers

Upvotes: 0

Views: 185

Answers (1)

BruceWayne
BruceWayne

Reputation: 23283

I think you will need to use the Target cell. Does this do what you're looking for?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

Set KeyCells = Range("A4:A27")

If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
    Dim myValue As Variant
    myValue = InputBox("Indiquer la quantité", "Quantité")
    Range("C" & Target.Row).Value = myValue

    If Cells(Target.Row, "C") > 1 Then
        MsgBox (myValue & " produits sélectionnés")
    End If

    If Cells(Target.Row, "C") < 1 Then

    End If
End If

Upvotes: 0

Related Questions