简体   繁体   English

将一列中的 ID(每个单元格以逗号分隔的 ID 字符串)匹配到另一个工作表,将相关值拉过来并应用超链接

[英]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:这是我要实现的顺序:

  1. De-concatenate IDs in Sheet 1 into separate cells将工作表 1 中的 ID 拆分为单独的单元格
  2. Match each of the de-concatenated IDs to its row in Sheet 2, copy over and add values from column 6 and 7 to Sheet 1's respective cell.将每个分离的 ID 与其在工作表 2 中的行匹配,复制第 6 列和第 7 列的值并将其添加到工作表 1 的相应单元格中。
  3. Apply a hyperlink to the final cell.将超链接应用到最后一个单元格。

For example, here's what a row in Sheet 1 & 2 currently look like:例如,这里是工作表 1 和 2 中的一行当前的样子:

Sheet 1第 1 张

ID ID
123456, 789123 123456、789123

Sheet 2工作表 2

ID ID Status地位 Class Class
123456 123456 In Progress进行中 A一种
789123 789123 Done完毕 B

And here's what I'd like the output to look for Sheet 1 when the macro runs:这是我希望 output 在宏运行时查找工作表 1 的内容:

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.

相关问题 在 Excel 中搜索带有逗号分隔值字符串的单元格 - Searching a cell with a string of comma delimited values in Excel 如何将单元格的分隔字符串与Excel中另一个工作表的内容匹配 - How to match a cell's delimited string with content from another sheet in Excel 基于一组ID对另一个工作表的值求和 - Sum values from another sheet based on a set of IDs 匹配并将单元格值从一张纸替换到另一张纸 - match and replace cell values from 1 sheet to another 如何在Excel中的相同/单个单元格中将逗号分隔的值替换为逗号分隔的ID? - How to replace comma separated values to comma separated ids in the same/individual cell in excel? 如果其他两列的值匹配,则将值从一张工作表中的单元格复制到另一张工作表中最后使用的列中的单元格 - Copy value from a cell in one sheet to a cell in the last used Column in another sheet, if values in two other Columns' values match EPPlus超链接到另一个工作表中的单元格 - EPPlus Hyperlink to cell in another sheet 超链接到Excel中另一个工作表上的单元格 - Hyperlink to a cell on another sheet in Excel VBA 将工作表名称拉入分隔字符串 - VBA Pull Work Sheet Names into a Delimited String 为另一个列中可能重复的值分配唯一ID(Excel) - Assigning Unique IDs to Possibly Repeating values in another column (Excel)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM