如何根据单元格值将数据复制到其他工作表

马特

我有一个数据表,其中显示了姓名、个人号码、电子邮件等。我有大约 500 行个人数据,需要将数据分隔在不同的工作表中,并按姓名排序。我已经将人员数据和工作表颜色编码到他们的数据应该去的地方。

我制作了一个 vba,可以使用给定的 500 个名称制作工作表,但不知道如何根据带有名称的单元格值将数据复制到正确的工作表。

我只知道如何复制:

Sheets("Sheet1").Range("A2:A15").Copy Destination:=Sheets("Susanne Koch Jensen").Range("A1")

但如果我必须搬家 500 人,那将需要很长时间。

链接到表格颜色编码的图像

里卡多·迪亚兹

这里有三个替代方案来说明如何解决它

首选的是“可打印”表格中的查找公式,但正如您所说,您正在学习,我编写了其他选项。

阅读每一行中的注释,调整参数,然后按代码F8查看每一行中发生的情况。测试所有三个Public程序。

对于选项 1,设置一个这样的工作表Printable

一般设置

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

查找公式


以下代码复制module

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

让我知道它是否有效

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将匹配值的 Excel 单元格复制到其他工作表?

如何根据相同的主题 ID 将单元格的值复制到其他行?

如何根据其他两列的值将单元格的值复制到其他行?

根据其他匹配的单元格将值复制到一个单元格中

根据单元格值将行复制到工作表底部并按升序排序

如何根据条件将单元格值复制到另一个工作表(Apps 脚本)

在多个单元格中查找重复的数据,然后将所有数据复制到其他Excel工作表中

根据单元格值自动将行复制到另一个工作表

根据ID匹配将单元格数据更新/复制到另一个工作表

将活动单元格的值复制到其他/非活动表中-如何确定要使用的目标/目标表

将单元格值复制到新工作表中,但不复制格式

如果单元格包含提及值,如何将一张工作表数据复制到另一张工作表?

根据单元格值将特定数据(不是整行!)复制到另一张表

如何根据值excel VBA将多行复制到特定单元格中的另一个工作表

将最后一个值复制到其他单元格,直到看到填充的单元格

excel根据单元格值将行复制到另一张表

将值从工作表中的单元格复制到一系列单元格中

如果列a =“x”,excel将单元格值复制到新工作表

如何将基于多个单元格条件的值复制到另一个工作表

如何将单元格值复制到php中csv文件的其他空单元格中?

VBA:根据单元格值将Excel行移动和删除到其他工作表

如果工作表中的 3 列值相同,如何将单元格内容从多行复制到一个单元格中?

如何将附加值复制到 Excel 工作表中的单元格

Google Sheets - 如何将单元格复制到新工作表并保持参考

根据Google工作表中的单元格值将行复制到另一个工作表

工作表脚本:激活按钮,将数据从单元格复制到工作表,然后清除数据范围

如何在不删除工作表2上的数据的情况下将单元格从工作表1复制到工作表2

如果单元格包含X,则将行复制到其他工作表,然后删除行。

遍历范围,如果Value =“ x”,则将值从其右侧的单元格8列复制到其他工作表