How to Excel Copy from one Sheet to another with vba - excel

I need some help for my Excel sheet,
I have the first sheet and this is blank
and on the seconde sheet i have a list and in the column D i can change the value starts from 1.
And when i changed in the column a cell then it should copy the row to the first sheet.
thanks
Regards

It is not clear for me what you want to do. If you want VBA to copy the row automatically, you should go to the module of the corresponding Worksheet in the VBA editor (in the list of the Wroksheets on the left, just double-click the Worksheet to open up the module associated with it). After that, write an event handler:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 Then
ActiveSheet.Rows(Target.Row).Copy Worksheets("Sheet1").Rows(whichRowYouNeed)
End If
End Sub
you should also write a routine to determine the 'whichRowYouNeed'
It may be the end of the filled range on Worksheet1

Hey sorry for my bad statement, i try it again :)
The first sheet is blank,
The Second sheet is that:
When i add a number in column D then I want to copy this row, in that case row 8, copy to the first sheet starting from row 5.
I hope now it is better to understand

Related

If I add a row in Worksheet 1; First column should be transferred to Worksheet 2; rest of the row should be empty in Worksheet 2

Hopefully I can explain the issue well. If not, PLEASE ask, I will try to explain it as good as I can.
I have 2 different worksheets.
Scenario 1:
Sheet 1 has 2 columns and several rows.
Sheet 2 has the same data in the first column as Sheet 1 but the data of the other columns are different.
Change to Scenario 2: If I add a row in Sheet 1 the first cell of this row (“new”) should also be automatically added in Sheet 2. And in the second/third/… row of the second worksheet should be no data, as somebody has to fill it in manually.
Just the first column has to be the same in both worksheets. BUT the row has to be added in the second worksheet automatically also but without data.
I really hope somebody can help me! Thanks so much in advance!
I tried it with macros and different formulas. But unfortunately I never got the right solution.
You can do this, using this macro (there might be some errors in it):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Sheets(2).Range("A1").End(xlDown).Offset(1, 0).Value = Target.Value
End If
End Sub
For your information, this macro needs to "belong" to your first sheet:

VBA Range_Copy Macro - How to apply same task to subsequent rows

I have a working spreadsheet with lists of items. One field could have 1 of 14 descriptions. I thought creating a macro would be quicker than manually going to a separate sheet and copying and pasting the desired description every time.
I am totally new to this, but was able to create a macro grabbing data in cell A1 in one sheet and adding that data to cell S3 in another sheet - see code below. (Each description has its own sheet with data in cell A1) My question is, how to move onto the next row and cell S4. Thanks in advance!
Sub Range_Copy()
Worksheets("Used Single Items Returns").Range("A1").Copy Worksheets("Gopher Items CSV Test").Range("S3")
End Sub
If you just want to copy into Col S on the selected row:
Sub Range_Copy()
Dim r
Set r = Selection.Cells(1).entirerow.range("S1") 'relative to row, not sheet
Worksheets("Used Single Items Returns").Range("A1").Copy r
End Sub

Excel sheets dependant on / mirroring each other

