View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
engbe engbe is offline
external usenet poster
 
Posts: 9
Default Deleting and Copying Marco code

Dear JLatham,

YOU ARE AWESOME! Thank you so much for your help! The code worked
perfectly!!!! I really really appreciate the time and effort you put in to
creating the code and helping someone that you don't even know...it is so
nice to know that there are genuine people like you in the world! : )

"JLatham" wrote:

I changed the code a little, this might be better for you. This lets you
"protect" certain rows that you may not want to be deleted using the button.
It also does some cleanup at the end to release resources back to the system.

Sub CopyAndDeleteRow()
Const moveToSheetRowNumber = 5 ' always put in row 5
Const moveToSheetName = "Sheet2" 'change?
Const saveRows = 2 ' 1st row # that can be deleted
Dim moveToSheet As Worksheet

'confirm only one row selected, although
'more than one cell in the row may be
'selected
If Selection.Rows.Count 1 Then
MsgBox "You may only move/delete 1 row at a time."
Exit Sub ' just quit
End If
'don't delete row(s) with lower numbers than
'the value of saveRows - this will allow you
'to make sure "permanent" information is preserved.
'I've set it to 2 so that you can't delete row 1
'which presumably contains labels and the button.
If Selection.Row < saveRows Then
MsgBox "This row cannot be deleted with this feature"
Exit Sub
End If
'confirm user wants to delete the row
If MsgBox("Do you want to move & delete row #" _
& Selection.Row, vbYesNo, "Confirm") < vbYes Then
'they did not respond with [YES]
Exit Sub
End If
Selection.EntireRow.Copy
Set moveToSheet = Worksheets(moveToSheetName)
'insert data into a new empty row on the "new" sheet
moveToSheet.Range("A" & moveToSheetRowNumber).Insert
'delete the entire old row
Selection.EntireRow.Delete
Set moveToSheet = Nothing ' cleanup
End Sub


"engbe" wrote:

Hi, Can any one please help me with writing a code in visual basics?

What i am trying to do is this:
I want to create a button called 'close' in row 1(sheet 1) and when I click
this I want a marco to run which will delete the entire row that the button
is on and then paste that row into row 5 in worksheet 2. I want to repeat
this for the next row however, when the second row is pasted into worksheet 2
i want the second row to be pasted in row 5 and the first row move down to
row 6...

I hope this makes sense and would really appreciate any assistance from
anyone as i am not to familiar with writing up codes...THANK YOU in advance!