Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Got a bit of an Excel query that I am struggling with.
Say you have two worksheets - Data1 and Data2.
Both sheets will have identical columns and column headings. But Data1 will be the main sheet where all the data is inputted to, save for one column.
When this column does get data entered into it, I want the spreadsheet to move that said row to Data2, also meaning that it is deleted from Data1. Then, the next time this action occurs, I will obviously need it to move the data to the next row down on Data2.
I've been fiddling with a few Macro examples I've found but no luck. Is there a simple way to do this?
Any help would be GREATLY appreciated
Cheers

Assuming you want to move the row immediately after the data is entered, use a sheet-level Change macro with the data entry Column as the target.
When a change is made to the target column, cut/paste the target row to the other sheet.
Use this to keep track of how many rows there are in your destination sheet:
LastRow = Sheets("Data2").Range("A" & Rows.Count).End(xlUp).Row

Thanks for the advice.
I'm still looking into this and still having problems. I did find this example online:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 And Target.Cells.Count = 1 Then
If Target.Value <> "" Then
Target.EntireRow.Cut
Sheets("Sheet2").Activate
ActiveSheet.Range("A1").Select
ActiveSheet.Range("A1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Sheet1").Activate
Target.EntireRow.Delete
End If
End If
End Sub....But I just get this error when testing it:
http://i72.photobucket.com/albums/i...
I have no idea why it's refusing to work!

What did you try as far as troubleshooting the code to see why it was giving you the error?
If you clicked Debug in the error window it would have highlighted:
ActiveSheet.Range("A1").End(xlDown).Offset(1, 0).Select
Excel is finicky about .End(xlDown) in an empty column or a column with data in just Row 1. In those cases it goes all the way down to the very last cell in the column. Therefore when you try to Select the next cell down with .Offset(1,0) it crashes.
Put some data in A2 and the code will sort of work, but not really.
When I tried it (after putting something in A2 so it didn't error) it did indeed cut the data from Sheet1 and put it in Sheet2, but it deleted the wrong row in Sheet1. When the code pasted the data into Sheet2, it reset the Target address to the destination address in Sheet2 and then deleted the corresponding row in Sheet1.
In addition, rarely do you need to Activate a sheet or Select a cell to perform an operation on it. It's much more efficient if you let VBA do it's work in the background.
Here's the code I would use:
Private Sub Worksheet_Change(ByVal Target As Range)
'Determine if the Target is in
'Column 6 and contains data
If Target.Column = 6 And Target.Cells.Count = 1 Then
If Target.Value <> "" Then
'Store Target Address
MyAddress = Target.Address
'Find Next Empty Cell In Sheet2 Column A
NextRow = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
'Cut and Paste Target Row to Sheet2
Target.EntireRow.Cut Destination:=Sheets(2).Range("A" & NextRow)
'Delete Target Row in Sheet1
Range(MyAddress).EntireRow.Delete
End If
End If
End Sub

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |