Help with 'simple' VBA code

Beau the dog

Board Regular
Joined
Mar 8, 2021
Messages
72
Office Version
  1. 365
Platform
  1. Windows
Hi,

New to VBA code and I'm trying to do something I'm sure you guys will find easy.

On my worksheet ("data Input") when cell H4 changes, I want the range in that worksheet (A4:AH4) to automatically copy (as value only) to another worksheet ("Archive2"). Each time this happens, I need it to copy this data on the next available row.

This is my effort...which doesn't work and I'm tired of YouTube now! The first Empty row on the Archive tab starts on A3

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = Range("H4").Address Then

Range("A4:AH4").Copy Sheets("Archive2").Range("A2").End(xlUp).Offset(1, 0)

End If

End Sub

Please help...!
 
Ok, so the cells being copied over contain formulas.
Do you want to keep them as formulas when copied over, or can we copy over just the values?
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim rw As Long

    Set rng = Intersect(Target, Range("H4:H53"))
    
'   Exit if update not made in H4:H53
    If rng Is Nothing Then Exit Sub

'   Copy row updated in
    For Each cell In rng
'       Get row of update
        rw = cell.Row
        Range(Cells(rw, "A"), Cells(rw, "AH")).Copy
        Sheets("Archive2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    Next cell

End Sub
 
Upvote 0
Solution
This time we get an error message...please see attachments
 

Attachments

  • image001.png
    image001.png
    10 KB · Views: 2
  • image001-2.png
    image001-2.png
    8.9 KB · Views: 2
Upvote 0
It ran just fine for me.
Do you have any merged cells or protected cells in your workbook?
Can you post a data example it is not working for?
 
Upvote 0
It ran just fine for me.
Do you have any merged cells or protected cells in your workbook?
Can you post a data example it is not working for?

Sorry that was my mistake, I had changed the Archive tab name. I've amended and it works fine. Thank you for your time. (y)(y)(y)(y)(y)
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,289
Members
448,885
Latest member
LokiSonic

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top