【问题标题】:Loop through a column till last non empty row to find matching data through a userform VBA遍历一列直到最后一个非空行以通过用户窗体 VBA 查找匹配数据
【发布时间】:2021-04-05 01:11:42
【问题描述】:

我被这个小问题困住了。这是我的 Excel 数据的样子:

我有一个用户窗体,它要求输入“色调”、“孟塞尔值”和“孟塞尔色度”值,它应该在 D 列(颜色名称)中给出值作为消息框中的输出。我试图循环到最后一个非空行并检查 A、B、C 列是否匹配数据,然后给出相应的第 4 列作为输出。

这是我的 VBA 代码:

Sub Result()

Dim r As Range
Dim LastRow As Long

SheetName = frmForm.cmbSheet
Hue = frmForm.txtHue
Value2 = frmForm.txtValue
Chroma = frmForm.txtChroma

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(SheetName)

LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    For Each r In ws.Range("A2:A" & LastRow)
        If r = Hue & r.Offset(0, 1) = Value2 & r.Offset(0, 2) = Chroma Then
        MsgBox r.Offset(0, 3).Value
        Exit Sub
        End If
    Next r

MsgBox ("No color found for these Munsell color values!")

End Sub

这是基本的用户窗体:

每次运行代码时,我都会收到“未找到颜色...”的消息。即使我将完全匹配的值放在用户窗体中。

【问题讨论】:

    标签: excel vba loops lookup userform


    【解决方案1】:

    改变

    If r = Hue & r.Offset(0, 1) = Value2 & r.Offset(0, 2) = Chroma Then
    

    If r = Hue and r.Offset(0, 1) = Value2 and r.Offset(0, 2) = Chroma Then
    

    【讨论】:

    • 非常感谢,这么长时间卡在这个问题上我觉得很傻 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多