简体   繁体   English

如何通过使用VBA映射将数据从一张纸复制到另一张纸

[英]How copy data from one sheet to another by mapping Using VBA

I have two sheets. 我有两张。 Sheet 1 is Mapping of column and Sheet 2 is Data. 工作表1是列的映射,工作表2是数据。 I wants to copy data from sheet 2 to new sheet 3 by using Sheet 1. sheet 1,Sheet 2 is shown below 我想通过使用工作表1将数据从工作表2复制到新工作表3。工作表1,工作表2如下所示

Sheet1:列映射 工作表2:具有文件名和工作表名的数据 工作表3:期望产出

Code: 码:

Sub ModdedMap()

    Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
    Dim HeadersOne As Range, HeadersTwo As Range
    Dim hCell As Range


    With ThisWorkbook
        Set Sh1 = .Sheets("Sheet 1") 'Modify as necessary.
        Set Sh2 = .Sheets("Sheet 2") 'Modify as necessary.
       Set Sh3 = .Sheets("Sheet3") 'Modify as necessary.
    End With

    Set HeadersOne = Sh3.Range("P2:P" & Sh3.Range("Q" & Rows.Count).End(xlUp).Row)

    Application.ScreenUpdating = False

    For Each hCell In HeadersOne

        SCol = GetColMatched(Sh1, hCell.Value)
        TCol = GetColMatched(Sh2, hCell.Offset(0, 1).Value)
        LRow = GetLastRowMatched(Sh1, hCell.Value)

        For Iter = 2 To LRow
            Sh2.Cells(Iter, TCol).Value = Sh1.Cells(Iter, SCol).Value
        Next Iter

    Next hCell

    Application.ScreenUpdating = True
    ActiveWorkbook.Sheets(3).Activate

End Sub

Function GetLastRowMatched(Sh As Worksheet, Header As String) As Long
    ColIndex = Application.Match(Header, Sh.Rows(1), 0)
' On Error Resume Next
    GetLastRowMatched = Sh.Cells(Rows.Count, ColIndex).End(xlUp).Row
End Function

Function GetColMatched(Sh As Worksheet, Header As String) As Long
    ColIndex = Application.Match(Header, Sh.Rows(1), 0)
    'On Error Resume Next
    GetColMatched = ColIndex
    ' On Error Resume Next
End Function

The Function getAlteranteHeaderName(value As String) will lookup the new header value. Function getAlteranteHeaderName(value As String)将查找新的标头值。 Sub CopyDataRemapHeader() is a minimalistic example of how you can copy a range from Sheet 2 to Sheet3 and then if an alternate header row name exists change it. Sub CopyDataRemapHeader()是一个简单示例,说明如何将范围从Sheet 2复制到Sheet3 ,然后如果存在备用标头行名称,请对其进行更改。

Sub CopyDataRemapHeader()
    Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
    Dim HeadersRow As Range, hCell As Range
    Dim newName As String

    Set Sh1 = Sheets("Sheet 1")
    Set Sh2 = Sheets("Sheet 2")
    Set Sh3 = Sheets("Sheet3")

    With Sh3
        Sh2.Range("A1").CurrentRegion.Copy .Range("A1")
        Set HeadersRow = Range(.Range("A1"), .Cells(1, Columns.Count).End(xlToLeft))
        For Each hCell In HeadersRow
            newName = getAlteranteHeaderName(hCell.Text)
            If Len(newName) Then hCell.value = newName
        Next hCell

    End With


End Sub

Function getAlteranteHeaderName(value As String) As String
    Dim rOld As Range, rNew As Range
    With Worksheets("Sheet 1")
        Set rNew = Intersect(.Range("A:A"), .UsedRange)
        Set rOld = Intersect(.Range("B:B"), .UsedRange)
        On Error Resume Next
        getAlteranteHeaderName = WorksheetFunction.Index(rNew, WorksheetFunction.Match(value, rOld, 0))
        On Error GoTo 0
    End With
End Function

Here is a screenshot of the output. 这是输出的屏幕截图。

在此处输入图片说明

Here is my Test Stub if you would like to download it. 如果您想下载它,这是我的测试存根

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用vba将多列数据从一张工作表复制并粘贴到另一张工作表 - How to copy and paste multiple column data from one sheet to another sheet using vba 使用vba以相反的顺序将数据从一张纸复制到另一张纸 - Copy data from one sheet to another in reverse order using vba 将数据从一张纸复制到另一张纸以使用 vba 保留日志 - Copy data from one sheet to another to keep a log using vba 使用 VBA 将数据从一张纸复制到另一张纸 - Copy data from one sheet to another using VBA Excel 使用 VBA 将单元格数据从一张纸复制到另一张纸 - Excel Copy Cell Data from one sheet to another using VBA VBA-将数据从一张纸复制到另一张纸 - VBA - Copy data from one sheet to another VBA将数据从一个工作表复制到另一个工作表 - VBA Copy data from one sheet to another 使用excel VBA将数据从一张纸复制并粘贴到另一张纸,然后从第二张纸复制到第三张纸 - Copy and paste data from one a sheet to another sheet, and from second sheet to third using excel VBA 如何使用 VBA 将遵循特定字符串的动态数据范围从一张纸复制到另一张纸? - How do I copy a dynamic range of data that follows a specific string from one sheet to another using VBA? 如何从一个工作表中复制列数据,然后将其复制到VBA Excel中的另一工作表 - How to copy column data from one sheet and then copy that to another sheet in vba excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM