[英]Using .find in a certain VBA program
I've made a invoice form and customers 'database' so I can easily make an invoice for my customers. 我已经制作了发票表格,并为客户创建了“数据库”,这样我就可以轻松地为我的客户制作发票。 I'm working with 2 sheets.
我正在处理2张纸。 Sheet 1 contains the invoice form and has a "find contact" macrobutton that locates the customers information by name (given in range"B12").
表格1包含发票表格,并具有“查找联系人”宏按钮,该宏按钮用于按名称查找客户信息(在“ B12”范围内)。 When the name is found in sheet 2 it automaticly copy's the information into sheet 1.
在工作表2中找到该名称时,它会自动将信息复制到工作表1中。
Only thing is, I have to search for the exact and entire name otherwise it won't find it. 唯一的是,我必须搜索确切的全名,否则找不到它。 If my contact is saved as "Nicolas Cage" it can't be found as "nicolas".
如果我的联系人另存为“ Nicolas Cage”,则找不到它为“ nicolas”。 So I want to know if I can integrate the next code...
所以我想知道我是否可以集成下一个代码...
.Find(What:="", , LookIn:=xlValues, LookAt:=xlPart)
(Or something which can be used to make it work.) (或者可以用来使其工作的东西。)
...in this code which I use to find the information and copy it from sheet2 to sheet1: ...在这段代码中,我用它来查找信息并将其从sheet2复制到sheet1:
Option Explicit
Sub ContactOproepen()
Dim customername As String
Dim Finalrow As Integer
Dim i As Integer
customername = Sheets("Sheet1").Range("B12").Value
Finalrow = Sheets("Sheet1").Range("A1000").End(xlUp).Row
For i = 2 To Finalrow
If Worksheets("Sheet2").Cells(i, 1) = customername Then
'Name
Worksheets("Sheet2").Cells(i, 1).Copy
Worksheets("Sheet1").Range("B12").PasteSpecial xlPasteFormulasAndNumberFormats
'Adress
Worksheets("Sheet2").Cells(i, 2).Copy
Worksheets("Sheet1").Range("B13").PasteSpecial xlPasteFormulasAndNumberFormats
'Postal & City
Worksheets("Sheet2").Cells(i, 3).Copy
Worksheets("Sheet1").Range("B14").PasteSpecial xlPasteFormulasAndNumberFormats
'Phonenumber
Worksheets("Sheet2").Cells(i, 4).Copy
Worksheets("Sheet1").Range("B15").PasteSpecial xlPasteFormulasAndNumberFormats
'E-mail
Worksheets("Sheet2").Cells(i, 5).Copy
Worksheets("Sheet1").Range("B16").PasteSpecial xlPasteFormulasAndNumberFormats
End If
Next i
Range("B12").Select
Application.CutCopyMode = False
If Range("B15") = "" Then
MsgBox "customer not found.", vbOKOnly, "Search customer"
End If
End Sub
It would be great if it had a msgbox which asks 'is this the customer you searched for?' 如果它有一个msgbox询问“这是您搜索的客户吗?”,那就太好了。 If it's NO it will go to the next customer till the right one is found.
如果否 ,它将转到下一个客户,直到找到合适的客户。 And if (eventually) it's YES it will proceed to copy everything and fill in the form.
如果(最终) 是 ,它将继续复制所有内容并填写表格。
I've been struggling for days now and can't find anything that will work. 我已经苦苦挣扎了好几天了,找不到任何可行的方法。 If you can help me that would be great!
如果您能帮助我,那太好了!
You could try this: 您可以尝试以下方法:
Dim rngFound As Range
Dim bNotTheGoodOne as Boolean
'first search
Set rngFound = Sheets("Sheet2").Columns(1).Cells.Find(What:=customername, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
If rngFound Is Nothing Then
MsgBox "No customer found", vbOKOnly
Else
'store first found address to avoid endless loop
FirstFound = rngFound.Address(False, False)
Do
'ask if it's the wanted customer
bNotTheGoodOne = MsgBox("Customer found: " & rngFound.Cells(1,1).Value & " . Find next ?", vbOKCancel)
If Not bNotTheGoodOne then
Worksheets("Sheet1").Range("B12").value = rngFound.Cells(1,1).Value
Worksheets("Sheet1").Range("B13").value = rngFound.Cells(1,1).offset(0,1).Value
Else
'if not, find next match
Set rngFound = wsSearch.Cells.FindNext(rngFound)
End if
Loop While Not rngFound Is Nothing And rngFound.Address(False, False) <> FirstFound
End If
I have found the solution! 我找到了解决方案! Added:
添加:
Dim foundrange As Range
'
Set foundrange = Sheets("Sheet2").Cells.Find(What:=Sheets("Sheet1").Range("B12").Value, LookIn:=xlFormulas, lookat:=xlPart)
So the code becomes: 因此,代码变为:
Sub ContactOproepen()
'
Dim Finalrow As Integer
Dim i As Integer
Dim cC As Object
Dim iR As Integer
Dim foundrange As Range
'
Set foundrange = Sheets("Sheet2").Cells.Find(What:=Sheets("Sheet1").Range("B12").Value, LookIn:=xlFormulas, lookat:=xlPart)
If Sheets("Sheet1").Range("B12").Value = "" Then
MsgBox "Fill in a name please", vbOKOnly, "Search customer"
Else
If foundrange Is Nothing Then
MsgBox " Customer not found," & vbNewLine & vbNewLine & " Try another searchkey.", vbOKOnly, "Search contact"
Else
Finalrow = Sheets("Sheet1").Range("A1000").End(xlUp).Row
For i = 2 To Finalrow
If Worksheets("Sheet2").Cells(i, 1) = foundrange Then
'Name
Worksheets("Sheet2").Cells(i, 1).Copy
Worksheets("Sheet1").Range("B12").PasteSpecial xlPasteFormulasAndNumberFormats
'Adress
Worksheets("Sheet2").Cells(i, 2).Copy
Worksheets("Sheet1").Range("B13").PasteSpecial xlPasteFormulasAndNumberFormats
'Postal & City
Worksheets("Sheet2").Cells(i, 3).Copy
Worksheets("Sheet1").Range("B14").PasteSpecial xlPasteFormulasAndNumberFormats
'Phonenumber
Worksheets("Sheet2").Cells(i, 4).Copy
Worksheets("Sheet1").Range("B15").PasteSpecial xlPasteFormulasAndNumberFormats
'E-mail
Worksheets("Sheet2").Cells(i, 5).Copy
Worksheets("Sheet1").Range("B16").PasteSpecial xlPasteFormulasAndNumberFormats
Range("B12").Select
End If
Next i
Set cC = New clsMsgbox
cC.Title = "Search Customer"
cC.Prompt = "Added Customer" & vbNewLine & "" & vbNewLine & "Is this the customer you were looking for?"
cC.Icon = Question + DefaultButton2
cC.ButtonText1 = "Yes"
cC.ButtonText2 = "No"
iR = cC.MessageBox()
If iR = Button1 Then
'Leave content in range
ElseIf iR = Button2 Then
Range("B12:E16").Select
Selection.ClearContents
Range("B12").Select
Range("B12").Select
Application.CutCopyMode = False
End If
End If
End If
End Sub
Thanks anyway! 不管怎么说,还是要谢谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.