宏/ vba将相应的行复制到相应的工作簿中

时间:2014-08-08 16:02:42

标签: vba excel-vba excel

我正在尝试将行复制到多个工作表中。

在管理员工工作产品时,我希望员工的姓名(A栏)及其信息(B,C,D,E,F和G栏)转到每位员工的相应工作表。

理想情况下,第一个“标签”是所有信息。工作表2将是员工2,其中信息行自动填充,依此类推。

我试过的代码只做了一行。

1 个答案:

答案 0 :(得分:0)

我不知道你为什么要这样做。只有一行的多张纸似乎是浪费 无论如何,这不是自动更新,多次运行,你会得到每个员工多个页面。如果您想要更多功能,您应该根据需要提供更多信息。您提到的那些你写的代码应该分享。

Const EMPLOYEE_NAME_COL As String = "A"
Const LAST_COL As String = "G"
Const MY_BOOK_NAME As String = "name your workbook"
Const INFO_SHEET_INDEX As Integer = 1

Function GetNewSheetAtEnd(book As Workbook) As Worksheet

    Dim sheets as Collection
    Set sheets = book.sheets

    Set GetNewSheetAtEnd = sheets.add(after:=sheets(sheets.count))

End Function

' Modify this Sub to copy however you want it to
Sub CopyRangeToSheet(rng As Range, sheet As WorkSheet)

    rng.Copy sheet.Range("A1")

End Sub

Sub CopyEmployeesToNewSheets()

    Dim info_sheet as Worksheet
    Set info_sheet = Workbooks(MY_BOOK_NAME).Sheets(INFO_SHEET_INDEX)

    Dim employees_range As Range
    ' Assumes you have a header.  You will probably need to modify this.
    Set employees_range = info_sheet.Range("A2:G" & info_sheet.UsedRange.Rows.Count)

    Dim employee_row As Range
    For Each employee_row In employees_range.Rows

        CopyRangeToSheet employee_row, GetNewSheetAtEnd()

    Next employee_row

End Sub