VBA - 如何正确地查找,复制并粘贴在用户窗体一个命令按钮搜索?

问题描述 投票:0回答:1

我需要一些方向,以什么可能不对,我使用VBA代码走了。我在这已经工作了好几个小时,似乎无法弄清楚到底是怎么回事。截至目前,当我运行的代码,什么都不会发生,没有错误,没有什么...

很多我使用我从这篇文章得到了代码:Similar Use Case

任何帮助将大大赞赏。

我所试图做的事:

我想按下一个命令按钮时,搜索值的数据库中的文本框在一个窗体。换句话说,我告诉VBA通过数据行搜索和文本框的值匹配,那么如果有匹配,那场比赛复制到新的工作表。

处理:

  1. 有一个点击事件的“运行检查”按钮,在用户窗体代码
  2. 每次运行(每点击)之前清除目标片区域。
  3. 从设置文本框值,其中每个指标相匹配的列数来搜索一个数组(虽然我只搜索数组中的2倍的值,我想在此建立以后这样的阵列是有道理的)
  4. 过滤搜索只能在具有“开放”的状态,在状态栏行
  5. 在一次一行,将适当的列的值进行比较,以与其匹配的数组索引
  6. 如果发现匹配,则“匹配”变量设置为true
  7. 遍历该数组中的文本框的值的休息,如果其中任何一个不匹配,“匹配”变量设置为false,并打破循环在文本框为失败。
  8. 如果“匹配”通过“搜索”工作表的ROW是真由环路的端部,将列1至8打通环,从搜索到的片材到目标表在设置这些值。
  9. 鸟巢行结束循环

屏幕截图,以帮助上下文

Step 1

Step 2

Step 3

Step 4

更新代码< - 工作:

Private Sub run_check_but_Click()
Const COL_STATUS As Long = 4
Dim wsData As Worksheet, wsSyn As Worksheet
Dim tRow As Long, i As Long
Dim tempList(1 To 9)
Dim match As Boolean
Dim rCol As Range, c As Range

Set wsData = Sheets("Database")
Set rCol = wsData.Range(wsData.Cells(3, 4), wsData.Cells(100, 4))

'Set TargetSheet and clear the previous contents
Set wsSyn = Sheets("Syn_Calc")
wsSyn.Range("A3:G" & wsSyn.Range("A" & Rows.count).End(xlUp).row + 1).ClearContents 'changed from  to 3
tRow = 3

'Set an array of strings, based on the index matching the column to search for each
tempList(5) = curbase_box.Text       'Column "E" (5)
tempList(6) = dirquote_box.Text       'Column "F" (6) 'changed from 9 to 6

For Each c In rCol.Cells
    With c.EntireRow
        If .Cells(COL_STATUS).Value = "Open" Then

            match = False

            For i = LBound(tempList) To UBound(tempList)
                If tempList(i) <> "" Then
                    match = (.Cells(i).Text = tempList(i))
                    If Not match Then Exit For
                End If
            Next i

            If match Then
                'copy values from E-K
                wsSyn.Cells(tRow, 1).Resize(1, 7).Value = _
                     .Cells(5).Resize(1, 7).Value
                tRow = tRow + 1
            End If

        End If 'open
    End With
Next c
End Sub
excel vba find copy-paste
1个回答
1
投票

未经测试:

Private Sub run_check_but_Click()

    Const COL_STATUS As Long = 4
    Dim wsData As Worksheet, wsSyn As Worksheet
    Dim tRow As Long, i As Long
    Dim tempList(1 To 9)
    Dim match As Boolean
    Dim rCol As Range, c As Range

    Set wsData = Sheets("Database")
    Set rCol = wsData.Range(wsData.Cells(3, 4), wsData.Cells(100, 4))

    'Set TargetSheet and clear the previous contents
    Set wsSyn = Sheets("Syn_Calc")
    wsSyn.Range("A8:F" & wsSyn.Range("A" & Rows.Count).End(xlUp).Row + 1).ClearContents
    tRow = 3 '<< but you clear from row 8 down?

    'Set an array of strings, based on the index matching the column to search for each
    tempList(5) = curbase_box.Text       'Column "E" (5)
    tempList(9) = dirquote_box.Text       'Column "I" (9)

    For Each c In rCol.Cells
        With c.EntireRow
            If .Cells(COL_STATUS).Value = "Open" Then

                match = False

                For i = LBound(tempList) To UBound(tempList)
                    If tempList(i) <> "" Then
                        match = (.Cells(i).Text = tempList(i))
                        If Not match Then Exit For
                    End If
                Next i

                If match Then
                    'copy values from E-K
                    wsSyn.Cells(tRow, 1).Resize(1, 7).Value = _
                         .Cells(5).Resize(1, 7).Value
                    tRow = tRow + 1
                End If

            End If 'open
        End With
    Next c
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.