[英]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.
我会附上图片,希望我不会太含糊。
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:请参阅以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.