想要在2个Excel文件之间传输数据

时间:2013-04-04 12:17:08

标签: excel

我有两个文件sit.xls和plan.xls。 在sit.xls

Roll No.    Roll No.    Roll No.
10          11          12
5659694     5659724     5659754
5659695     5659725     5659755

我希望当我点击rollno(5659724)时,它会自动转移或复制到plan.xls 其中包含

ROW 1       ROW 2
ROLL NO.    ROLL NO.

1 个答案:

答案 0 :(得分:0)

您可以在sit.xls中的选择更改事件上编写VBA代码,如下所示。以下代码将plan.xls中选定的单元格值复制到完全相同的位置。

  Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     ' The variable for current cell value
     Dim currentValue As String
     ' The variable for current cell address
     Dim ActiveCellAddress As String
     'Get the value of the current cell from the current sheet Sit.xls
     currentValue = ActiveCell.FormulaR1C1
     'Get the address of the current cell from the current sheet Sit.xls
     ActiveCellAddress = ActiveCell.Address
     'Activate the another workbook Plan.xls
     Workbooks("Plan.xls").Activate
     'select the desired range
     Workbooks("Plan.xls").Sheets("Sheet1").Range(ActiveCellAddress).Select
     'Copy the value to the desired location
     ActiveCell.FormulaR1C1 = currentValue
     'Activate the old workbook again
     Workbooks("Sit.xls").Activate
  End Sub