Excel 如何根据单元格值将数据复制到其他图纸

Excel 如何根据单元格值将数据复制到其他图纸,excel,vba,Excel,Vba,我有一个数据表,其中有姓名、个人号码、电子邮件等。我有大约500行个人数据,需要在不同的表中分开数据,按姓名排序。我已对人员数据和表格进行了颜色编码,以确定其数据的去向 我制作了一个vba,它可以生成具有给定500个名称的工作表,但不知道如何根据单元格值及其名称将数据复制到正确的工作表 我只知道如何使用以下工具进行复制: Sheets("Sheet1").Range("A2:A15").Copy Destination:=Sheets("Susanne Koch Jensen").Range("

我有一个数据表,其中有姓名、个人号码、电子邮件等。我有大约500行个人数据,需要在不同的表中分开数据,按姓名排序。我已对人员数据和表格进行了颜色编码,以确定其数据的去向

我制作了一个vba,它可以生成具有给定500个名称的工作表,但不知道如何根据单元格值及其名称将数据复制到正确的工作表

我只知道如何使用以下工具进行复制:

Sheets("Sheet1").Range("A2:A15").Copy Destination:=Sheets("Susanne Koch Jensen").Range("A1")
但如果我要搬500人的话,那要花很长时间


以下是三个备选方案,以说明如何解决此问题

首选的是“可打印”表单中的查找公式,但正如您所说的,我对其他选项进行了编码

阅读每一行中的注释,调整参数,然后按F8查看代码,这样您就可以看到每一行中发生了什么。测试所有三个
Public
程序

对于选项1,设置一个名为
可打印的
的工作表,如下所示:

查找公式:
=INDEX(Sheet1!$A2:$C2;;$B$1)
指定$A2:$C2,其中A到C是源工作表中包含数据的列(可能是500列),2是与名称对应的行(如果向下复制,则参考其他行)


将以下代码复制到
模块中

Option Explicit

' OPTION 1
' Have a printable sheet with lookup formulas and print that sheet
Public Sub LookupAndPrint()

    Dim sourceSheet As Worksheet
    Dim printableSheet As Worksheet

    Dim firstColumn As Long
    Dim lastColumn As Long
    Dim nameRow As Long
    Dim counter As Long

    Dim sourcePath As String
    Dim fileName As String

    ' Adjust the following parameters
    Set sourceSheet = ThisWorkbook.Worksheets("Sheet1")
    Set printableSheet = ThisWorkbook.Worksheets("Printable")

    firstColumn = 2 ' = B
    nameRow = 2 ' Relative to sheet

    ' Get the last column with data
    lastColumn = sourceSheet.Cells(nameRow, sourceSheet.Columns.Count).End(xlToLeft).Column

    ' Get current file path
    sourcePath = ThisWorkbook.path

    For counter = firstColumn To lastColumn

        ' Set the lookup column's number
        printableSheet.Range("B1").Value = counter

        ' Set the file name
        fileName = printableSheet.Range("B3").Value
        fileName = Replace(fileName, ".", "_")
        fileName = Replace(fileName, " ", "")

        ' Export the sheet
        exportToPDF printableSheet, sourcePath, fileName

    Next counter

End Sub

Private Sub exportToPDF(ByVal sourceSheet As Worksheet, ByVal path As String, ByVal fileName As String)

    Dim cleanFileName As String
    Dim fullPath As String

    cleanFileName = Replace(fileName, ".", "_")
    cleanFileName = Replace(cleanFileName, " ", "")

    fullPath = path & "\" & cleanFileName

    sourceSheet.ExportAsFixedFormat xlTypePDF, fullPath

End Sub


' OPTION 2
' You can hide other columns and export to PDF
Public Sub HideColumnsAndPrintToPDF()

    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim evalRange As Range
    Dim sourceColumn As Range

    Dim firstRow As Long
    Dim lastRow As Long
    Dim firstColumn As Long
    Dim lastColumn As Long
    Dim nameRow As Long

    Dim sourcePath As String

    ' Adjust the following parameters
    Set sourceSheet = ThisWorkbook.Worksheets("Sheet1")

    firstRow = 2
    lastRow = 15
    firstColumn = 2 ' = B
    nameRow = 1 ' Relative to firstRow



    ' Get the last column with data
    lastColumn = sourceSheet.Cells(firstRow, sourceSheet.Columns.Count).End(xlToLeft).Column

    ' Set the evaluated range
    Set evalRange = sourceSheet.Range(sourceSheet.Cells(firstRow, firstColumn), sourceSheet.Cells(lastRow, lastColumn))

    ' Get current file path
    sourcePath = ThisWorkbook.path

    ' Loop through each column in range
    For Each sourceColumn In evalRange.Columns

        ' Hide other columns
        hideOtherColumns sourceColumn.Column, evalRange

        ' Export to pdf
        exportToPDF sourceSheet, sourcePath, sourceColumn.Cells(nameRow).Value

    Next sourceColumn

End Sub

Private Sub hideOtherColumns(ByVal currentColumn As Long, ByVal evalRange As Range)

    Dim evalColumn As Range

    For Each evalColumn In evalRange.Columns

        evalColumn.EntireColumn.Hidden = (evalColumn.Column <> currentColumn)

    Next evalColumn

End Sub



' OPTION 3
' If you plan to copy data to sheets
Public Sub CopyDataToSheets()

    Dim sourceSheet As Worksheet
    Dim targetSheet As Worksheet
    Dim evalRange As Range
    Dim sourceColumn As Range

    Dim firstRow As Long
    Dim lastRow As Long
    Dim firstColumn As Long
    Dim lastColumn As Long
    Dim nameRow As Long

    Dim sourcePath As String

    ' Adjust the following parameters
    Set sourceSheet = ThisWorkbook.Worksheets("Sheet1")

    firstRow = 2
    lastRow = 15
    firstColumn = 2 ' = B
    nameRow = 1 ' Relative to firstRow



    ' Get the last column with data
    lastColumn = sourceSheet.Cells(firstRow, sourceSheet.Columns.Count).End(xlToLeft).Column

    ' Set the evaluated range
    Set evalRange = sourceSheet.Range(sourceSheet.Cells(firstRow, firstColumn), sourceSheet.Cells(lastRow, lastColumn))

    ' Get current file path
    sourcePath = ThisWorkbook.path

    ' Loop through each column in range
    For Each sourceColumn In evalRange.Columns

        ' Get the sheet based on the name
        Set targetSheet = getSheet(sourceColumn.Cells(nameRow).Value)

        ' Check that a sheet was found
        If Not targetSheet Is Nothing Then

            ' Copy data to sheet
            sourceColumn.Copy Destination:=targetSheet.Range("A1")

            ' Export to pdf
            exportToPDF targetSheet, sourcePath, sourceColumn.Cells(nameRow).Value

        End If

    Next sourceColumn

End Sub

Private Function getSheet(ByVal sheetName As String) As Worksheet

    Dim sheet As Worksheet

    For Each sheet In ThisWorkbook.Worksheets
        ' Use this if names are approximate, or: sheet.name = sheetName if names should be equal
        If InStr(LCase$(sheet.Name), LCase$(sheetName)) > 0 Then ' If sheet.name = sheetName then
            Set getSheet = sheet
        End If
    Next sheet

End Function
选项显式
“选择1
'拥有一张带有查找公式的可打印表格,然后打印该表格
公共子查找和打印()
将源表设置为工作表
将可打印工作表设置为工作表
第一列的长度相同
将最后一列变长
暗淡的名字行一样长
昏暗的柜台一样长
将源路径设置为字符串
将文件名设置为字符串
'调整以下参数
设置sourceSheet=ThisWorkbook.Worksheets(“Sheet1”)
设置可打印工作表=此工作簿。工作表(“可打印”)
第一列=2'=B
相对于图纸,nameRow=2'
'获取包含数据的最后一列
lastColumn=sourceSheet.Cells(nameRow,sourceSheet.Columns.Count)。End(xlToLeft)。Column
'获取当前文件路径
sourcePath=ThisWorkbook.path
对于计数器=从第一列到最后一列
'设置查找列的编号
可打印页。范围(“B1”)。值=计数器
'设置文件名
fileName=printableSheet.Range(“B3”).值
fileName=Replace(文件名,“.”,“uu”)
fileName=Replace(文件名“,”)
'导出工作表
exportToPDF可打印表格,源路径,文件名
下一个柜台
端接头
私有子exportToPDF(ByVal源表作为工作表,ByVal路径作为字符串,ByVal文件名作为字符串)
将文件名设置为字符串
将完整路径设置为字符串
cleanFileName=Replace(文件名“.”,“389;”)
cleanFileName=Replace(cleanFileName,“,”)
fullPath=path&“\”文件名
sourceSheet.ExportAsFixedFormat xlTypePDF,完整路径
端接头
“选择2
'您可以隐藏其他列并导出为PDF
公共子HideColumnsAndPrintToPDF()
将源表设置为工作表
将目标工作表变暗为工作表
Dim evalRange As范围
将sourceColumn设置为范围
第一排一样长
最后一排一样长
第一列的长度相同
将最后一列变长
暗淡的名字行一样长
将源路径设置为字符串
'调整以下参数
设置sourceSheet=ThisWorkbook.Worksheets(“Sheet1”)
第一行=2
最后一行=15
第一列=2'=B
相对于第一行,nameRow=1
'获取包含数据的最后一列
lastColumn=sourceSheet.Cells(第一行,sourceSheet.Columns.Count)。End(xlToLeft)。Column
'设置评估范围
Set evalRange=sourceSheet.Range(sourceSheet.Cells(firstRow,firstColumn),sourceSheet.Cells(lastRow,lastColumn))
'获取当前文件路径
sourcePath=ThisWorkbook.path
'循环遍历范围中的每列
对于evalRange.Columns中的每个sourceColumn
'隐藏其他列
hideOtherColumns sourceColumn.Column,evalRange
'导出为pdf
exportToPDF sourceSheet、sourcePath、sourceColumn.Cells(nameRow).Value
下一个sourceColumn
端接头
私有子隐藏其他列(ByVal currentColumn为长,ByVal evalRange为范围)
Dim evalColumn As范围
对于evalRange.Columns中的每个evalColumn
evalColumn.EntireColumn.Hidden=(evalColumn.Column currentColumn)
下一个评估柱
端接头
“备选方案3
'如果您计划将数据复制到工作表
公共子CopyDataToSheets()
将源表设置为工作表
将目标工作表变暗为工作表
Dim evalRange As范围
将sourceColumn设置为范围
第一排一样长
最后一排一样长
第一列的长度相同
将最后一列变长
暗淡的名字行一样长
将源路径设置为字符串
'调整以下参数
设置sourceSheet=ThisWorkbook.Worksheets(“Sheet1”)
第一行=2
最后一行=15
第一列=2'=B
相对于第一行,nameRow=1
'获取包含数据的最后一列
lastColumn=sourceSheet.Cells(第一行,sourceSheet.Columns.Count)。End(xlToLeft)。Column
'设置评估范围
Set evalRange=sourceSheet.Range(sourceSheet.Cells(firstRow,firstColumn),sourceSheet.Cells(lastRow,lastColumn))
'获取当前文件路径
sourcePath=ThisWorkbook.path
'循环遍历范围中的每列
对于evalRange.Columns中的每个sourceColumn
'根据名称获取工作表
Set targetSheet=getSheet(sourceColumn.Cells(nameRow.Value)
'检查是否找到了工作表
如果不是,那么targetSheet什么都不是
'将数据复制到工作表
sourceColumn.Copy Destination:=targetSheet.Range(“A1”)
'导出为pdf
exportToPDF targetSheet,sourcePath,sourceColumn.Cells(nameRow).Value
如果结束
下一个sourceColumn
端接头
私有函数getSheet(ByVal sheetName作为字符串)作为工作表
将板材调暗为W