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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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