If sentence in macro

elin tveito

New Member
Joined
Sep 28, 2006
Messages
2
I want to copy a line from on sheet to another using a macro, and I want to insert an if formula that tells the macro to overwrite an existing line if the category already exists, and paste the line on the bottom of the list if the category does not exist already.

Any idea how to do this?

Thanks:)
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Derek

Well-known Member
Joined
Feb 16, 2002
Messages
1,592
Hi there

Here is a very simple example to get you started.
Catagories are listed in Column A of sheet1 and Column A of sheet2. If you select a cell in column A of sheet1 and run the following code, it will check if there is a match in column A of sheet2 and if so, overwrite that row, else it will add it to the next vacant row on sheet2

Sub CopyValues()
If ActiveCell.Column <> 1 Then Exit Sub
If Selection.Rows.Count > 1 Then Exit Sub
x = ActiveCell.Value
Dim y As Long
On Error GoTo here
y = WorksheetFunction.Match(x, Sheets("Sheet2").Range("A:A"), 0)
Sheets("Sheet2").Rows(y).EntireRow.Value = ActiveCell.EntireRow.Value
MsgBox y
Exit Sub
here:
Sheets("Sheet2").Range("A65536").End(xlUp).Offset(1, 0).EntireRow.Value = ActiveCell.EntireRow.Value
End Sub

regards
Derek
 

Watch MrExcel Video

Forum statistics

Threads
1,114,384
Messages
5,547,621
Members
410,804
Latest member
bluepinky
Top