[英]Match IDs in a column (comma delimited string of IDs per cell) to another sheet, pull the relevant values over & apply hyperlink
Need some help with an Excel macro-- I'm currently struggling to write a macro that combines all three processes.在 Excel 宏方面需要一些帮助——我目前正在努力编写一个结合了所有三个进程的宏。 I have two sheets: Sheet 1 contains a column with multiple IDs in each cell delimited by commas (can go up to like 30 IDs in one cell), Sheet 2 contains data for each of the IDs.
我有两张工作表:工作表 1 包含一列,每个单元格中有多个 ID,用逗号分隔(go 最多可以在一个单元格中包含 30 个 ID),工作表 2 包含每个 ID 的数据。
Here's the sequence that I'm trying to achieve:这是我要实现的顺序:
For example, here's what a row in Sheet 1 & 2 currently look like:例如,这里是工作表 1 和 2 中的一行当前的样子:
Sheet 1第 1 张
ID ![]() |
---|
123456, 789123 ![]() |
Sheet 2工作表 2
ID ![]() |
Status![]() |
Class ![]() |
---|---|---|
123456 ![]() |
In Progress![]() |
A![]() |
789123 ![]() |
Done![]() |
B![]() |
And here's what I'd like the output to look for Sheet 1 when the macro runs:这是我希望 output 在宏运行时查找工作表 1 的内容:
ID ![]() |
||
---|---|---|
123456, 789123 ![]() |
123456, In Progress, A ![]() |
789123, Done, B ![]() |
My code is super off, but here's what I have:我的代码非常糟糕,但这就是我所拥有的:
Set wb = ThisWorkbook
Dim sel As Range
Set sel = Selection
Dim arr() As String
Dim cell As Range
Dim i As Long
Set wsCheck = wb.Sheets("2")
'Column N (IDs)
wb.Sheets("1").Columns("N:N").Select
For Each cell In sel
arr = Split(cell, ",")
For i = 0 To UBound(arr)
m = Application.Match("*" & arr(i) & "*", wsCheck.Columns(1), 0)
If Not IsError(m) Then
cell.Offset(0, i + 1).Value = wsCheck.Cells(m, 6).Value & wsCheck.Cells(m, 7).Value
cell.Parent.Hyperlinks.Add Anchor:=cell.Offset(0, i + 1), Address:="URL" & arr(i), TextToDisplay:=arr(i)
End If
Next i
Next cell
Try this:尝试这个:
Sub test()
Dim wb As Workbook, arr, ws As Worksheet, wsCheck As Worksheet
Dim cell As Range
Dim i As Long, v, m
Set wb = ThisWorkbook
Set ws = wb.Sheets("1")
Set wsCheck = wb.Sheets("2")
If Not TypeOf Selection Is Range Then Exit Sub 'make sure a range is selected
If Selection.Worksheet.Name <> ws.Name Then Exit Sub '...on the correct sheet
For Each cell In Selection.EntireRow.Columns("N").Cells
arr = Split(cell.Value, ",")
For i = 0 To UBound(arr)
v = CLng(Trim(arr(i))) 'remove spaces and convert to number
m = Application.Match(v, wsCheck.Columns(1), 0)
If Not IsError(m) Then
With cell.Offset(0, i + 1)
.Value = Join(Array(v, wsCheck.Cells(m, 6).Value, _
wsCheck.Cells(m, 7).Value), ",")
.Parent.Hyperlinks.Add Anchor:=.Cells(1), _
Address:="", _
SubAddress:=wsCheck.Cells(m, 1).Address(0, 0, xlA1, True), _
TextToDisplay:=.Value
End With
End If
Next i
Next cell
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.