0

我在 MS Excel 文件的第一个工作表上有一个表格。如果我创建一个新工作表,我想用第一个工作表中的表中的某些行和列填充它。

具体来说,如果我在第一个工作表中有下表:

________________________________________________________
| Header 1 | Header 2 | Header 3 | Header 4 | Header 5 |
-------------------------------------------------------
|   Data   |   Data   |   Data   |   Data   |   Data   |
--------------------------------------------------------
|   Data   |   Data   |   Data   |   Data   |   Data   |
--------------------------------------------------------
|   Data   |   Data   |   Data   |   Data   |   Data   |
--------------------------------------------------------
|   Data   |   Data   |   Data   |   Data   |   Data   |
--------------------------------------------------------
|   Data   |   Data   |   Data   |   Data   |   Data   |
--------------------------------------------------------

然后我点击一个新的工作表,我应该会自动出现以下内容:

__________________________________
| Header 1 | Header 4 | Header 5 |
----------------------------------
|   Data   |   Data   |   Data   |
----------------------------------
|   Data   |   Data   |   Data   |
----------------------------------
|   Data   |   Data   |   Data   |
----------------------------------
|   Data   |   Data   |   Data   |
----------------------------------
|   Data   |   Data   |   Data   |
----------------------------------

关于我应该如何实现这一目标的任何指示?我想避免创建宏,因为我不熟悉 VB 编程。但如果这是唯一的选择,我愿意探索它。

4

2 回答 2

1

您将不得不使用宏。在ThisWorkbook(见快照)中,粘贴此代码

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Dim ws As Worksheet

    '~~> This is where you set the input sheet
    Set ws = Sheet1

    '~~> the range that you need to replicate
    ws.Range("A1:A5,D1:E5").Copy

    '~~> I am doing paste special values
    '~~> Change it to `xlPasteAll` if you need formats to be copied as well
    Sh.Range("A1").PasteSpecial xlPasteValues

    Application.CutCopyMode = False
End Sub

快照

假设您在 Sheet1 中有这些数据。

enter image description here

现在每次添加工作表时,数据都会被复制到新工作表中。

enter image description here

The Code has to be pasted here

enter image description here

NOTE:

The biggest drawback of this method is that you will loose your undo list.

于 2012-08-04T18:00:49.893 回答
0

If you want to avoid macros you will find a solution in the combination of Index, Match and Row functions.

=Match(ColumHeader2,ColumHeaders1,0)

will give you the column in the Sheet1

=Index(Sheet1!A1,Row(Sheet2!CellRef),Match(ColumHeader2,ColumHeaders1,0))

gives you the value to be transposed to Sheet 2

CellRef = address of the cell where you want the data to be transposed.

The formula in sheet2!A2 then looks like:

=INDEX(Sheet1!$A$1:$F$11,ROW(Sheet2!A2),MATCH(A$1,Sheet1!$A$1:$F$1;0))

Sheet1!$A$1:$F$11 = data range including headers in sheet1

Sheet1!$A$1:$F$1 = headers range in sheet1

Just copy the formula to all cells in sheet2 that might contain transposed values.

于 2012-08-05T11:43:45.047 回答