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:)
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

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
 

Forum statistics

Threads
1,171,366
Messages
5,875,171
Members
433,105
Latest member
NDave

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
Top