VBA Identify numbers within a range - excel

I am sure this is a simple one but cant seem to find anything explicit out on the WWW.
I need my code to identify any numbers between 1 and 9 within a column for it to throw the error back for that range, I can only get it working with one number at present.
Public Sub ErrorCheck()
Dim FindString As String
Dim Rng As Range
FindString = ""
If VBA.Trim(FindString) <> "" Then
With Sheets("Scoring").Range("S:S")
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 "Error", True
Else
End If
End With
End If
End Sub
Thanks for your help!

You can execute your code iteratively for all the values you want. Example:
Public Sub ErrorCheck()
Dim FindString As String
Dim Rng As Range
Dim startVal As Integer, endVal As Integer
startVal = 1
endVal = 9
For i = startVal To endVal
FindString = CStr(i)
With Sheets("Scoring").Range("S:S")
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 "Error", True
Exit For
Else
End If
End With
Next i
End Sub

Related

How to declare scientific data type in my code

Sub Find_Value()
Dim FindString As Double
Dim Rng As Range``
FindString = InputBox("Enter a search value")
If Trim(FindString) <> "" Then
With Sheets("Sheet1").Range("D:D")
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
the inputbox from the code has to read the barcode data which usually gives data in this format "0.00000E+12". However, my present code just reads numbers...what do i do ? i am new to vba so... please help :)
Try the next code, please:
Sub Find_Value()
Dim sh As Worksheet, FindString As String, Rng As Range, lastRow As Long
Set sh = sheets("Sheet1")
lastRow = sh.Range("D" & Rows.count).End(xlUp).Row
FindString = InputBox("Enter a search value")
If FindString = "" Then MsgBox "No input...": Exit Sub
FindString = Trim(FindString) 'to trim spaces if they exist in the copied string...
If Trim(FindString) <> "" Then
With sh.Range("D1:D" & lastRow)
.TextToColumns Destination:=.cells(1), FieldInfo:=Array(1, 2)
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

Check if there is a "1" in column, if there is, then filter

I have this code that allows me to search for a string and if it's there, then filter for that string but if it's not there then don't do anything.
Dim Findvalue As String
Dim Rng As Range
Findvalue = "1"
With ws.Range("K:K")
Set Rng = .Find(What:=Findvalue, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
ws.Range("K4").AutoFilter _
Field:=11, _
Criteria1:="1"
Else
Exit Sub
End If
End With
This works if the value is a letter but doesn't work when it's a number.
The find method often has format-related problems; you can try with
Dim Findvalue As String
Findvalue = "1"
With ws
If Application.WorksheetFunction.CountIf(.Range("K:K"), Findvalue) > 0 Then
.Range("K4").AutoFilter _
Field:=11, _
Criteria1:="1" 'Criteria1:=Findvalue ?
End If
End With

Search a part of a description

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.

I want to search/find value in textbox2 in an active sheet using command button

I want to search/find value in textbox2 in an active sheet using command button
Here is my code:
Dim ws As Worksheet
Set ws = Worksheets("FSS-TEM-00025")
Dim FindString As String
Dim Rng As Range
FindString = Me.TextBox2.Value
If Trim(FindString) <> "" Then
With ws.Range("A1:Z1048576")
'Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)'
Set Rng = ws.Cells.Find(What:=FindString, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
Unload Me
Try this. This works for me
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim FindString As String
Dim Rng As Range
Set ws = ThisWorkbook.Worksheets("FSS-TEM-00025")
FindString = TextBox2.Value
If Trim(FindString) <> "" Then
Set Rng = ws.Cells.Find( _
What:=FindString, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End If
End Sub
Change LookAt:=xlPart to LookAt:=xlWhole if you are trying to find a complete match.
More on .Find here.

Using the Find Function in VBA

1
2
3
4
.
.
So I have a sequence of numbers running from 1-20. I have the number "1" on top selected and I would like to search the entire column and find the number "9". The code works when I don't name the range "rng"; it finds the number and selects. But the code stops working when I name the range of number. What's wrong with the range function? could it be that if I define Dim rng as Range that when I later define the "Set rng=" I cannot have the ".Select" or ".Copy" extension on the end?
Sub macro2()
Dim rng As Range
Set rng = Range(ActiveCell, ActiveCell.End(xlDown)).Select
rng.Find(10).Select
End Sub
Also, If I want to sum the entire column from 1-20, on the last cell below the number "20" should I use the following code? because the application object doesn't seem to do it. Thank you!
rng.End(xlDown).Offset(1, 0).Select
Application.WorksheetFunction.Sum (rng.Value)
To look for 10 in the active column you could try this (which ends up selecting the first 10 - although Select in vba isn't normally needed other than taken the user to location at code end)
test that the found range exists (ie you can find 10 before proceeding)
you should also use xlWhole to avoid matching 100 if the current default for [lookAt] is xlPart
using search [After] as Cells(1, ActiveCell.Column , and [Search Direction] as xlNext finds the first value looking down.
code
Sub QuickFind()
Dim rng1 As Range
Set rng1 = ActiveCell.EntireColumn.Find(10, Cells(1, ActiveCell.Column), xlFormulas, xlWhole, , xlNext)
If Not rng1 Is Nothing Then
Application.Goto rng1
Else
MsgBox "10 not found"
End If
End Sub
Part 2
Sub Other()
Dim rng1 As Range
Set rng1 = Range(Cells(1, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp))
rng1.Cells(rng1.Cells.Count).Offset(1, 0) = Application.WorksheetFunction.Sum(rng1.Value)
End Sub
Try this, I hope this will help u to find the specific row no as well as column name too. In code you can use
strRw = FindColumn(Sheet name, "Value which need to be found", True, "Cell Name",Row number)
sourceCOL = colname(FindColumn(Shee Name, "Value which need to be found", False, , 4))
Below is main function of find
Public Function FindColumn(colnocountWS As Worksheet, srcstr As String, Optional rowflag As Boolean, Optional bycol As String, Optional strw As Integer, Optional stcol As Integer) As Integer
Dim srcrng As Range 'range of search text
Dim srcAddr As String 'address of search text
Dim stcolnm As String
colnocountWS.Activate
If stcol <> 0 Then stcolnm = colname(stcol)
If stcol = 0 Then stcolnm = "A"
If strw = 0 Then strw = 1
colnocountWS.Range(stcolnm & strw).Select
If ActiveSheet.Range(stcolnm & strw) = srcstr Then
ActiveSheet.Range(stcolnm & strw).Select
FindColumn = 1
Else
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
'ByPart
If srcrng Is Nothing Then
If bycol = "" Then
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Else
Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End If
End If
If srcrng Is Nothing Then
FindColumn = 0
Exit Function
Else
srcAddr = srcrng.Address
colnocountWS.Range(srcAddr).Select
FindColumn = ActiveCell.Column
If rowflag = True Then FindColumn = ActiveCell.Row
End If
End If
End Function
'this function find column name
Public Function colname(iFinalCol1 As Integer) As String
Dim colnm As String
On Error GoTo gg
If Mid(Cells(1, iFinalCol1).Address, 3, 1) = "$" Then
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 1)
Else
colnm = Mid(Cells(1, iFinalCol1).Address, 2, 2)
End If
gg: colname = colnm
End Function

Resources