Let's say I have three sheets in Excel. Sheet 1 is the Master sheet, sheets 2 and 3 contain different sets of information with the same headers that feed into the table in the master sheet. Is there a way to make it such that I could edit information in sheet 1 and sheet 2 will change AND vice versa so I can edit info in sheet 2 that will update the master sheet?
You could solve it by having Vlookup-formulas in your Master sheet. That way, if you change anything in sheet 2 and 3 the Master will automatically be updated.
If the user changes anything in the Master sheet, you will have to build logic in VBA on that. One way to go is to format the Master sheet so that there is something that helps the VBA know what the formula should be in the edited cell, and also to know from where the data should come. Loosely one could set up the Master sheet like this:
Row 1 is hidden and contains the template formulas
Row 2 is hidden and is completely empty (this will make less problems with filtering)
Row 3 contains headers
Row 4 and down contains the data, using the formulas define in row 1
Add the Change event on the Master sheet, that sees if the changed cell was one with a formula. If so, it will examine the template formula to identify from where the data should come. Then it will update that cell in Sheet 2 or 3 with the new value that is entered in the Master sheet. After this, it will overwrite the value manually entered in the Master sheet with the formula from the template row.
The big job here is to write a parser that understands from which cell the vlookup will get it's value.
One thing that I overlooked is that the CHANGE event is triggered only ONCE if the user pastes several cells in one go. The TARGET will then contain several rows or columns.
So this is some kind of skeleton using the above idea...
Option Explicit
Dim ChangeEventDisabled As Boolean 'Flag for disabling the Change event
Public Sub Disable_ChangeEvent()
ChangeEventDisabled = True
End Sub
Public Sub Enable_ChangeEvent()
ChangeEventDisabled = False
End Sub
Sub Worksheet_Change(ByVal Target As Range)
Dim updatedValue As Variant
Dim SourceCell As Range
'While the MasterSHeet is populated intially, we don't want this event to do anything
If ChangeEventDisabled Then
'There are chenges being done in teh sheet that should not trigger updates of the source-sheets.
Else
'Only run the code if it was a data-cell that was changed
If Target.Row > 3 Then
'We are in the rows containg data
'Did the changed cell contain a Vlookup formula before the user changed the cells value?
If UCase(Cells(1, Target.Column).Formula) Like "=VLOOKUP(*" Then
'A vlookup normally populates this cell.
'To know from where the data normally comes, I will need to put back the formula in the changed cell.
'So, first save the new value that we will write in the source cell
updatedValue = Target.Value
'Insert the formula again in the cell
'As we will now CHANGE a cell in the Masterr sheet, a Change event will trigger. Disable it temporarily
Disable_ChangeEvent
Cells(1, Target.Column).Copy Destination:=Target
Enable_ChangeEvent
'Find out from which cell the data is being fetched by the Vlookup
Set SourceCell = MyMagicParsing(Target)
'Update the source-cell with the new value
SourceCell.Value = updatedValue
End If
End If
End If
End Sub
Function GetSourceCell(Target As Range) As Range
'This function should decipher the formula in the cell Target, and figure out from where
'the data is actually coming. It shoudl return the range which is the source of the data.
'As I dont know how to do that quickly, I just hardcode the cell that is the source.
GetSourceCell = Worksheets("Sheet2").Cells(67, 3)
End Function

Removing duplicates on two sheets in excel

I am trying to make a macro that will look at a list on sheet 2 and look for the same value on sheet 1. If/when it finds it, it would delete the row on sheet 1. Then it would end at the bottem of sheet 2. Any advice would help, thank you!
The best way to do this is to loop through the values on sheet 2. On each value, check if it exists on sheet 1, and if so, delete that row.
The key is in how you structure your loop, and how you plan for the unexpected. How does your code handle if the same value is listed more than once on sheet 1? What if there are blank spots in sheet 1?
My advice is to use a built-in function such as the .Find method. This can skip over unexpected spaces, and it can easily be called multiple times. More info on this and other methods is found by pressing F1 in VBA editor, or you can look here.
As has been pointed out elsewhere, avoiding using .Select is also good practice.
To get you started, here is an outline of what's possible, looking at information in Sheet2 rows 1 through 3, and replacing those values in Sheet1 rows 1 through 10:
Public Sub delete_selected_rows()
'look at sheet2, A1 through A3 for search values
For Each search_value In Worksheets(2).Range("A1:A3")
'as long as there is something to delete...
Do While Not Worksheets(1).Range("A1:A10"). _
Find(search_value.Value, lookat:=xlWhole) Is Nothing
'...delete that row
Worksheets(1).Range("A1:A10").Find(search_value.Value, _
lookat:=xlWhole).EntireRow.Delete
Loop
Next
End Sub

Moving rows to other sheets

Is there a way or formula that could move a certain row to a different sheet by just typing in the criteria and pressing enter?
Like if I have 3 sheets and one of the columns on each sheet was called status, and in this column I type COMP, could it move all the information to a sheet called COMP sheet?
Insert this code on your worksheets
Private Sub Worksheet_Change(ByVal Target As Range)
If (Range("A1").Value = "COMP") Then ' Replace "A1" for your cell that will contain COMP
ActiveSheet.Range("1:1").Copy _ ' Replace 1:1 by your souce row
Destination:=Worksheets("COMP").Range("2:2") 'replace 2:2 by your dest row
End If
End Sub
When the user changes any value in the Worksheet, if the value of "A1" is "COMP", the row you chose (1 in the example) is copied into your destination row (row 2 in the example) in worksheet COMP.
You can build a VBA Macro that does all of the moving for you, but that would require the user to manually run it, not just typing COMP in a cell. You can also have it update automatically without the user pressing COMP and enter by using a formula that references the other worksheets (but then it will always be there).
=Sheet1!A1
The above code will reference the top left cell in the Worksheet 'Sheet1'.

Resources