Copy what I write to seperate worksheet automatically

时间:2018-06-04 16:59:10

标签: excel vba

I want to be able to type into any cell in the worksheet and have it automatically copy to the second worksheet as I type. I know how I can copy from the current cell to another but I do not know how to do this live. I want to do this in VBA I know I can select both worksheets and just type but I want to do this in VBA.

Private Sub CommandButton1_Click()

Worksheets("Sheet1").Activate
ActiveCell.Copy
irow = ActiveCell.Row
icol = ActiveCell.Column

Worksheets("Sheet2").Cells(irow, icol).PasteSpecial xlPasteAll

End Sub

1 个答案:

答案 0 :(得分:1)

Try this short event macro:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
        Target.Copy Sheets("Sheet2").Range(Target.Address)
    Application.EnableEvents = True
End Sub

Because it is worksheet code, it is very easy to install and automatic to use:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the macro:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!