Copy cells from same row as a given cell

Mathilde

New Member
Joined
Dec 6, 2010
Messages
1
Hi,

I'm pretty new to VBA and need to solve the (probably) easy problem below:

I have an array (called OldData), that contains the addresses of cells. When the sheet is saved, it compares the old values with the new ones. If the value has changed, I need to copy the value of this cell, and 2 other cells in the same row into another worksheet (called Log).
To make it more simple, I tried to just copy the whole row in the new sheet, but that doesn't work either.
Also, as my cells contain formulas, I need to use some kind of "paste special", to copy the actual values, but couldn't find anything about that in the Excel VB help.

Thanks a lot for any help,

Mathilde



Private Sub Workbook_Open()
Dim r As Range, n As Long
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
End With
With Sheets("New materials list").Range("K4:K1000")
For Each r In .Cells
n = n + 1
ReDim Preserve OldData(1 To 2, 1 To n)
OldData(1, n) = r.Address: OldData(2, n) = r.Value
Next
End With
With Application
.Calculation = xlAutomatic
.EnableEvents = True
End With
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim i As Long
For i = 1 To UBound(OldData, 2)
If Worksheets("New materials list").Range(OldData(1, i)).Value <> OldData(2, i) Then
Worksheets("Log").Range("A1").EntireRow.Insert
Worksheets("New materials list").Range(OldData(1, i)).Rows.Copy _
destination:=Worksheets("Log").Range("A1")
End If
Next
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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