简体   繁体   English

如何根据标题复制一系列单元格以粘贴到另一个工作表并匹配标题?

[英]How can I copy a range of cells based on a Header to paste to another worksheet and match the headers?

I need a code to copy a range of cells ( H21:H38 ) from my source worksheet ( Acct Total ) to a corresponding column on my target worksheet ( COS% Tracking ) based on matching headers.我需要一个代码来根据匹配的标题将一系列单元格 ( H21:H38 ) 从我的源工作表 ( Acct Total ) 复制到我的目标工作表 ( COS% Tracking ) 上的相应列。 But the hiccup I have is that the header is in cell A6 on my source worksheet ( Acct Total ).但我遇到的问题是标题位于我的源工作表( Acct Total )上的单元格 A6 中。 I've researched it a bit and I've found this code that worked for someone else:我对它进行了一些研究,发现这段代码对其他人有用:

Sub CopyHeaders()
    Dim header As Range, headers As Range
    Set headers = Worksheets("ws1").Range("A1:Z1")

    For Each header In headers
        If GetHeaderColumn(header.Value) > 0 Then
            Range(header.Offset(1, 0), header.End(xlDown)).Copy Destination:=Worksheets("ws2").Cells(2, GetHeaderColumn(header.Value))
        End If
    Next
End Sub

Function GetHeaderColumn(header As String) As Integer
    Dim headers As Range
    Set headers = Worksheets("ws2").Range("A1:Z1")
    GetHeaderColumn = IIf(IsNumeric(Application.Match(header, headers, 0)), Application.Match(header, headers, 0), 0)
End Function

So my issue is that I don't know where to begin to edit this code to work like I need.所以我的问题是我不知道从哪里开始编辑此代码以按照我的需要工作。 This code worked by using the header above the range of cells but that won't do in my case.此代码通过使用单元格范围上方的标题工作,但在我的情况下不会这样做。 I'll attach pictures so that hopefully I'm not too vague.我会附上图片,希望我不会太含糊。

帐户总计选项卡

COS% 跟踪选项卡

Can someone help me to edit this code according to my needs?有人可以根据我的需要帮助我编辑此代码吗?

Edit: Additional Picture for the source of the dates.编辑:日期来源的附加图片。 GL Code Tab总帐代码选项卡

Look at the following construct as a starting point for a different way to solve the same problem.将以下构造视为解决同一问题的不同方法的起点。 There are descriptive variables so you have an idea of what is happening.有描述性变量,因此您可以了解正在发生的事情。

Edit: As the target sheet row 3 is locked, code has been amended to use Match function to return column number where string is found (if found).编辑:由于目标工作表第 3 行被锁定,代码已被修改为使用 Match 函数返回找到字符串的列号(如果找到)。

Essentially:本质上:

Set your source and target worksheets.设置源和目标工作表。

Set sourceWorksheet = wb.Sheets("Acct Total")
Set targetWorksheet = wb.Sheets("COS% Tracking")

Define your target value (the date you are trying to match on) and source range定义您的目标值(您尝试匹配的日期)和源范围

targetDate = Trim$(sourceWorksheet.Range("A6"))
Set sourceRange = sourceWorksheet.Range("H21:H38")

Find the column number of where value (targetDate) is present in the target sheet查找目标工作表中存在值 (targetDate) 的列号

colNum = Application.WorksheetFunction.Match(targetDate, searchRange, 0)

Add error handling in case it is not present ie if date (as string) is not found....添加错误处理以防它不存在,即如果未找到日期(作为字符串)....

ErrHand: 'code in this section.....

Set the address of where the target data will be pasted设置目标数据的粘贴地址

Set targetRange = .Range(Cells(4, colNum), Cells(21, colNum))

Set the target range to be equal to the source range.将目标范围设置为等于源范围。

targetRange.Value = sourceRange.Value

Adapt as appropriate.酌情调整。

Putting it together you getting something along the lines of the following:把它放在一起,你会得到以下内容:

Option Explicit

Public Sub copydata()

    Dim sourceRange As Range
    Dim targetDate As String
    Dim targetRange As Range
    Dim wb As Workbook
    Dim sourceWorksheet As Worksheet
    Dim targetWorksheet As Worksheet
    Dim searchRange As Range

    Set wb = ThisWorkbook
    Set sourceWorksheet = wb.Sheets("Acct Total")
    Set targetWorksheet = wb.Sheets("COS% Tracking")
    targetDate = Trim$(sourceWorksheet.Range("A6"))
    Set sourceRange = sourceWorksheet.Range("H21:H38")
    Set searchRange = targetWorksheet.Rows(3)

    On Error GoTo ErrHand

    Dim colNum As Long    
    colNum = Application.WorksheetFunction.Match(targetDate, searchRange, 0)

    With targetWorksheet
        Set targetRange = .Range(Cells(4, colNum), Cells(21, colNum))
        targetRange.Value = sourceRange.Value
    End With

ErrHand:

    If Err = 1004 Then
        MsgBox "Not found: " & targetDate
        Err.Clear
        Exit Sub
    End If

End Sub

See the following:请参阅以下内容:

Finding address of text in worksheet 在工作表中查找文本地址

Moving data between sheets 在工作表之间移动数据

暂无
暂无

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

相关问题 如果条件满足,我如何使用 VBA 代码复制和粘贴特定单元格到另一个工作表的不同区域 - How can i use VBA Code to Copy and Paste Specific Cells if Condition is Met to different areas of another worksheet Excel VBA如何将WorkSheet名称与单元格值匹配,然后复制并粘贴一系列单元格 - Excel VBA How to match WorkSheet name with Cell value, then Copy and Paste a Range of Cells 如何根据VBA中的预定义索引从另一个工作表复制和粘贴一系列单元格 - How to copy and paste a range of cells from another worksheet depending on predefined indices in VBA 如何复制没有标题的过滤范围并将其粘贴到另一张纸上? - How can I copy the filtered range without the headers and paste it into another sheet? 复制单元格范围并根据日期粘贴到另一个工作表中? - Copy cell range and paste in another worksheet based on the date? 如果在两个或多个工作表中满足条件,我如何使用 VBA 代码将特定单元格复制和粘贴到另一个工作表的不同区域 - How can i use VBA Code to Copy and Paste Specific Cells if Condition is Met in two or more worksheets to different areas of another worksheet 如何让 VBA 复制一系列单元格,等待单元格计算并粘贴到另一个范围? - How do I get VBA to Copy a range of cells, WAIT FOR THE CELLS TO CALCULATE and paste in another range? 如何通过匹配单元格值将范围复制并粘贴到另一个工作表 - How to copy and paste a range to another worksheet by matching a cell value 将范围复制到最后一行并粘贴到另一个工作表中 - Copy range to last row and paste in another worksheet 将公式复制/粘贴到另一个工作表 - copy/paste range with formula to another worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM