VBA - 如何从用户窗体上的命令按钮正确查找、复制和粘贴搜索?
VBA -How to properly Find, Copy and Paste a Search from a Command Button on a Userform?
我需要一些指导,以了解我正在使用的 VBA 代码可能出了什么问题。我有
已经为此工作了几个小时,但似乎无法弄清楚发生了什么。从右边开始
现在当我 运行 代码时,没有任何反应,没有错误,什么都没有……
我正在使用的很多代码都是从这个 post:
如有任何帮助,我们将不胜感激。
我想做什么:
我正在尝试在数据库中搜索用户窗体文本框中的值,同时按下
命令按钮。换句话说,我告诉 vba 搜索数据行并匹配
文本框中的值,然后如果有匹配项,则将匹配项复制到新的 sheet。
进程:
- 用户窗体代码
上的“运行检查”按钮有一个点击事件
- 在每次 运行(每次点击)之前清除目标 sheet 区域。
- 根据文本框值设置一个数组,其中每个值的索引与要搜索的列号相匹配(虽然我只在数组中搜索 2 个值,但我想稍后在此基础上构建一个数组有意义)
- 过滤搜索状态列中状态为“打开”的行
- 一次一行,将相应列的值与匹配它的数组索引进行比较
- 如果找到匹配项,“匹配”变量将设置为 true
- 循环遍历数组中其余的文本框值,如果其中任何一个不匹配,“匹配”变量将设置为 false,并在文本框失败时中断循环。
- 如果在“已搜索”作品的 ROW 循环结束时“匹配”为真sheet,将循环遍历第 1 至 8 列,设置已搜索的值 sheet 到目标 Sheet。
- 嵌套行完成循环
帮助上下文的屏幕截图
代码已更新<-工作:
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
未测试:
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
我需要一些指导,以了解我正在使用的 VBA 代码可能出了什么问题。我有 已经为此工作了几个小时,但似乎无法弄清楚发生了什么。从右边开始 现在当我 运行 代码时,没有任何反应,没有错误,什么都没有……
我正在使用的很多代码都是从这个 post:
如有任何帮助,我们将不胜感激。
我想做什么:
我正在尝试在数据库中搜索用户窗体文本框中的值,同时按下 命令按钮。换句话说,我告诉 vba 搜索数据行并匹配 文本框中的值,然后如果有匹配项,则将匹配项复制到新的 sheet。
进程:
- 用户窗体代码 上的“运行检查”按钮有一个点击事件
- 在每次 运行(每次点击)之前清除目标 sheet 区域。
- 根据文本框值设置一个数组,其中每个值的索引与要搜索的列号相匹配(虽然我只在数组中搜索 2 个值,但我想稍后在此基础上构建一个数组有意义)
- 过滤搜索状态列中状态为“打开”的行
- 一次一行,将相应列的值与匹配它的数组索引进行比较
- 如果找到匹配项,“匹配”变量将设置为 true
- 循环遍历数组中其余的文本框值,如果其中任何一个不匹配,“匹配”变量将设置为 false,并在文本框失败时中断循环。
- 如果在“已搜索”作品的 ROW 循环结束时“匹配”为真sheet,将循环遍历第 1 至 8 列,设置已搜索的值 sheet 到目标 Sheet。
- 嵌套行完成循环
帮助上下文的屏幕截图
代码已更新<-工作:
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
未测试:
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