【问题标题】:Excel VBA Concatenate Unique Values LookupExcel VBA 连接唯一值查找
【发布时间】:2016-07-12 16:18:57
【问题描述】:

早上好!我正在尝试创建一个 VBA 函数,并感谢您提供的任何帮助,以使我走上正轨。简而言之,对于 Worksheet Exams 的 A 列中的每个值,我需要连接 Worksheet Findings 的 B 列中的所有唯一值,其中 Worksheet Exams 的 A 列 = Worksheet Findings 的 A 列。我正在努力从哪里开始,似乎找不到任何好的指导。在此先感谢您的帮助。非常感谢。

从这里开始了解连接...我知道 & ExamID 部分是错误的,但我不确定我需要在那里与该 RX721502 的下一个实例连接的代码:

Dim ExamID As Range
Dim strConcat As String
Dim i As Integer
i = 2

Do While Cells(i, 1).Value <> ""
    For Each ExamID In Range("A2:A10000")
        If InStr(ExamID.Value, "RX721502") > 0 Then
        Cells(i, 18).Value = ActiveCell.Offset(0, 10) & ", " & ExamID
        End If
    Next ExamID
    Cells(2, 18) = Trim(Cells(2, 18))
i = i + 1
Loop

G

【问题讨论】:

  • 是的,我知道...我什至无法让我该死的串联工作。我已经开始: 将 conOG 作为字符串 将 SourceExams 作为工作表 将 SourceFindings 作为工作表设置 SourceExams = Sheets("Source-Exams") Set SourceFindings = Sheets("Source-Findings") For i = 1 To N + 3 conOG = conOG & Cells(3, i) 下一个 i
  • 你的叙述是关于“工作表状态”“工作表城市”,而你的“代码”显示@ 987654322@ 和Sheets("Source-Findings")。你可能想更新你的代码并澄清一下。
  • 调整...谢谢...

标签: excel vba macros


【解决方案1】:

试试下面的代码,它将城市的Concatenate字符串放入工作表SourceExams,第2列。

Sub use_VLookup()


Dim conOG                               As String
Dim SourceExams                         As Worksheet
Dim SourceFindings                      As Worksheet
Dim lastrow, lastrow2                   As Long
Dim rowfound                            As Long
Dim Vlookup_result                      As Variant

Set SourceExams = ActiveWorkbook.Sheets("Source-Exams")
Set SourceFindings = ActiveWorkbook.Sheets("Source-Findings")

lastrow = SourceExams.UsedRange.Rows.count
lastrow2 = SourceFindings.UsedRange.Rows.count

For i = 2 To lastrow
    j = 2
    While j <= lastrow2
        ' search Worksheet Cities workcheet for match on Column A, and return the value in column B
        Vlookup_result = Application.VLookup(SourceExams.Cells(i, 1), SourceFindings.Range(SourceFindings.Cells(j, 1), SourceFindings.Cells(lastrow2, 2)), 2, False)
        If IsError(Vlookup_result) Then
            ' do nothing , you can add erro handling, but I don't think it's necesary
        Else
            conOG = conOG & ", " & Application.WorksheetFunction.VLookup(SourceExams.Cells(i, 1), SourceFindings.Range(SourceFindings.Cells(j, 1), SourceFindings.Cells(lastrow2, 2)), 2, False)
            rowfound = Application.WorksheetFunction.Match(SourceExams.Cells(i, 1), SourceFindings.Range(SourceFindings.Cells(j, 1), SourceFindings.Cells(lastrow2, 1)), 0)
        End If

        j = j + rowfound
        ' if first found go to row 3
        If j <= 2 Then j = 3
    Wend
    SourceExams.Cells(i, 2) = conOG
    conOG = ""
Next i

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2017-06-26
    • 2018-04-05
    • 2017-03-13
    • 2014-06-08
    相关资源
    最近更新 更多