Excel 查找匹配引用并复制到图纸

Excel 查找匹配引用并复制到图纸,excel,vba,copy,find,excel-2010,Excel,Vba,Copy,Find,Excel 2010,我有一些VBA的经验,我可以阅读和理解代码,但在找到正确的代码时遇到问题 现在,我有一个userform,用户在其中输入他的ID,excel将打开数据库,搜索并返回找到的ID旁边单元格的结果。结果将返回并覆盖标签1和标签2。当用户单击“下一步”或“上一步”按钮时,下一步或上一步结果将覆盖这两个标签 我现在拥有的代码允许我搜索找到的ID的位置,并以($2、$3、$4、$6)等格式输出位置。问题是,我不确定什么是正确的功能,可以将其分解为“下一步”或“上一步”按钮可以引用的单个范围 添加了我的代码

我有一些VBA的经验,我可以阅读和理解代码,但在找到正确的代码时遇到问题

现在,我有一个userform,用户在其中输入他的ID,excel将打开数据库,搜索并返回找到的ID旁边单元格的结果。结果将返回并覆盖标签1和标签2。当用户单击“下一步”或“上一步”按钮时,下一步或上一步结果将覆盖这两个标签

我现在拥有的代码允许我搜索找到的ID的位置,并以($2、$3、$4、$6)等格式输出位置。问题是,我不确定什么是正确的功能,可以将其分解为“下一步”或“上一步”按钮可以引用的单个范围

添加了我的代码

Dim cell As Range
Dim bcell As Range
Dim foundat As String
Dim oRange As Range
Dim userid As String
Dim x As Long
Dim y As Long
Dim Prob As String
Dim ws As Worksheet

Set ws = Worksheets("OFI")
Set oRange = ws.Columns(1)
userid = txt_user.Text


Set cell = oRange.Find(what:=userid, after:=Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)

If Not cell Is Nothing Then
    Set bcell = cell
    foundat = cell.Address
    Do
        Set cell = oRange.FindNext(after:=cell)

        If Not cell Is Nothing Then
            If cell.Address = bcell.Address Then Exit Do
            foundat = foundat & ", " & cell.Address
    Else
        Exit Do
    End If
Loop
Else
    msgbox userid & "not found"
    Exit Sub
End If

capproblem_output.Caption = foundat


Exit Sub

您需要添加两个名为
cmdNext
cmdPrev
的命令按钮,并使用名为
capproblem\u output2
的标签来运行以下代码。将代码复制到userform代码部分

Public foundat As String

Private Sub cmdNext_Click()
    capproblem_output.Caption = ActiveCell.Offset(1, 1)
    capproblem_output2.Caption = ActiveCell.Offset(1, 1)
    ActiveCell.Offset(1, 0).Select
End Sub

Private Sub cmdPrev_Click()
    capproblem_output.Caption = ActiveCell.Offset(-1, 1)
    capproblem_output2.Caption = ActiveCell.Offset(-1, 1)
    ActiveCell.Offset(-1, 0).Select
End Sub

Private Sub CommandButton1_Click()
    Main
End Sub


Sub Main()
    Dim cell As Range
    Dim bcell As Range

    Dim oRange As Range
    Dim userid As String
    Dim x As Long
    Dim y As Long
    Dim Prob As String
    Dim ws As Worksheet

    Set ws = Worksheets("OFI")
    Set oRange = ws.Columns(1)
    userid = UserForm1.txt_user.Text


    Set cell = oRange.Find(what:=userid, after:=Range("A1"), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)

    If Not cell Is Nothing Then
        Set bcell = cell
        foundat = cell.Address
        Do
            Set cell = oRange.FindNext(after:=cell)

            If Not cell Is Nothing Then
                If cell.Address = bcell.Address Then Exit Do
                foundat = foundat & ", " & cell.Address
        Else
            Exit Do
        End If
    Loop
    Else
        MsgBox userid & "not found"
        Exit Sub
    End If

    capproblem_output.Caption = Range(foundat).Offset(0, 1)
    capproblem_output2.Caption = Range(foundat).Offset(0, 1)
End Sub

哎呀,完全忘了回答这个问题。刚开始理解它有困难,花了一段时间来处理它。然后我忘了这件事。谢谢你的帮助!