Search a part of a description - excel

Is there a way that i can search a part of a description in a cell?
because when i search for a part like UTP he give's me an error
Got anny idea?
Here is my code
'search for a cbxItem_Nr or a cbx_Description
Sub Find_test_click()
Dim FindString As String
Dim Rng As Range
FindString = InputBox("Voer de product code in")
If Trim(FindString) <> "" Then
With Sheets("Magazijn").Range("A:B") 'searches all of column A and B
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True 'value found
Else
MsgBox "Opgegeven product Niet gevonden" '(MsgBox) Saying Did not find the product
End If
End With
End If
End Sub
In the code there are some dutch words

Not sure if I understood the question, but maybe try to use "xlPart" instead of "xlWhole" as lookAt-parameter.

Related

Refer to entire column using variables

I have a find method below:
Sub Find_First()
Dim FindString As String
Dim Rng As Range
FindString = Range("s4").Value
If Trim(FindString) <> "" Then
With Sheet3.Range("A:A") 'searches all of column A
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
'Application.Goto Rng, True 'value found
MsgBox Rng.Address
Else
MsgBox "Nothing found" 'value not found
End If
End With
End If
End Sub
but when I tried to change the range("a:a") to a variable it would give me an error. for example:
col = range ("a1").value 'a1 gives a,b,c,d or e
with Sheet3.Range(col:col) 'this would give me a compile error Expected : list separator or )
why is that?
Sheet3.Range(col:col) needs to be Sheet3.Range(col & ":" & col) then.
Alternatively you can use Sheet3.Columns(col).
Instead of sheet.range you can use column, It should work; So try this;
Sub select_column()
col = Range("A1").Value
Columns(col).Select
End Sub
Hope this Helps...

Search and filter based on InputBox entry

I'm hoping to achieve the following:
Take user input via the Input box.
Search the table headers for that text.
Filter the found column to remove all blank cells (Leaving just the cells with data in.)
I've progressed a bit with a script I found, to give the input box, search the table header and select the found cell.
I need to merge into this the step of filtering the column of the found cell. If I record the steps it filters the same column no matter what I search for, so I think I need a way of reading back the found cell details and choosing that column to filter out blanks.
Sub Find_First()
Dim FindString As String
Dim Rng As Range
FindString = Application.InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("ACM").Range("B2:DA2") ' This is the table headers
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
I now have it working using the following code, the only error I now get is a 1004 (WorksheetFunction class) error if I cancel the InputBox :-
Sub Find_First()
Dim i1 As Integer
Dim FindString As String
Dim Rng As Range
Dim rngData As Range
Set rngData = Application.Range("A2").CurrentRegion
FindString = Application.InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("ACM").Range("B2:DA2") ' This is the table headers
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
i1 = Application.WorksheetFunction.Match(FindString, Application.Range("A2:CZ2"), 0)
Rng.AutoFilter Field:=i1, Criteria1:="<>"
End Sub
Looks like you really need the autofilter worked out:
I've done this in a similar scenario:
Dim i1 as Interger
Dim rngData as Range
Set rngData = ws.Range("A1").CurrentRegion
Using Match to find my column number matching FindString
i1 = Application.WorksheetFunction.Match(FindString, ws.Range("A1:CZ1"), 0)
rngData.AutoFilter Field:=i1, Criteria1:="<>"

How to get cell address from Find function in Excel VBA

How do I get cell address using Find function.
Here's the code
Dim Found As Range
Set Found = Worksheets("Sheet 1").Cells.Find(What:="test", LookAt:=xlWhole, MatchCase:=True)
If Not Found Is Nothing Then
' do something
End If
When I debug the code, "Found" variable contain a "string" instead of cell address.
It seems you can just use found.address even though it shows as string. The below code worked for me.
Sub findCellAddress()
Dim ra As Range
Set ra = Cells.Find(What:="fff", LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If ra Is Nothing Then
MsgBox ("Not found")
Else
MsgBox (ra.Address)
End If
End Sub
I could not find this anywhere on the internet.
This code will give you the row and the column.
Dim ThisPos As Range
With Range("A1:J100")
Set ThisPos = .Find(What:="List_Position", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not ThisPos Is Nothing Then
Cell_Add = Split(ThisPos.Address, "$")
ThisRow = Cell_Add(1)
ThisCol = Cell_Add(2)
End If
End With
This code will give you reference style of the cell address.
Dim SValue As Range
With Range("D1:D100")
Set SValue = .Find(What:="Searched Value", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not SValue Is Nothing Then
Cell_Split_R = Split(SValue.Address(ReferenceStyle:=xlR1C1), "R")
Cell_Split_C = Split(Cell_Split_R(1), "C")
SCol = Cell_Split_C(0)
SRow = Cell_Split_C(1)
End If
End With

looking for vba code to list today between no of dates in excel vba

I have a excel file with a schedule of date wise samples up to 3 years.
In that sheet how to find a today samples as a popup alert in VBA
Please help me.
So far as i think, this might ful-fill your purpose, this is a macro code in which it would ask the user to enter the date it want to search in a particular column, you can change the column by changing Range("A:A") to whatever you prefer.
Sub Find_Date()
Dim FindString As Date
Dim Rng As Range
FindString = InputBox("Please Enter Date")
If Trim(FindString) <> "" Then
With ActiveWorkbook.ActiveSheet.Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub

vba check value in column then check month and year of date in another column?

i am searching for a value in my column A on sheet 2. the below code works fine, and if the value is found it shows the message found else it shows not found if the value is not found.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
Dim FindString As String
Dim Rng As Range
FindString = Range("B1").Value
If Trim(FindString) <> "" Then
With Sheets("Sheet2").Range("A:A") 'searches all of column A
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
MsgBox "found" 'value not found
Else
MsgBox "Nothing found" 'value not found
End If
End With
End If
End If
End Sub
what i want to do is add to this code so that as well as searching column A for one value also search Column B for a different value? and then if both values exist say found else say not found.
would someone please be able to show me how i can do this?
Thanks
To keep with your flow, perhaps add another Find:
With Sheets("Sheet2").Range("A:A") 'searches all of column A
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
MsgBox "col A value found"
FindString = "<different find string>"
With Sheets("Sheet2").Range("B:B") 'searches all of column B
Set Rng = .Find(What:="<different find string>", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
MsgBox "both values found" 'value found
Else
MsgBox "col B values not found" 'value not found
End If
Else
MsgBox "col A value not found" 'value not found
End If
End With

Resources