简体   繁体   中英

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

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

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