1

I am working with Excel VBA Copy Paste. Cell R7 has formula =Max ("C77:AD81").

  • R7 = Highest Value for Month
  • F7 = Highest Value to date
  • Q7 = the date F7 was achieved

What I am trying to achieve is if R7 > F7, copy R7 Value to F7 and change the Q7 to = today.

All I'm achieving is R7 changes to max of ("C77:AD81") and the remaining code doesn't work. My code below.

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range, r As Range, rv As Long


    If Not Intersect(Target, Range("R7")) Is Nothing Then
        Set rng = Intersect(Target, Range("R7"))
        For Each r In rng
            'Change Best Peak Flow and Date Achieved
            Select Case r.Value
            Case Is > ("F7")
            Case Range("R7").Select
            Case Range("R7").Copy
            Case Range("F7").Select
            Case Range("F7").Paste
            Case ("R7") = ("F7")
            Case Range("Q5").Select
            Range("Q5") = Today()
            Application.CutCopyMode = False
            End Select
        Next r
    End If

End Sub
2
  • I think you have misunderstood the use of Select Case. I will take a look at your code in a moment. Commented Dec 29, 2017 at 0:05
  • Thank you any help would be appreciated. Commented Dec 29, 2017 at 0:11

3 Answers 3

1

My advice is not to use .select. You can program everything without a single .select. Recording and analyzing macros are very good starting point for learning VBA, but sometimes they are way too complicated. I prefer simple solutions so give this a try:

Private Sub Worksheet_Change(ByVal Target As Range)

If Range("F7") <> Range("R7") Then
    Range("F7") = Range("R7")
    Range("Q5") = Date
End If
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

So, your rng object is only 1 cell, because you specified 1 target range of R7. With this being said, your For Each...Next statement is redundant.

I also wouldn't even use Select Case at all, but I will leave it in the event you later want to build off of it.

Give this a shot

Private Sub Worksheet_Change(ByVal Target As Range)

    On Error GoTo ErrHandler    'Important to ensure events are reenabled
    Application.EnableEvents = False

    Dim rng As Range, r As Range, rv As Long
    Set rng = Intersect(Target, Range("R7"))
    If Not rng Is Nothing Then
        'Change Best Peak Flow and Date Achieved
        Select Case True
        Case r.Value > Range("F7").Value
            Range("F7") = Range("R7")
            Range("Q5") = Date
        End Select
    End If

    Application.EnableEvents = True
    Exit Sub
ErrHandler:
    Application.EnableEvents = True
    MsgBox Err.Number & vbNewLine & Err.Description

End Sub

8 Comments

The code still only updates ("R7"), ("F7") and ("Q5") don't update.
Good answer. Make sure you disable the events when you are inside the Worksheet_Change event and reenable them on exit. If you do not do so, changing a cell will trigger a recursive call to Worksheet_Change.
I think R7 needs to be copied to F7.
Thanks for the input Ioannis & SJR, you both are correct. @FatherGoose I have updated the code to reflect the issues that were presented.
The code update problem, ("R7"), ("F7") and ("Q5") don't update, still exist. Sorry people as a learner, I've obviously written some awful code.
|
1

I solved it.
Here is the code I used.

Private Sub Worksheet_Change(ByVal Target As Range)

'Change Best Peak Flow and Date Achieved

If Range("R7").Value > Range("F7").Value Then
    Range("R7").Select
    Selection.Copy
    Range("F7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("Q5").Select
    Selection.Copy
    Range("K7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
End If
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.