[英]Searching for multiple values using VBA in Excel
I do not know if it is possible to implement this problem in VBA or it must be done with VB. 我不知道是否可以在VBA中实现此问题,或者必须使用VB完成。 Net using Visual Studio.
NET使用Visual Studio。
Problem: Excel has its search function and it is a pain if there many value available or you must find a value that far away from column A. 问题:Excel具有其搜索功能,如果有很多可用值,或者您必须找到一个与A列相距甚远的值,那将是一件痛苦的事情。
I would like to have something like this 我想要这样的东西
In which I can specify what columns I want to display by their header name. 我可以在其中指定要通过标题名称显示的列。 Rearrange the column in the way I would like to, and ability to copy and paste.
按照我想要的方式重新排列列,并具有复制和粘贴的功能。 Like datagrid in Visual basic?
像Visual Basic中的datagrid一样? Is it possible?
可能吗?
I have given below both the methods - VBA and VB.net (take your pick) :) 我在下面给出了两种方法-VBA和VB.net(请选择):)
USING VB.NET 使用VB.NET
Place a DataGridView on your VB.net Form and also place a Button. 将一个DataGridView放在您的VB.net窗体上,然后再放置一个按钮。 Place this code in the Button
将此代码放在按钮中
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim masterTable As New DataTable
Dim cnnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=""Excel 12.0 Xml;HDR=NO"";Data Source=""{0}"";"
Using da As New OleDb.OleDbDataAdapter("select * from [Sheet1$] Where F1 = 'Test1'", String.Format(cnnStr, "C:\Book1.xlsx"))
da.Fill (masterTable)
End Using
DataGridView1.DataSource = masterTable
End Sub
End Class
And you are done :) 你完成了:)
SNAPSHOT 快照
I am using limited Data. 我正在使用有限的数据。
You can also change your string "select * from [Sheet1$] Where F1 = 'Test1'"
to "select F1 as Name,F2 as PN, F3 as [Inventory Loc] from [Sheet1$] Where F1 = 'Test1'"
to display the headers as below 您也可以将字符串
"select * from [Sheet1$] Where F1 = 'Test1'"
更改为"select F1 as Name,F2 as PN, F3 as [Inventory Loc] from [Sheet1$] Where F1 = 'Test1'"
"select * from [Sheet1$] Where F1 = 'Test1'"
"select F1 as Name,F2 as PN, F3 as [Inventory Loc] from [Sheet1$] Where F1 = 'Test1'"
显示标题如下
EDIT 编辑
In case you are wondering how to do it in VBA 如果您想知道如何在VBA中进行操作
USING VBA 使用VBA
Place a Listbox and a Command Button on a Form and then use this code. 将一个列表框和一个命令按钮放在窗体上,然后使用此代码。
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet, ws1 As Worksheet
Dim rng As Range
Dim lastRow As Long
Dim Ar As Variant
Set ws = Sheets("Sheet1")
lastRow = ws.Cells.Find(What:="*", After:=ws.Range("A1"), _
Lookat:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Set rng = ws.Range("A1:C" & lastRow)
Set ws1 = Sheets.Add
With rng
ws.AutoFilterMode = False
.AutoFilter Field:=1, Criteria1:="Test1"
.SpecialCells(xlCellTypeVisible).Copy ws1.Range("A1")
ws.AutoFilterMode = False
lastRow = ws1.Cells.Find(What:="*", After:=ws1.Range("A1"), _
Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
Ar = ws1.Range("A1:C" & lastRow)
Application.DisplayAlerts = False
ws1.Delete
Application.DisplayAlerts = True
End With
With Me.ListBox1
.Clear
.ColumnHeads = False
.ColumnCount = 3
.List = Ar
.ColumnWidths = "50;50;50"
.TopIndex = 0
End With
End Sub
SNAPSHOT 快照
MORE FOLLOWUP 更多跟进
Hi Siddharth, thank you very much for both code.
嗨,Siddharth,非常感谢您提供这两个代码。 For VB.
对于VB。 Net it is wonderful.
网络真是太好了。 For the VBA in Exel, is there any way that I can copy (using Ctrl + C) to copy the data - single row will be fine, although copy multiple rows is more desirable.
对于Exel中的VBA,有什么方法可以复制(使用Ctrl + C)以复制数据-单行就可以了,尽管更希望复制多行。 I am able to replace "Test1" with textbox i49.tinypic.com/2ceq3yf.jpg – user1370854 5 hours ago
我可以用文本框i49.tinypic.com/2ceq3yf.jpg替换“ Test1” – user1370854 5小时前
Yes it is possible to copy the single selected or multiple selected items from the listbox to cliboard. 是的,可以将列表框中的一个或多个选中的项目复制到cliboard。 To make the listobx multiselect, in the design mode, set the property of the listbox to
fmMultiSelectMulti1
. 若要使listobx多选,请在设计模式下,将列表框的属性设置为
fmMultiSelectMulti1
。 Next Add a command button and paste this code. 下一步添加命令按钮并粘贴此代码。
This code is again based on the data I use above so amend it as applicable. 该代码再次基于我上面使用的数据,因此请对其进行适当的修改。 When you press the
Copy
button, the data is copied to the clipboard and then you can simply use CTL V
to paste the data where ever you want; 当您按下“
Copy
按钮时,数据将被复制到剪贴板,然后您可以简单地使用CTL V
将数据粘贴到所需的位置。 for example in Notepad. 例如在记事本中。
Private Sub CommandButton2_Click()
Dim MyData As DataObject
Dim i As Long
Dim strCopiedText As String
Set MyData = New DataObject
With Me.ListBox1
For i = 1 To .ListCount
If .Selected(i - 1) Then
strCopiedText = strCopiedText & _
.List(i - 1, 0) & vbTab & _
.List(i - 1, 1) & vbTab & _
.List(i - 1, 2) & vbCrLf
End If
Next i
If Len(strCopiedText) > 0 Then
MyData.Clear
MyData.SetText strCopiedText
MyData.PutInClipboard
MsgBox "Data copied to clipboard"
End If
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.